Use then/then_some to simplify.

This commit is contained in:
Stephen Chung 2023-02-21 16:36:57 +08:00
parent 00f2b07d38
commit 129a5c6e86
6 changed files with 31 additions and 63 deletions

View File

@ -139,12 +139,8 @@ impl Engine {
// Built-in found // Built-in found
auto_restore! { let orig_level = global.level; global.level += 1 } auto_restore! { let orig_level = global.level; global.level += 1 }
let context = if need_context { let context = need_context
let source = global.source(); .then_some((self, op_x_str, global.source(), &*global, pos).into());
Some((self, op_x_str, source, &*global, pos).into())
} else {
None
};
return func(context, args).map(|_| ()); return func(context, args).map(|_| ());
} }
} }

View File

@ -398,11 +398,9 @@ impl Engine {
let is_method = func.is_method(); let is_method = func.is_method();
let src = source.as_ref().map(|s| s.as_str()); let src = source.as_ref().map(|s| s.as_str());
let context = if func.has_context() { let context = func
Some((self, name, src, &*global, pos).into()) .has_context()
} else { .then_some((self, name, src, &*global, pos).into());
None
};
let mut _result = if let Some(f) = func.get_plugin_fn() { let mut _result = if let Some(f) = func.get_plugin_fn() {
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() { if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
@ -1460,11 +1458,9 @@ impl Engine {
Some(f) if f.is_plugin_fn() => { Some(f) if f.is_plugin_fn() => {
let f = f.get_plugin_fn().expect("plugin function"); let f = f.get_plugin_fn().expect("plugin function");
let context = if f.has_context() { let context = f
Some((self, fn_name, module.id(), &*global, pos).into()) .has_context()
} else { .then_some((self, fn_name, module.id(), &*global, pos).into());
None
};
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() { if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into()) Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into())
} else { } else {
@ -1475,18 +1471,16 @@ impl Engine {
Some(f) if f.is_native() => { Some(f) if f.is_native() => {
let func = f.get_native_fn().expect("native function"); let func = f.get_native_fn().expect("native function");
let context = if f.has_context() { let context = f
Some((self, fn_name, module.id(), &*global, pos).into()) .has_context()
} else { .then_some((self, fn_name, module.id(), &*global, pos).into());
None
};
func(context, args).and_then(|r| self.check_data_size(r, pos)) func(context, args).and_then(|r| self.check_data_size(r, pos))
} }
Some(f) => unreachable!("unknown function type: {:?}", f), Some(f) => unreachable!("unknown function type: {:?}", f),
None => { None => Err(ERR::ErrorFunctionNotFound(
let sig = if namespace.is_empty() { if namespace.is_empty() {
self.gen_fn_call_signature(fn_name, args) self.gen_fn_call_signature(fn_name, args)
} else { } else {
format!( format!(
@ -1494,10 +1488,10 @@ impl Engine {
crate::engine::NAMESPACE_SEPARATOR, crate::engine::NAMESPACE_SEPARATOR,
self.gen_fn_call_signature(fn_name, args) self.gen_fn_call_signature(fn_name, args)
) )
}; },
pos,
Err(ERR::ErrorFunctionNotFound(sig, pos).into()) )
} .into()),
} }
} }
@ -1605,11 +1599,8 @@ impl Engine {
// Built-in found // Built-in found
auto_restore! { let orig_level = global.level; global.level += 1 } auto_restore! { let orig_level = global.level; global.level += 1 }
let context = if need_context { let context =
Some((self, name.as_str(), None, &*global, pos).into()) need_context.then_some((self, name.as_str(), None, &*global, pos).into());
} else {
None
};
return func(context, operands); return func(context, operands);
} }

View File

@ -86,7 +86,7 @@ impl Engine {
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len(); let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let orig_constants = if let Some(environ) = _environ { let orig_constants = _environ.map(|environ| {
let EncapsulatedEnviron { let EncapsulatedEnviron {
lib, lib,
imports, imports,
@ -100,10 +100,8 @@ impl Engine {
global.lib.push(lib.clone()); global.lib.push(lib.clone());
Some(mem::replace(&mut global.constants, constants.clone())) mem::replace(&mut global.constants, constants.clone())
} else { });
None
};
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.is_debugger_registered() { if self.is_debugger_registered() {

View File

@ -1137,11 +1137,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
_ if x.args.len() == 2 && x.op_token.is_some() && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => { _ if x.args.len() == 2 && x.op_token.is_some() && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => {
if let Some(result) = get_builtin_binary_op_fn(x.op_token.as_ref().unwrap(), &arg_values[0], &arg_values[1]) if let Some(result) = get_builtin_binary_op_fn(x.op_token.as_ref().unwrap(), &arg_values[0], &arg_values[1])
.and_then(|(f, ctx)| { .and_then(|(f, ctx)| {
let context = if ctx { let context = ctx.then_some((state.engine, x.name.as_str(), None, &state.global, *pos).into());
Some((state.engine, x.name.as_str(), None, &state.global, *pos).into())
} else {
None
};
let (first, second) = arg_values.split_first_mut().unwrap(); let (first, second) = arg_values.split_first_mut().unwrap();
(f)(context, &mut [ first, &mut second[0] ]).ok() (f)(context, &mut [ first, &mut second[0] ]).ok()
}) { }) {

View File

@ -215,11 +215,7 @@ impl<'e, 's> ParseState<'e, 's> {
self.allow_capture = true; self.allow_capture = true;
} }
let index = if hit_barrier { let index = (!hit_barrier).then(|| NonZeroUsize::new(index)).flatten();
None
} else {
NonZeroUsize::new(index)
};
(index, is_func_name) (index, is_func_name)
} }
@ -2357,12 +2353,8 @@ impl Engine {
let op = op_token.to_string(); let op = op_token.to_string();
let hash = calc_fn_hash(None, &op, 2); let hash = calc_fn_hash(None, &op, 2);
let is_valid_script_function = is_valid_function_name(&op); let native_only = !is_valid_function_name(&op);
let operator_token = if is_valid_script_function { let operator_token = native_only.then_some(op_token.clone());
None
} else {
Some(op_token.clone())
};
let mut args = FnArgsVec::new_const(); let mut args = FnArgsVec::new_const();
args.push(root); args.push(root);
@ -2446,10 +2438,10 @@ impl Engine {
.and_then(|m| m.get(s.as_str())) .and_then(|m| m.get(s.as_str()))
.map_or(false, Option::is_some) => .map_or(false, Option::is_some) =>
{ {
op_base.hashes = if is_valid_script_function { op_base.hashes = if native_only {
FnCallHashes::from_hash(calc_fn_hash(None, &s, 2))
} else {
FnCallHashes::from_native_only(calc_fn_hash(None, &s, 2)) FnCallHashes::from_native_only(calc_fn_hash(None, &s, 2))
} else {
FnCallHashes::from_hash(calc_fn_hash(None, &s, 2))
}; };
op_base.into_fn_call_expr(pos) op_base.into_fn_call_expr(pos)
} }
@ -3085,8 +3077,7 @@ impl Engine {
let (id, id_pos) = parse_var_name(input)?; let (id, id_pos) = parse_var_name(input)?;
let (alias, alias_pos) = if match_token(input, Token::As).0 { let (alias, alias_pos) = if match_token(input, Token::As).0 {
let (name, pos) = parse_var_name(input)?; parse_var_name(input).map(|(name, pos)| (Some(name), pos))?
(Some(name), pos)
} else { } else {
(None, Position::NONE) (None, Position::NONE)
}; };

View File

@ -1229,11 +1229,7 @@ fn get_next_token_inner(
// Still inside a comment? // Still inside a comment?
if state.comment_level > 0 { if state.comment_level > 0 {
let start_pos = *pos; let start_pos = *pos;
let mut comment = if state.include_comments { let mut comment = state.include_comments.then_some(String::new());
Some(String::new())
} else {
None
};
state.comment_level = state.comment_level =
scan_block_comment(stream, state.comment_level, pos, comment.as_mut()); scan_block_comment(stream, state.comment_level, pos, comment.as_mut());