From 4c70985b7db783f03b935e97a92bc263a7c4469e Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Wed, 26 Jun 2024 14:50:58 +0700 Subject: [PATCH] changed native function error --- src/compiler/native_fn/error.rs | 20 ++++++++++++++++++++ src/compiler/native_fn/implementations.rs | 16 ++++++++-------- src/compiler/native_fn/mod.rs | 2 ++ src/compiler/native_fn/signature.rs | 22 ++-------------------- src/compiler/rst/error.rs | 4 +--- 5 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 src/compiler/native_fn/error.rs diff --git a/src/compiler/native_fn/error.rs b/src/compiler/native_fn/error.rs new file mode 100644 index 0000000..fdb5f3c --- /dev/null +++ b/src/compiler/native_fn/error.rs @@ -0,0 +1,20 @@ +#[derive(Clone, Debug)] +pub(crate) enum Error { + Unknown(String), + MissingArgument { name: String, available_arguments: Vec, function_name: String }, +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Unknown(name) => write!(f, "Unknown error: {}", name), + Error::MissingArgument { name, available_arguments, function_name } => { + write!(f, "Missing argument {} for function {}. Available arguments: {:?}", + name, function_name, available_arguments + ) + } + } + } +} + +impl std::error::Error for Error {} diff --git a/src/compiler/native_fn/implementations.rs b/src/compiler/native_fn/implementations.rs index e92036e..bfd7c10 100644 --- a/src/compiler/native_fn/implementations.rs +++ b/src/compiler/native_fn/implementations.rs @@ -4,7 +4,7 @@ use std::io::Read; use clap::arg; use crate::compiler::native_fn::signature::{NativeFunction, NativeFunctionSignature}; -use crate::compiler::native_fn::NativeFunctionError; +use crate::compiler::native_fn::error::Error; use crate::compiler::util::ByteBuffer; pub(crate) fn create_len_native_function() -> NativeFunction { @@ -64,12 +64,12 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction { executor: |arguments: HashMap| { let command = get_argument_at(&arguments, 0, "cmd")? .as_string() - .map_err(|e| NativeFunctionError::Unknown(e.to_string()))?; + .map_err(|e| Error::Unknown(e.to_string()))?; let output = std::process::Command::new(command) .output() .map_err(|e| - NativeFunctionError::Unknown( + Error::Unknown( format!("Error executing command: {}", e) ) )?; @@ -90,11 +90,11 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction { let arg0 = get_argument_at(&arguments, 0, "read_file")?; let file_path = arg0.as_string() - .map_err(|e| NativeFunctionError::Unknown(e.to_string()))?; + .map_err(|e| Error::Unknown(e.to_string()))?; let mut file = File::open(file_path) .map_err(|e| - NativeFunctionError::Unknown( + Error::Unknown( format!("Error executing command: {}", e) ) )?; @@ -102,7 +102,7 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction { let mut buf_string = String::new(); file.read_to_string(&mut buf_string) .map_err(|e| - NativeFunctionError::Unknown( + Error::Unknown( format!("Error executing command: {}", e) ) )?; @@ -139,11 +139,11 @@ pub(crate) fn create_pad_native_function() -> NativeFunction { }; } -fn get_argument_at<'a>(arguments: &'a HashMap, pos: usize, fn_name: &str) -> Result<&'a ByteBuffer, NativeFunctionError> { +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(|| NativeFunctionError::MissingArgument { + .ok_or_else(|| Error::MissingArgument { name: pos.to_string(), available_arguments: arguments.keys().cloned().collect(), function_name: fn_name.to_string(), diff --git a/src/compiler/native_fn/mod.rs b/src/compiler/native_fn/mod.rs index 697589b..8f8a847 100644 --- a/src/compiler/native_fn/mod.rs +++ b/src/compiler/native_fn/mod.rs @@ -1,7 +1,9 @@ mod implementations; mod index; mod signature; +mod error; pub(crate) use implementations::*; pub(crate) use index::*; pub(crate) use signature::*; +pub(crate) use error::Error; \ No newline at end of file diff --git a/src/compiler/native_fn/signature.rs b/src/compiler/native_fn/signature.rs index 40e281c..68e26a2 100644 --- a/src/compiler/native_fn/signature.rs +++ b/src/compiler/native_fn/signature.rs @@ -1,24 +1,6 @@ use crate::compiler::util::ByteBuffer; use std::collections::HashMap; - -#[derive(Clone, Debug)] -pub(crate) enum NativeFunctionError { - Unknown(String), - MissingArgument { name: String, available_arguments: Vec, function_name: String }, -} - -impl std::fmt::Display for NativeFunctionError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - NativeFunctionError::Unknown(name) => write!(f, "Unknown error: {}", name), - NativeFunctionError::MissingArgument { name, available_arguments, function_name } => { - write!(f, "Missing argument {} for function {}. Available arguments: {:?}", - name, function_name, available_arguments - ) - } - } - } -} +use crate::compiler::native_fn::error::Error; #[derive(Clone, Debug)] pub(crate) struct NativeFunctionSignature { @@ -26,7 +8,7 @@ pub(crate) struct NativeFunctionSignature { } type NativeFunctionExecutor = - fn(HashMap) -> Result; + fn(HashMap) -> Result; #[derive(Clone, Debug)] pub(crate) struct NativeFunction { diff --git a/src/compiler/rst/error.rs b/src/compiler/rst/error.rs index d512cb7..e9007b3 100644 --- a/src/compiler/rst/error.rs +++ b/src/compiler/rst/error.rs @@ -1,10 +1,8 @@ -use crate::compiler::native_fn::NativeFunctionError; - #[derive(Debug)] pub(crate) enum Error { UnresolvedConstant { name: String }, UnresolvedFunction { name: String }, - NativeFunctionExecution(NativeFunctionError), + NativeFunctionExecution(crate::compiler::native_fn::Error), } impl std::fmt::Display for Error {