rhai/src/packages/string_basic.rs

95 lines
3.2 KiB
Rust
Raw Normal View History

2020-04-21 17:01:10 +02:00
use crate::def_package;
2020-06-25 05:07:46 +02:00
use crate::engine::{FN_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT};
2020-06-25 12:07:57 +02:00
use crate::fn_native::FnPtr;
2020-05-13 13:21:42 +02:00
use crate::module::FuncReturn;
2020-05-25 07:44:28 +02:00
use crate::parser::{ImmutableString, INT};
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
#[cfg(not(feature = "no_object"))]
use crate::engine::Map;
use crate::stdlib::{
fmt::{Debug, Display},
format,
2020-05-26 08:14:03 +02:00
string::ToString,
};
// Register print and debug
2020-05-25 07:44:28 +02:00
fn to_debug<T: Debug>(x: &mut T) -> FuncReturn<ImmutableString> {
Ok(format!("{:?}", x).into())
}
2020-05-25 07:44:28 +02:00
fn to_string<T: Display>(x: &mut T) -> FuncReturn<ImmutableString> {
Ok(format!("{}", x).into())
}
#[cfg(not(feature = "no_object"))]
2020-05-25 07:44:28 +02:00
fn format_map(x: &mut Map) -> FuncReturn<ImmutableString> {
Ok(format!("#{:?}", x).into())
}
2020-05-13 13:21:42 +02:00
macro_rules! reg_op {
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
$( $lib.set_fn_1_mut($op, $func::<$par>); )*
};
}
2020-04-22 08:55:40 +02:00
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
2020-06-25 12:07:57 +02:00
reg_op!(lib, KEYWORD_PRINT, to_string, INT, bool, char, FnPtr);
reg_op!(lib, FN_TO_STRING, to_string, INT, bool, char, FnPtr);
lib.set_fn_1_mut(KEYWORD_DEBUG, |f: &mut FnPtr| Ok(f.to_string()));
2020-05-13 13:21:42 +02:00
lib.set_fn_0(KEYWORD_PRINT, || Ok("".to_string()));
lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string()));
2020-06-25 05:07:46 +02:00
lib.set_fn_1(FN_TO_STRING, |_: ()| Ok("".to_string()));
2020-05-25 11:01:39 +02:00
lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s));
2020-06-25 05:07:46 +02:00
lib.set_fn_1(FN_TO_STRING, |s: ImmutableString| Ok(s));
2020-05-25 07:44:28 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString);
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_op!(lib, KEYWORD_PRINT, to_string, i8, u8, i16, u16, i32, u32);
2020-06-25 05:07:46 +02:00
reg_op!(lib, FN_TO_STRING, to_string, i8, u8, i16, u16, i32, u32);
2020-04-21 17:01:10 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, i8, u8, i16, u16, i32, u32);
2020-06-17 10:50:46 +02:00
reg_op!(lib, KEYWORD_PRINT, to_string, i64, u64);
2020-06-25 05:07:46 +02:00
reg_op!(lib, FN_TO_STRING, to_string, i64, u64);
2020-06-17 10:50:46 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, i64, u64);
#[cfg(not(target_arch = "wasm32"))]
{
reg_op!(lib, KEYWORD_PRINT, to_string, i128, u128);
2020-06-25 05:07:46 +02:00
reg_op!(lib, FN_TO_STRING, to_string, i128, u128);
2020-06-17 10:50:46 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, i128, u128);
}
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_float"))]
{
reg_op!(lib, KEYWORD_PRINT, to_string, f32, f64);
2020-06-25 05:07:46 +02:00
reg_op!(lib, FN_TO_STRING, to_string, f32, f64);
2020-04-21 17:01:10 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, f32, f64);
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_index"))]
{
reg_op!(lib, KEYWORD_PRINT, to_debug, Array);
2020-06-25 05:07:46 +02:00
reg_op!(lib, FN_TO_STRING, to_debug, Array);
2020-04-21 17:01:10 +02:00
reg_op!(lib, KEYWORD_DEBUG, to_debug, Array);
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_object"))]
{
2020-05-13 13:21:42 +02:00
lib.set_fn_1_mut(KEYWORD_PRINT, format_map);
2020-06-25 05:07:46 +02:00
lib.set_fn_1_mut(FN_TO_STRING, format_map);
2020-05-13 13:21:42 +02:00
lib.set_fn_1_mut(KEYWORD_DEBUG, format_map);
}
2020-04-21 17:01:10 +02:00
2020-05-26 08:14:03 +02:00
lib.set_fn_2("+", |s: ImmutableString, ch: char| Ok(s + ch));
lib.set_fn_2_mut("+=", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) });
lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) });
lib.set_fn_2_mut("append", |s: &mut ImmutableString, s2: ImmutableString| { *s += &s2; Ok(()) });
2020-04-21 17:01:10 +02:00
});