diff --git a/src/api.rs b/src/api.rs index 84cefab4..f1c32155 100644 --- a/src/api.rs +++ b/src/api.rs @@ -87,12 +87,14 @@ impl Engine { self.script_fns.clear(); // Clean up engine match result { - Err(EvalAltResult::Return(out, pos)) => Ok(*out - .downcast::() - .map_err(|a| EvalAltResult::ErrorMismatchOutputType((*a).type_name(), pos))?), + Err(EvalAltResult::Return(out, pos)) => Ok(*out.downcast::().map_err(|a| { + let name = self.map_type_name((*a).type_name()); + EvalAltResult::ErrorMismatchOutputType(name, pos) + })?), Ok(out) => Ok(*out.downcast::().map_err(|a| { - EvalAltResult::ErrorMismatchOutputType((*a).type_name(), Position::eof()) + let name = self.map_type_name((*a).type_name()); + EvalAltResult::ErrorMismatchOutputType(name, Position::eof()) })?), Err(err) => Err(err), diff --git a/src/engine.rs b/src/engine.rs index 7818d96a..89544c16 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -149,6 +149,7 @@ pub struct Engine { pub(crate) script_fns: HashMap>, /// A hashmap containing all iterators known to the engine type_iterators: HashMap>, + type_names: HashMap, pub(crate) on_print: Box, pub(crate) on_debug: Box, @@ -188,9 +189,10 @@ impl Engine { self.call_fn_raw(ident.into(), args.into_vec(), None, pos) .and_then(|b| { - b.downcast() - .map(|b| *b) - .map_err(|a| EvalAltResult::ErrorMismatchOutputType((*a).type_name(), pos)) + b.downcast().map(|b| *b).map_err(|a| { + let name = self.map_type_name((*a).type_name()); + EvalAltResult::ErrorMismatchOutputType(name, pos) + }) }) } @@ -207,7 +209,7 @@ impl Engine { "Trying to call function {:?} with args {:?}", ident, args.iter() - .map(|x| Any::type_name(&**x)) + .map(|x| { self.map_type_name((**x).type_name()) }) .collect::>() ); @@ -266,13 +268,14 @@ impl Engine { // Return default value Ok(val.clone()) } else { - let type_names = args + let types_list = args .iter() .map(|x| (*(&**x).into_dynamic()).type_name()) + .map(|name| self.map_type_name(name)) .collect::>(); Err(EvalAltResult::ErrorFunctionNotFound( - format!("{} ({})", ident, type_names.join(", ")), + format!("{} ({})", ident, types_list.join(", ")), pos, )) } @@ -908,12 +911,33 @@ impl Engine { } } + pub(crate) fn map_type_name(&self, name: String) -> String { + self.type_names + .get(&name) + .map(|x| x.clone()) + .unwrap_or(name.to_string()) + } + /// Make a new engine pub fn new() -> Engine { + // User-friendly names for built-in types + let type_names = [ + ("alloc::string::String", "string"), + ( + "alloc::vec::Vec>", + "array", + ), + ("alloc::boxed::Box", "dynamic"), + ] + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + let mut engine = Engine { fns: HashMap::new(), script_fns: HashMap::new(), type_iterators: HashMap::new(), + type_names, on_print: Box::new(|x: &str| println!("{}", x)), on_debug: Box::new(|x: &str| println!("{}", x)), }; diff --git a/tests/decrement.rs b/tests/decrement.rs index 344c70ac..75d85076 100644 --- a/tests/decrement.rs +++ b/tests/decrement.rs @@ -9,7 +9,7 @@ fn test_decrement() -> Result<(), EvalAltResult> { let r = engine.eval::("let s = \"test\"; s -= \"ing\"; s"); match r { - Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err.starts_with("- ") => (), + Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "- (string, string)" => (), _ => panic!(), } diff --git a/tests/mismatched_op.rs b/tests/mismatched_op.rs index 314ea378..9964189b 100644 --- a/tests/mismatched_op.rs +++ b/tests/mismatched_op.rs @@ -7,7 +7,7 @@ fn test_mismatched_op() { let r = engine.eval::("60 + \"hello\""); match r { - Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "alloc::string::String" => (), + Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "string" => (), _ => panic!(), } }