Skip to content

Commit

Permalink
clippy run
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 26, 2024
1 parent 4c70985 commit 1125ef0
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 62 deletions.
10 changes: 4 additions & 6 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -89,11 +88,11 @@ fn run_watch(source: String, output: Option<String>) -> Result<(), Error> {
let mut watcher = notify::recommended_watcher(move |event: Result<Event, _>| {
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");

Expand Down Expand Up @@ -125,7 +124,7 @@ pub(crate) fn run_build(source: String, output: Option<String>) -> 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));

Expand Down
1 change: 0 additions & 1 deletion src/compiler/ast/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt::Display;
use pest::iterators::Pair;
use pest::Parser;
use pest_derive::Parser;
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ impl HexoCompiler {
source: &TSource,
) -> Result<AstNode, Error> {
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<TSource: CompilerSource>(
Expand All @@ -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<TSource: CompilerSource>(
Expand All @@ -46,7 +46,7 @@ impl HexoCompiler {

rst_compiler
.compile(&cst)
.map_err(Error::RST)
.map_err(Error::Rst)
}

pub(crate) fn compile<TSource: CompilerSource>(
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compiler_context.rs
Original file line number Diff line number Diff line change
@@ -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("."),

}
}
}
4 changes: 3 additions & 1 deletion src/compiler/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> 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),
}
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/error.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
}
Expand Down
41 changes: 18 additions & 23 deletions src/compiler/native_fn/implementations.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -140,19 +139,15 @@ pub(crate) fn create_pad_native_function() -> NativeFunction {
}

fn get_argument_at<'a>(arguments: &'a HashMap<String, ByteBuffer>, 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<String, ByteBuffer>, name: &str) -> Option<&'a ByteBuffer> {
let argument_named = arguments.get(name);

return argument_named
arguments.get(name)
}
6 changes: 3 additions & 3 deletions src/compiler/rst/compilation_context.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(),
}
Expand Down
16 changes: 9 additions & 7 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -143,7 +145,7 @@ impl RstCompiler<'_> {

fn build_context(
context_id: u64,
file_path: &PathBuf,
file_path: &Path,
cst: &CstFunctionStatement,
) -> Result<CompilationContext, Error> {
let mut root_context = CompilationContext::new(file_path);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/util/byte_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ByteBuffer {
}

pub(crate) fn as_string(&self) -> Result<String, FromUtf8Error> {
return String::from_utf8(self.inner.clone())
String::from_utf8(self.inner.clone())
}
}

Expand Down

0 comments on commit 1125ef0

Please sign in to comment.