From 1125ef0c0512967fd30373484fb1db5379954b3c Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Wed, 26 Jun 2024 15:14:29 +0700 Subject: [PATCH] clippy run --- src/cli/error.rs | 10 +++--- src/cli/mod.rs | 11 +++--- src/compiler/ast/parser.rs | 1 - src/compiler/compiler.rs | 8 ++--- src/compiler/compiler_context.rs | 4 +-- src/compiler/cst/parser.rs | 4 ++- src/compiler/error.rs | 16 ++++----- src/compiler/native_fn/implementations.rs | 41 ++++++++++------------- src/compiler/rst/compilation_context.rs | 6 ++-- src/compiler/rst/compiler.rs | 16 +++++---- src/compiler/util/byte_buffer.rs | 2 +- 11 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/cli/error.rs b/src/cli/error.rs index 6bb6839..4fe68d8 100644 --- a/src/cli/error.rs +++ b/src/cli/error.rs @@ -3,24 +3,22 @@ use std::fmt::{Display, Formatter}; #[derive(Debug)] pub(crate) enum Error { UnknownCommand, - CantCreateWatcher(notify::Error), - CantStartWatcher(notify::Error), + FileWatcher(notify::Error), CantCrateOutputFile(std::io::Error), CantReadInputFile(std::io::Error), AstParsingFailed(crate::compiler::ast::Error), - CompilationError(crate::compiler::Error), + Compilation(crate::compiler::Error), } impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Error::UnknownCommand => write!(f, "Unknown command"), - Error::CantCreateWatcher(e) => write!(f, "Can't create watcher: {}", e), - Error::CantStartWatcher(e) => write!(f, "Can't start watcher: {}", e), + Error::FileWatcher(e) => write!(f, "File watching error: {}", e), Error::CantCrateOutputFile(e) => write!(f, "Can't create output file: {}", e), Error::CantReadInputFile(e) => write!(f, "Can't read input file: {}", e), Error::AstParsingFailed(e) => write!(f, "Ast parsing error: {}", e), - Error::CompilationError(e) => write!(f, "Compilation error: {}", e), + Error::Compilation(e) => write!(f, "Compilation error: {}", e), } } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index e4fc45d..87a3097 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -61,12 +61,11 @@ fn handle_cli_error(cli_result: Result<(), Error>) { if cli_result.is_err() { match cli_result.unwrap_err() { Error::UnknownCommand => eprintln!("unknown command"), - Error::CantCreateWatcher(_) => eprintln!("can't create watcher"), - Error::CantStartWatcher(_) => eprintln!("can't start watcher"), + Error::FileWatcher(_) => eprintln!("can't create watcher"), Error::CantCrateOutputFile(_) => eprintln!("can't create output file"), Error::CantReadInputFile(_) => eprintln!("can't read input file"), Error::AstParsingFailed(_) => eprintln!("ast parsing error"), - Error::CompilationError(compilation_error) => { + Error::Compilation(compilation_error) => { handle_compilation_error(compilation_error) } } @@ -89,11 +88,11 @@ fn run_watch(source: String, output: Option) -> Result<(), Error> { let mut watcher = notify::recommended_watcher(move |event: Result| { run_watch_loop(source.clone(), output.clone(), event) }) - .map_err(Error::CantCreateWatcher)?; + .map_err(Error::FileWatcher)?; watcher .watch(source_path, RecursiveMode::NonRecursive) - .map_err(Error::CantStartWatcher)?; + .map_err(Error::FileWatcher)?; println!("watcher started"); @@ -125,7 +124,7 @@ pub(crate) fn run_build(source: String, output: Option) -> Result<(), Er let compilation_result = compiler .compile(&compiler_source) - .map_err(Error::CompilationError)?; + .map_err(Error::Compilation)?; let output_file_path = output.unwrap_or(format!("{}.bin", source)); diff --git a/src/compiler/ast/parser.rs b/src/compiler/ast/parser.rs index 43a1e53..6477ec5 100644 --- a/src/compiler/ast/parser.rs +++ b/src/compiler/ast/parser.rs @@ -1,4 +1,3 @@ -use std::fmt::Display; use pest::iterators::Pair; use pest::Parser; use pest_derive::Parser; diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index 733129b..9072ded 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -18,11 +18,11 @@ impl HexoCompiler { source: &TSource, ) -> Result { let ast_parser = AstParser::new(); - let source_text = source.read().map_err(Error::IO)?; + let source_text = source.read().map_err(Error::Io)?; ast_parser .parse(source_text) - .map_err(Error::AST) + .map_err(Error::Ast) } pub(crate) fn compile_cst( @@ -34,7 +34,7 @@ impl HexoCompiler { cst_parser .parse(source.path(), ast) - .map_err(Error::CST) + .map_err(Error::Cst) } pub(crate) fn compile_rst( @@ -46,7 +46,7 @@ impl HexoCompiler { rst_compiler .compile(&cst) - .map_err(Error::RST) + .map_err(Error::Rst) } pub(crate) fn compile( diff --git a/src/compiler/compiler_context.rs b/src/compiler/compiler_context.rs index 5e12f81..5928515 100644 --- a/src/compiler/compiler_context.rs +++ b/src/compiler/compiler_context.rs @@ -1,13 +1,13 @@ use std::path::PathBuf; pub(crate) struct HexoCompilerContext { - pub(crate) root_dir: PathBuf, + } impl HexoCompilerContext { pub(crate) fn new() -> Self { HexoCompilerContext { - root_dir: PathBuf::from("."), + } } } diff --git a/src/compiler/cst/parser.rs b/src/compiler/cst/parser.rs index d54f226..0451c78 100644 --- a/src/compiler/cst/parser.rs +++ b/src/compiler/cst/parser.rs @@ -308,7 +308,9 @@ fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec) -> Result<() let base_value_str = base.ok_or(Error::MissingContent { node_type: AstNodeType::AtomBaseNumberBase, })?; - let base_value = u32::from_str_radix(base_value_str.as_str(), 10).map_err(|_| { + + + let base_value = base_value_str.parse().map_err(|_| { Error::MalformedNodeValue { message: format!("can't parse base {}", base_value_str), } diff --git a/src/compiler/error.rs b/src/compiler/error.rs index fe40c88..b1c71b2 100644 --- a/src/compiler/error.rs +++ b/src/compiler/error.rs @@ -1,19 +1,19 @@ #[derive(Debug)] pub(crate) enum Error { - IO(std::io::Error), - AST(crate::compiler::ast::Error), - CST(crate::compiler::cst::Error), - RST(crate::compiler::rst::Error), + Io(std::io::Error), + Ast(crate::compiler::ast::Error), + Cst(crate::compiler::cst::Error), + Rst(crate::compiler::rst::Error), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Error::IO(e) => write!(f, "IO error: {}", e), - Error::AST(e) => write!(f, "AST error: {}", e), - Error::CST(e) => write!(f, "CST error: {}", e), - Error::RST(e) => write!(f, "RST error: {}", e), + Error::Io(e) => write!(f, "IO error: {}", e), + Error::Ast(e) => write!(f, "AST error: {}", e), + Error::Cst(e) => write!(f, "CST error: {}", e), + Error::Rst(e) => write!(f, "RST error: {}", e), } } } diff --git a/src/compiler/native_fn/implementations.rs b/src/compiler/native_fn/implementations.rs index bfd7c10..463041f 100644 --- a/src/compiler/native_fn/implementations.rs +++ b/src/compiler/native_fn/implementations.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; use std::fs::File; use std::io::Read; -use clap::arg; -use crate::compiler::native_fn::signature::{NativeFunction, NativeFunctionSignature}; use crate::compiler::native_fn::error::Error; +use crate::compiler::native_fn::signature::{NativeFunction, NativeFunctionSignature}; use crate::compiler::util::ByteBuffer; pub(crate) fn create_len_native_function() -> NativeFunction { @@ -69,9 +68,9 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction { let output = std::process::Command::new(command) .output() .map_err(|e| - Error::Unknown( - format!("Error executing command: {}", e) - ) + Error::Unknown( + format!("Error executing command: {}", e) + ) )?; let buffer = ByteBuffer::from(output.stdout); @@ -94,17 +93,17 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction { let mut file = File::open(file_path) .map_err(|e| - Error::Unknown( - format!("Error executing command: {}", e) - ) + Error::Unknown( + format!("Error executing command: {}", e) + ) )?; let mut buf_string = String::new(); file.read_to_string(&mut buf_string) .map_err(|e| - Error::Unknown( - format!("Error executing command: {}", e) - ) + Error::Unknown( + format!("Error executing command: {}", e) + ) )?; let buffer = ByteBuffer::from(buf_string.as_bytes().to_vec()); @@ -140,19 +139,15 @@ pub(crate) fn create_pad_native_function() -> NativeFunction { } fn get_argument_at<'a>(arguments: &'a HashMap, pos: usize, fn_name: &str) -> Result<&'a ByteBuffer, Error> { - Ok( - arguments - .get(&pos.to_string()) - .ok_or_else(|| Error::MissingArgument { - name: pos.to_string(), - available_arguments: arguments.keys().cloned().collect(), - function_name: fn_name.to_string(), - })? - ) + arguments + .get(&pos.to_string()) + .ok_or_else(|| Error::MissingArgument { + name: pos.to_string(), + available_arguments: arguments.keys().cloned().collect(), + function_name: fn_name.to_string(), + }) } fn get_named_argument<'a>(arguments: &'a HashMap, name: &str) -> Option<&'a ByteBuffer> { - let argument_named = arguments.get(name); - - return argument_named + arguments.get(name) } diff --git a/src/compiler/rst/compilation_context.rs b/src/compiler/rst/compilation_context.rs index 8dfa86c..3439ce7 100644 --- a/src/compiler/rst/compilation_context.rs +++ b/src/compiler/rst/compilation_context.rs @@ -1,7 +1,7 @@ use crate::compiler::cst::{CstEmitStatement}; use crate::compiler::native_fn::{NativeFunction, NativeFunctionIndex}; use std::collections::HashMap; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use crate::compiler::util::ByteBuffer; @@ -33,9 +33,9 @@ pub(crate) struct CompilationContext { } impl CompilationContext { - pub(crate) fn new(path: &PathBuf) -> CompilationContext { + pub(crate) fn new(path: &Path) -> CompilationContext { CompilationContext { - self_path: path.clone(), + self_path: path.to_path_buf(), local_contexts: HashMap::new(), native_function_index: NativeFunctionIndex::new(), } diff --git a/src/compiler/rst/compiler.rs b/src/compiler/rst/compiler.rs index c2fd76a..6981776 100644 --- a/src/compiler/rst/compiler.rs +++ b/src/compiler/rst/compiler.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::path::PathBuf; +use std::path::Path; use crate::compiler::cst::{ CstActualParameter, CstAtom, CstAtomVec, CstEmitStatement, CstFile, CstFunctionStatement, @@ -27,11 +27,13 @@ impl RstCompiler<'_> { let bb = Self::build_bytes(context_id, &mut context, &cst.main.emits)?; - Ok(HexoFile { - path: cst.path.clone(), - context, - emits: bb, - }) + Ok( + HexoFile { + path: cst.path.clone(), + context, + emits: bb, + } + ) } fn build_bytes( @@ -143,7 +145,7 @@ impl RstCompiler<'_> { fn build_context( context_id: u64, - file_path: &PathBuf, + file_path: &Path, cst: &CstFunctionStatement, ) -> Result { let mut root_context = CompilationContext::new(file_path); diff --git a/src/compiler/util/byte_buffer.rs b/src/compiler/util/byte_buffer.rs index 4a2268e..20ac3dc 100644 --- a/src/compiler/util/byte_buffer.rs +++ b/src/compiler/util/byte_buffer.rs @@ -72,7 +72,7 @@ impl ByteBuffer { } pub(crate) fn as_string(&self) -> Result { - return String::from_utf8(self.inner.clone()) + String::from_utf8(self.inner.clone()) } }