Skip to content

Commit

Permalink
changed native function error
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 26, 2024
1 parent 9b86c0b commit 4c70985
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
20 changes: 20 additions & 0 deletions src/compiler/native_fn/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[derive(Clone, Debug)]
pub(crate) enum Error {
Unknown(String),
MissingArgument { name: String, available_arguments: Vec<String>, 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 {}
16 changes: 8 additions & 8 deletions src/compiler/native_fn/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -64,12 +64,12 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction {
executor: |arguments: HashMap<String, ByteBuffer>| {
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)
)
)?;
Expand All @@ -90,19 +90,19 @@ 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)
)
)?;

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)
)
)?;
Expand Down Expand Up @@ -139,11 +139,11 @@ 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, NativeFunctionError> {
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(|| NativeFunctionError::MissingArgument {
.ok_or_else(|| Error::MissingArgument {
name: pos.to_string(),
available_arguments: arguments.keys().cloned().collect(),
function_name: fn_name.to_string(),
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/native_fn/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 2 additions & 20 deletions src/compiler/native_fn/signature.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
use crate::compiler::util::ByteBuffer;
use std::collections::HashMap;

#[derive(Clone, Debug)]
pub(crate) enum NativeFunctionError {
Unknown(String),
MissingArgument { name: String, available_arguments: Vec<String>, 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 {
pub name: String,
}

type NativeFunctionExecutor =
fn(HashMap<String, ByteBuffer>) -> Result<ByteBuffer, NativeFunctionError>;
fn(HashMap<String, ByteBuffer>) -> Result<ByteBuffer, Error>;

#[derive(Clone, Debug)]
pub(crate) struct NativeFunction {
Expand Down
4 changes: 1 addition & 3 deletions src/compiler/rst/error.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 4c70985

Please sign in to comment.