Better function parameter names.

This commit is contained in:
Stephen Chung
2021-08-13 13:42:39 +08:00
parent 4bf22e6cb7
commit dba4510456
11 changed files with 174 additions and 97 deletions

View File

@@ -753,17 +753,19 @@ mod array_functions {
#[rhai_fn(name = "==", return_raw, pure)]
pub fn equals(
ctx: NativeCallContext,
array: &mut Array,
mut array2: Array,
array1: &mut Array,
array2: Array,
) -> Result<bool, Box<EvalAltResult>> {
if array.len() != array2.len() {
if array1.len() != array2.len() {
return Ok(false);
}
if array.is_empty() {
if array1.is_empty() {
return Ok(true);
}
for (a1, a2) in array.iter_mut().zip(array2.iter_mut()) {
let mut array2 = array2;
for (a1, a2) in array1.iter_mut().zip(array2.iter_mut()) {
if !ctx
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [a1, a2])
.or_else(|err| match *err {
@@ -791,9 +793,9 @@ mod array_functions {
#[rhai_fn(name = "!=", return_raw, pure)]
pub fn not_equals(
ctx: NativeCallContext,
array: &mut Array,
array1: &mut Array,
array2: Array,
) -> Result<bool, Box<EvalAltResult>> {
equals(ctx, array, array2).map(|r| !r)
equals(ctx, array1, array2).map(|r| !r)
}
}

View File

@@ -10,15 +10,15 @@ def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
#[export_module]
mod fn_ptr_functions {
#[rhai_fn(name = "name", get = "name", pure)]
pub fn name(f: &mut FnPtr) -> ImmutableString {
f.fn_name_raw().into()
pub fn name(fn_ptr: &mut FnPtr) -> ImmutableString {
fn_ptr.fn_name_raw().into()
}
#[cfg(not(feature = "no_function"))]
pub mod functions {
#[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)]
pub fn is_anonymous(f: &mut FnPtr) -> bool {
f.is_anonymous()
pub fn is_anonymous(fn_ptr: &mut FnPtr) -> bool {
fn_ptr.is_anonymous()
}
}

View File

@@ -34,9 +34,10 @@ mod map_functions {
map.extend(map2.into_iter());
}
#[rhai_fn(name = "+")]
pub fn merge(mut map: Map, map2: Map) -> Map {
map.extend(map2.into_iter());
map
pub fn merge(map1: Map, map2: Map) -> Map {
let mut map1 = map1;
map1.extend(map2.into_iter());
map1
}
pub fn fill_with(map: &mut Map, map2: Map) {
map2.into_iter().for_each(|(key, value)| {
@@ -46,17 +47,19 @@ mod map_functions {
#[rhai_fn(name = "==", return_raw, pure)]
pub fn equals(
ctx: NativeCallContext,
map: &mut Map,
mut map2: Map,
map1: &mut Map,
map2: Map,
) -> Result<bool, Box<EvalAltResult>> {
if map.len() != map2.len() {
if map1.len() != map2.len() {
return Ok(false);
}
if map.is_empty() {
if map1.is_empty() {
return Ok(true);
}
for (m1, v1) in map.iter_mut() {
let mut map2 = map2;
for (m1, v1) in map1.iter_mut() {
if let Some(v2) = map2.get_mut(m1) {
let equals = ctx
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [v1, v2])
@@ -75,10 +78,10 @@ mod map_functions {
#[rhai_fn(name = "!=", return_raw, pure)]
pub fn not_equals(
ctx: NativeCallContext,
map: &mut Map,
map1: &mut Map,
map2: Map,
) -> Result<bool, Box<EvalAltResult>> {
equals(ctx, map, map2).map(|r| !r)
equals(ctx, map1, map2).map(|r| !r)
}
#[cfg(not(feature = "no_index"))]

View File

@@ -112,7 +112,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
#[export_module]
mod int_functions {
#[rhai_fn(name = "parse_int", return_raw)]
pub fn parse_int_radix(s: &str, radix: INT) -> Result<INT, Box<EvalAltResult>> {
pub fn parse_int_radix(string: &str, radix: INT) -> Result<INT, Box<EvalAltResult>> {
if !(2..=36).contains(&radix) {
return EvalAltResult::ErrorArithmetic(
format!("Invalid radix: '{}'", radix),
@@ -121,17 +121,17 @@ mod int_functions {
.into();
}
INT::from_str_radix(s.trim(), radix as u32).map_err(|err| {
INT::from_str_radix(string.trim(), radix as u32).map_err(|err| {
EvalAltResult::ErrorArithmetic(
format!("Error parsing integer number '{}': {}", s, err),
format!("Error parsing integer number '{}': {}", string, err),
Position::NONE,
)
.into()
})
}
#[rhai_fn(name = "parse_int", return_raw)]
pub fn parse_int(s: &str) -> Result<INT, Box<EvalAltResult>> {
parse_int_radix(s, 10)
pub fn parse_int(string: &str) -> Result<INT, Box<EvalAltResult>> {
parse_int_radix(string, 10)
}
}
@@ -283,10 +283,10 @@ mod float_functions {
}
}
#[rhai_fn(return_raw)]
pub fn parse_float(s: &str) -> Result<FLOAT, Box<EvalAltResult>> {
s.trim().parse::<FLOAT>().map_err(|err| {
pub fn parse_float(string: &str) -> Result<FLOAT, Box<EvalAltResult>> {
string.trim().parse::<FLOAT>().map_err(|err| {
EvalAltResult::ErrorArithmetic(
format!("Error parsing floating-point number '{}': {}", s, err),
format!("Error parsing floating-point number '{}': {}", string, err),
Position::NONE,
)
.into()
@@ -427,12 +427,12 @@ mod decimal_functions {
x.fract()
}
#[rhai_fn(return_raw)]
pub fn parse_decimal(s: &str) -> Result<Decimal, Box<EvalAltResult>> {
Decimal::from_str(s)
pub fn parse_decimal(string: &str) -> Result<Decimal, Box<EvalAltResult>> {
Decimal::from_str(string)
.or_else(|_| Decimal::from_scientific(s))
.map_err(|err| {
EvalAltResult::ErrorArithmetic(
format!("Error parsing decimal number '{}': {}", s, err),
format!("Error parsing decimal number '{}': {}", string, err),
Position::NONE,
)
.into()

View File

@@ -50,12 +50,13 @@ mod string_functions {
string1 + string2
}
#[rhai_fn(name = "+", name = "append")]
pub fn add_append_char(string: ImmutableString, ch: char) -> ImmutableString {
string + ch
pub fn add_append_char(string: ImmutableString, character: char) -> ImmutableString {
string + character
}
#[rhai_fn(name = "+", name = "append")]
pub fn add_append_unit(string: ImmutableString, _item: ()) -> ImmutableString {
pub fn add_append_unit(string: ImmutableString, item: ()) -> ImmutableString {
let _item = item;
string
}
#[rhai_fn(name = "+")]
@@ -369,11 +370,13 @@ mod string_functions {
#[rhai_fn(return_raw)]
pub fn pad(
_ctx: NativeCallContext,
ctx: NativeCallContext,
string: &mut ImmutableString,
len: INT,
character: char,
) -> Result<(), Box<crate::EvalAltResult>> {
let _ctx = ctx;
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
@@ -411,11 +414,13 @@ mod string_functions {
}
#[rhai_fn(name = "pad", return_raw)]
pub fn pad_with_string(
_ctx: NativeCallContext,
ctx: NativeCallContext,
string: &mut ImmutableString,
len: INT,
padding: &str,
) -> Result<(), Box<crate::EvalAltResult>> {
let _ctx = ctx;
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {

View File

@@ -54,14 +54,14 @@ mod time_functions {
#[rhai_fn(return_raw, name = "-")]
pub fn time_diff(
timestamp: Instant,
timestamp1: Instant,
timestamp2: Instant,
) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]
return Ok(if timestamp2 > timestamp {
-(timestamp2 - timestamp).as_secs_f64() as FLOAT
return Ok(if timestamp2 > timestamp1 {
-(timestamp2 - timestamp1).as_secs_f64() as FLOAT
} else {
(timestamp - timestamp2).as_secs_f64() as FLOAT
(timestamp1 - timestamp2).as_secs_f64() as FLOAT
}
.into());
@@ -225,27 +225,27 @@ mod time_functions {
}
#[rhai_fn(name = "==")]
pub fn eq(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp == timestamp2
pub fn eq(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 == timestamp2
}
#[rhai_fn(name = "!=")]
pub fn ne(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp != timestamp2
pub fn ne(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 != timestamp2
}
#[rhai_fn(name = "<")]
pub fn lt(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp < timestamp2
pub fn lt(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 < timestamp2
}
#[rhai_fn(name = "<=")]
pub fn lte(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp <= timestamp2
pub fn lte(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 <= timestamp2
}
#[rhai_fn(name = ">")]
pub fn gt(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp > timestamp2
pub fn gt(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 > timestamp2
}
#[rhai_fn(name = ">=")]
pub fn gte(timestamp: Instant, timestamp2: Instant) -> bool {
timestamp >= timestamp2
pub fn gte(timestamp1: Instant, timestamp2: Instant) -> bool {
timestamp1 >= timestamp2
}
}