Simplify formatting.

This commit is contained in:
Stephen Chung 2022-03-14 08:50:17 +08:00
parent a704d26986
commit 672cda63ab
7 changed files with 53 additions and 93 deletions

View File

@ -69,12 +69,8 @@ impl Engine {
let typ = self.map_type_name(result.type_name()); let typ = self.map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
ERR::ErrorMismatchOutputType( let t = self.map_type_name(type_name::<T>()).into();
self.map_type_name(type_name::<T>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
typ.into(),
Position::NONE,
)
.into()
}) })
} }
/// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments and the /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments and the

View File

@ -195,12 +195,8 @@ impl Engine {
let typ = self.map_type_name(result.type_name()); let typ = self.map_type_name(result.type_name());
result.try_cast::<T>().ok_or_else(|| { result.try_cast::<T>().ok_or_else(|| {
ERR::ErrorMismatchOutputType( let t = self.map_type_name(type_name::<T>()).into();
self.map_type_name(type_name::<T>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
typ.into(),
Position::NONE,
)
.into()
}) })
} }
/// Evaluate an [`AST`] with own scope. /// Evaluate an [`AST`] with own scope.

View File

@ -125,7 +125,7 @@ impl Engine {
#[inline(never)] #[inline(never)]
#[must_use] #[must_use]
pub(crate) fn make_type_mismatch_err<T>(&self, typ: &str, pos: Position) -> RhaiError { pub(crate) fn make_type_mismatch_err<T>(&self, typ: &str, pos: Position) -> RhaiError {
ERR::ErrorMismatchDataType(self.map_type_name(type_name::<T>()).into(), typ.into(), pos) let t = self.map_type_name(type_name::<T>()).into();
.into() ERR::ErrorMismatchDataType(t, typ.into(), pos).into()
} }
} }

View File

@ -466,11 +466,8 @@ impl Engine {
KEYWORD_PRINT => { KEYWORD_PRINT => {
if let Some(ref print) = self.print { if let Some(ref print) = self.print {
let text = result.into_immutable_string().map_err(|typ| { let text = result.into_immutable_string().map_err(|typ| {
ERR::ErrorMismatchOutputType( let t = self.map_type_name(type_name::<ImmutableString>()).into();
self.map_type_name(type_name::<ImmutableString>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), pos)
typ.into(),
pos,
)
})?; })?;
(print(&text).into(), false) (print(&text).into(), false)
} else { } else {
@ -480,11 +477,8 @@ impl Engine {
KEYWORD_DEBUG => { KEYWORD_DEBUG => {
if let Some(ref debug) = self.debug { if let Some(ref debug) = self.debug {
let text = result.into_immutable_string().map_err(|typ| { let text = result.into_immutable_string().map_err(|typ| {
ERR::ErrorMismatchOutputType( let t = self.map_type_name(type_name::<ImmutableString>()).into();
self.map_type_name(type_name::<ImmutableString>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), pos)
typ.into(),
pos,
)
})?; })?;
let source = match global.source.as_str() { let source = match global.source.as_str() {
"" => None, "" => None,
@ -507,15 +501,10 @@ impl Engine {
crate::engine::FN_IDX_GET => { crate::engine::FN_IDX_GET => {
assert!(args.len() == 2); assert!(args.len() == 2);
Err(ERR::ErrorIndexingType( let t0 = self.map_type_name(args[0].type_name());
format!( let t1 = self.map_type_name(args[1].type_name());
"{} [{}]",
self.map_type_name(args[0].type_name()), Err(ERR::ErrorIndexingType(format!("{} [{}]", t0, t1), pos).into())
self.map_type_name(args[1].type_name())
),
pos,
)
.into())
} }
// index setter function not found? // index setter function not found?
@ -523,16 +512,11 @@ impl Engine {
crate::engine::FN_IDX_SET => { crate::engine::FN_IDX_SET => {
assert!(args.len() == 3); assert!(args.len() == 3);
Err(ERR::ErrorIndexingType( let t0 = self.map_type_name(args[0].type_name());
format!( let t1 = self.map_type_name(args[1].type_name());
"{} [{}] = {}", let t2 = self.map_type_name(args[3].type_name());
self.map_type_name(args[0].type_name()),
self.map_type_name(args[1].type_name()), Err(ERR::ErrorIndexingType(format!("{} [{}] = {}", t0, t1, t2), pos).into())
self.map_type_name(args[2].type_name())
),
pos,
)
.into())
} }
// Getter function not found? // Getter function not found?
@ -540,11 +524,13 @@ impl Engine {
_ if name.starts_with(crate::engine::FN_GET) => { _ if name.starts_with(crate::engine::FN_GET) => {
assert!(args.len() == 1); assert!(args.len() == 1);
let prop = &name[crate::engine::FN_GET.len()..];
let t0 = self.map_type_name(args[0].type_name());
Err(ERR::ErrorDotExpr( Err(ERR::ErrorDotExpr(
format!( format!(
"Unknown property '{}' - a getter is not registered for type '{}'", "Unknown property '{}' - a getter is not registered for type '{}'",
&name[crate::engine::FN_GET.len()..], prop, t0
self.map_type_name(args[0].type_name())
), ),
pos, pos,
) )
@ -556,12 +542,14 @@ impl Engine {
_ if name.starts_with(crate::engine::FN_SET) => { _ if name.starts_with(crate::engine::FN_SET) => {
assert!(args.len() == 2); assert!(args.len() == 2);
let prop = &name[crate::engine::FN_SET.len()..];
let t0 = self.map_type_name(args[0].type_name());
let t1 = self.map_type_name(args[1].type_name());
Err(ERR::ErrorDotExpr( Err(ERR::ErrorDotExpr(
format!( format!(
"No writable property '{}' - a setter is not registered for type '{}' to handle '{}'", "No writable property '{}' - a setter is not registered for type '{}' to handle '{}'",
&name[crate::engine::FN_SET.len()..], prop, t0, t1
self.map_type_name(args[0].type_name()),
self.map_type_name(args[1].type_name()),
), ),
pos, pos,
) )
@ -608,8 +596,11 @@ impl Engine {
level: usize, level: usize,
) -> RhaiResultOf<(Dynamic, bool)> { ) -> RhaiResultOf<(Dynamic, bool)> {
fn no_method_err(name: &str, pos: Position) -> RhaiResultOf<(Dynamic, bool)> { fn no_method_err(name: &str, pos: Position) -> RhaiResultOf<(Dynamic, bool)> {
let msg = format!("'{0}' should not be called this way. Try {0}(...);", name); Err(ERR::ErrorRuntime(
Err(ERR::ErrorRuntime(msg.into(), pos).into()) (format!("'{0}' should not be called this way. Try {0}(...);", name)).into(),
pos,
)
.into())
} }
// Check for data race. // Check for data race.
@ -623,10 +614,8 @@ impl Engine {
match fn_name { match fn_name {
// Handle type_of() // Handle type_of()
KEYWORD_TYPE_OF if args.len() == 1 => { KEYWORD_TYPE_OF if args.len() == 1 => {
return Ok(( let typ = self.map_type_name(args[0].type_name()).to_string().into();
self.map_type_name(args[0].type_name()).to_string().into(), return Ok((typ, false));
false,
))
} }
// Handle is_def_fn() // Handle is_def_fn()
@ -816,16 +805,12 @@ impl Engine {
KEYWORD_FN_PTR_CALL => { KEYWORD_FN_PTR_CALL => {
if !call_args.is_empty() { if !call_args.is_empty() {
if !call_args[0].is::<FnPtr>() { if !call_args[0].is::<FnPtr>() {
return Err(self.make_type_mismatch_err::<FnPtr>( let typ = self.map_type_name(call_args[0].type_name());
self.map_type_name(call_args[0].type_name()), return Err(self.make_type_mismatch_err::<FnPtr>(typ, *call_arg_pos));
*call_arg_pos,
));
} }
} else { } else {
return Err(self.make_type_mismatch_err::<FnPtr>( let typ = self.map_type_name(target.type_name());
self.map_type_name(target.type_name()), return Err(self.make_type_mismatch_err::<FnPtr>(typ, pos));
pos,
));
} }
// FnPtr call on object // FnPtr call on object
@ -855,10 +840,8 @@ impl Engine {
} }
KEYWORD_FN_PTR_CURRY => { KEYWORD_FN_PTR_CURRY => {
if !target.is::<FnPtr>() { if !target.is::<FnPtr>() {
return Err(self.make_type_mismatch_err::<FnPtr>( let typ = self.map_type_name(target.type_name());
self.map_type_name(target.type_name()), return Err(self.make_type_mismatch_err::<FnPtr>(typ, pos));
pos,
));
} }
let fn_ptr = target.read_lock::<FnPtr>().expect("`FnPtr`"); let fn_ptr = target.read_lock::<FnPtr>().expect("`FnPtr`");
@ -1005,10 +988,8 @@ impl Engine {
self.get_arg_value(scope, global, state, lib, this_ptr, arg, level)?; self.get_arg_value(scope, global, state, lib, this_ptr, arg, level)?;
if !arg_value.is::<FnPtr>() { if !arg_value.is::<FnPtr>() {
return Err(self.make_type_mismatch_err::<FnPtr>( let typ = self.map_type_name(arg_value.type_name());
self.map_type_name(arg_value.type_name()), return Err(self.make_type_mismatch_err::<FnPtr>(typ, arg_pos));
arg_pos,
));
} }
let fn_ptr = arg_value.cast::<FnPtr>(); let fn_ptr = arg_value.cast::<FnPtr>();
@ -1055,10 +1036,8 @@ impl Engine {
self.get_arg_value(scope, global, state, lib, this_ptr, first, level)?; self.get_arg_value(scope, global, state, lib, this_ptr, first, level)?;
if !arg_value.is::<FnPtr>() { if !arg_value.is::<FnPtr>() {
return Err(self.make_type_mismatch_err::<FnPtr>( let typ = self.map_type_name(arg_value.type_name());
self.map_type_name(arg_value.type_name()), return Err(self.make_type_mismatch_err::<FnPtr>(typ, arg_pos));
arg_pos,
));
} }
let (name, fn_curry) = arg_value.cast::<FnPtr>().take_data(); let (name, fn_curry) = arg_value.cast::<FnPtr>().take_data();

View File

@ -277,12 +277,8 @@ impl<'a> NativeCallContext<'a> {
let typ = self.engine().map_type_name(result.type_name()); let typ = self.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
ERR::ErrorMismatchOutputType( let t = self.engine().map_type_name(type_name::<T>()).into();
self.engine().map_type_name(type_name::<T>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
typ.into(),
Position::NONE,
)
.into()
}) })
} }
/// Call a function inside the call context. /// Call a function inside the call context.

View File

@ -1085,7 +1085,8 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
match x.name.as_str() { match x.name.as_str() {
KEYWORD_TYPE_OF if arg_values.len() == 1 => { KEYWORD_TYPE_OF if arg_values.len() == 1 => {
state.set_dirty(); state.set_dirty();
*expr = Expr::from_dynamic(state.engine.map_type_name(arg_values[0].type_name()).into(), *pos); let typ = state.engine.map_type_name(arg_values[0].type_name()).into();
*expr = Expr::from_dynamic(typ, *pos);
return; return;
} }
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]

View File

@ -162,12 +162,8 @@ impl FnPtr {
let typ = engine.map_type_name(result.type_name()); let typ = engine.map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
ERR::ErrorMismatchOutputType( let t = engine.map_type_name(type_name::<T>()).into();
engine.map_type_name(type_name::<T>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
typ.into(),
Position::NONE,
)
.into()
}) })
} }
/// Call the function pointer with curried arguments (if any). /// Call the function pointer with curried arguments (if any).
@ -190,12 +186,8 @@ impl FnPtr {
let typ = context.engine().map_type_name(result.type_name()); let typ = context.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
ERR::ErrorMismatchOutputType( let t = context.engine().map_type_name(type_name::<T>()).into();
context.engine().map_type_name(type_name::<T>()).into(), ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into()
typ.into(),
Position::NONE,
)
.into()
}) })
} }
/// Call the function pointer with curried arguments (if any). /// Call the function pointer with curried arguments (if any).