Reduce nesting in plugin sub-modules.

This commit is contained in:
Stephen Chung 2021-10-20 16:22:12 +08:00
parent ae493918a2
commit 313999b0ac
4 changed files with 126 additions and 142 deletions

View File

@ -15,20 +15,16 @@ mod fn_ptr_functions {
} }
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub mod functions { #[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)]
#[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)] pub fn is_anonymous(fn_ptr: &mut FnPtr) -> bool {
pub fn is_anonymous(fn_ptr: &mut FnPtr) -> bool { fn_ptr.is_anonymous()
fn_ptr.is_anonymous()
}
} }
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub mod functions_and_maps { pub fn get_fn_metadata_list(ctx: NativeCallContext) -> crate::Array {
pub fn get_fn_metadata_list(ctx: NativeCallContext) -> crate::Array { collect_fn_metadata(ctx)
collect_fn_metadata(ctx)
}
} }
} }

View File

@ -108,22 +108,21 @@ mod map_functions {
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub mod indexing { #[rhai_fn(pure)]
#[rhai_fn(pure)] pub fn keys(map: &mut Map) -> Array {
pub fn keys(map: &mut Map) -> Array { if map.is_empty() {
if map.is_empty() { Array::new()
Array::new() } else {
} else { map.keys().cloned().map(Into::<Dynamic>::into).collect()
map.keys().cloned().map(Into::<Dynamic>::into).collect()
}
} }
#[rhai_fn(pure)] }
pub fn values(map: &mut Map) -> Array { #[cfg(not(feature = "no_index"))]
if map.is_empty() { #[rhai_fn(pure)]
Array::new() pub fn values(map: &mut Map) -> Array {
} else { if map.is_empty() {
map.values().cloned().collect() Array::new()
} } else {
map.values().cloned().collect()
} }
} }
} }

View File

@ -293,11 +293,9 @@ mod float_functions {
}) })
} }
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
pub mod f32_f64 { #[rhai_fn(name = "to_float")]
#[rhai_fn(name = "to_float")] pub fn f32_to_f64(x: f32) -> f64 {
pub fn f32_to_f64(x: f32) -> f64 { x as f64
x as f64
}
} }
} }
@ -308,21 +306,23 @@ mod decimal_functions {
prelude::{FromStr, RoundingStrategy}, prelude::{FromStr, RoundingStrategy},
Decimal, MathematicalOps, Decimal, MathematicalOps,
}; };
#[cfg(not(feature = "no_float"))]
use std::convert::TryFrom;
#[cfg(feature = "no_float")] #[cfg(feature = "no_float")]
pub mod float_polyfills { #[rhai_fn(name = "PI")]
#[rhai_fn(name = "PI")] pub fn pi() -> Decimal {
pub fn pi() -> Decimal { Decimal::PI
Decimal::PI }
} #[cfg(feature = "no_float")]
#[rhai_fn(name = "E")] #[rhai_fn(name = "E")]
pub fn e() -> Decimal { pub fn e() -> Decimal {
Decimal::E Decimal::E
} }
#[rhai_fn(return_raw)] #[cfg(feature = "no_float")]
pub fn parse_float(s: &str) -> Result<Decimal, Box<EvalAltResult>> { #[rhai_fn(return_raw)]
super::parse_decimal(s) pub fn parse_float(s: &str) -> Result<Decimal, Box<EvalAltResult>> {
} super::parse_decimal(s)
} }
pub fn sin(x: Decimal) -> Decimal { pub fn sin(x: Decimal) -> Decimal {
@ -480,39 +480,37 @@ mod decimal_functions {
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub mod float { #[rhai_fn(name = "to_decimal", return_raw)]
use std::convert::TryFrom; pub fn f32_to_decimal(x: f32) -> Result<Decimal, Box<EvalAltResult>> {
Decimal::try_from(x).map_err(|_| {
#[rhai_fn(name = "to_decimal", return_raw)] EvalAltResult::ErrorArithmetic(
pub fn f32_to_decimal(x: f32) -> Result<Decimal, Box<EvalAltResult>> { format!("Cannot convert to Decimal: to_decimal({})", x),
Decimal::try_from(x).map_err(|_| { Position::NONE,
EvalAltResult::ErrorArithmetic( )
format!("Cannot convert to Decimal: to_decimal({})", x), .into()
Position::NONE, })
) }
.into() #[cfg(not(feature = "no_float"))]
}) #[rhai_fn(name = "to_decimal", return_raw)]
} pub fn f64_to_decimal(x: f64) -> Result<Decimal, Box<EvalAltResult>> {
#[rhai_fn(name = "to_decimal", return_raw)] Decimal::try_from(x).map_err(|_| {
pub fn f64_to_decimal(x: f64) -> Result<Decimal, Box<EvalAltResult>> { EvalAltResult::ErrorArithmetic(
Decimal::try_from(x).map_err(|_| { format!("Cannot convert to Decimal: to_decimal({})", x),
EvalAltResult::ErrorArithmetic( Position::NONE,
format!("Cannot convert to Decimal: to_decimal({})", x), )
Position::NONE, .into()
) })
.into() }
}) #[cfg(not(feature = "no_float"))]
} #[rhai_fn(return_raw)]
#[rhai_fn(return_raw)] pub fn to_float(x: Decimal) -> Result<FLOAT, Box<EvalAltResult>> {
pub fn to_float(x: Decimal) -> Result<FLOAT, Box<EvalAltResult>> { FLOAT::try_from(x).map_err(|_| {
FLOAT::try_from(x).map_err(|_| { EvalAltResult::ErrorArithmetic(
EvalAltResult::ErrorArithmetic( format!("Cannot convert to floating-point: to_float({})", x),
format!("Cannot convert to floating-point: to_float({})", x), Position::NONE,
Position::NONE, )
) .into()
.into() })
})
}
} }
} }

View File

@ -71,82 +71,73 @@ mod print_debug_functions {
} }
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub mod float_functions { #[rhai_fn(name = "print", name = "to_string")]
use crate::ast::FloatWrapper; pub fn print_f64(number: f64) -> ImmutableString {
crate::ast::FloatWrapper::new(number).to_string().into()
#[rhai_fn(name = "print", name = "to_string")] }
pub fn print_f64(number: f64) -> ImmutableString { #[cfg(not(feature = "no_float"))]
FloatWrapper::new(number).to_string().into() #[rhai_fn(name = "print", name = "to_string")]
} pub fn print_f32(number: f32) -> ImmutableString {
#[rhai_fn(name = "print", name = "to_string")] crate::ast::FloatWrapper::new(number).to_string().into()
pub fn print_f32(number: f32) -> ImmutableString { }
FloatWrapper::new(number).to_string().into() #[cfg(not(feature = "no_float"))]
} #[rhai_fn(name = "debug", name = "to_debug")]
#[rhai_fn(name = "debug", name = "to_debug")] pub fn debug_f64(number: f64) -> ImmutableString {
pub fn debug_f64(number: f64) -> ImmutableString { format!("{:?}", crate::ast::FloatWrapper::new(number)).into()
format!("{:?}", FloatWrapper::new(number)).into() }
} #[cfg(not(feature = "no_float"))]
#[rhai_fn(name = "debug", name = "to_debug")] #[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_f32(number: f32) -> ImmutableString { pub fn debug_f32(number: f32) -> ImmutableString {
format!("{:?}", FloatWrapper::new(number)).into() format!("{:?}", crate::ast::FloatWrapper::new(number)).into()
}
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub mod array_functions { #[rhai_fn(
use super::*; name = "print",
name = "to_string",
name = "debug",
name = "to_debug",
pure
)]
pub fn format_array(ctx: NativeCallContext, array: &mut Array) -> ImmutableString {
let len = array.len();
let mut result = String::with_capacity(len * 5 + 2);
result.push('[');
#[rhai_fn( array.iter_mut().enumerate().for_each(|(i, x)| {
name = "print", result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, x));
name = "to_string", if i < len - 1 {
name = "debug", result.push_str(", ");
name = "to_debug", }
pure });
)]
pub fn format_array(ctx: NativeCallContext, array: &mut Array) -> ImmutableString {
let len = array.len();
let mut result = String::with_capacity(len * 5 + 2);
result.push('[');
array.iter_mut().enumerate().for_each(|(i, x)| { result.push(']');
result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, x)); result.into()
if i < len - 1 {
result.push_str(", ");
}
});
result.push(']');
result.into()
}
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub mod map_functions { #[rhai_fn(
use super::*; name = "print",
name = "to_string",
name = "debug",
name = "to_debug",
pure
)]
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString {
let len = map.len();
let mut result = String::with_capacity(len * 5 + 3);
result.push_str("#{");
#[rhai_fn( map.iter_mut().enumerate().for_each(|(i, (k, v))| {
name = "print", result.push_str(&format!(
name = "to_string", "{:?}: {}{}",
name = "debug", k,
name = "to_debug", &print_with_func(FUNC_TO_DEBUG, &ctx, v),
pure if i < len - 1 { ", " } else { "" }
)] ));
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString { });
let len = map.len();
let mut result = String::with_capacity(len * 5 + 3);
result.push_str("#{");
map.iter_mut().enumerate().for_each(|(i, (k, v))| { result.push('}');
result.push_str(&format!( result.into()
"{:?}: {}{}",
k,
&print_with_func(FUNC_TO_DEBUG, &ctx, v),
if i < len - 1 { ", " } else { "" }
));
});
result.push('}');
result.into()
}
} }
} }