rhai/src/packages/string_basic.rs

143 lines
4.5 KiB
Rust
Raw Normal View History

2020-08-15 06:57:47 +02:00
#![allow(non_snake_case)]
use crate::plugin::*;
use crate::stdlib::{format, string::ToString};
use crate::{def_package, FnPtr, ImmutableString};
#[cfg(not(feature = "no_index"))]
2020-11-16 09:28:04 +01:00
use crate::Array;
#[cfg(not(feature = "no_object"))]
2020-11-16 09:28:04 +01:00
use crate::Map;
2020-11-30 04:20:51 +01:00
const FUNC_TO_DEBUG: &'static str = "to_debug";
2020-04-22 08:55:40 +02:00
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
2020-10-19 17:49:01 +02:00
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
2020-04-21 17:01:10 +02:00
});
2020-08-15 06:57:47 +02:00
2020-11-30 04:20:51 +01:00
// Register print and debug
2020-11-30 14:16:59 +01:00
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
2020-11-30 04:20:51 +01:00
#[inline(always)]
fn print_with_func(fn_name: &str, ctx: &NativeCallContext, value: &mut Dynamic) -> ImmutableString {
2021-03-01 08:39:49 +01:00
match ctx.call_fn_dynamic_raw(fn_name, true, &mut [value]) {
2020-11-30 04:20:51 +01:00
Ok(result) if result.is::<ImmutableString>() => result.take_immutable_string().unwrap(),
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
Err(_) => ctx.engine().map_type_name(value.type_name()).into(),
}
}
2020-08-15 06:57:47 +02:00
2020-10-19 17:49:01 +02:00
#[export_module]
mod print_debug_functions {
use crate::ImmutableString;
#[rhai_fn(name = "print", name = "to_string", pure)]
pub fn print_generic(item: &mut Dynamic) -> ImmutableString {
item.to_string().into()
}
#[rhai_fn(name = "debug", name = "to_debug", pure)]
pub fn debug_generic(item: &mut Dynamic) -> ImmutableString {
format!("{:?}", item).into()
}
2020-10-19 17:49:01 +02:00
#[rhai_fn(name = "print", name = "debug")]
pub fn print_empty_string() -> ImmutableString {
"".to_string().into()
}
#[rhai_fn(name = "print", name = "to_string")]
pub fn print_string(s: ImmutableString) -> ImmutableString {
s
}
2021-02-24 04:05:39 +01:00
#[rhai_fn(name = "debug", name = "to_debug", pure)]
2020-10-19 17:49:01 +02:00
pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
f.to_string().into()
}
#[cfg(not(feature = "no_float"))]
pub mod float_functions {
#[rhai_fn(name = "print", name = "to_string")]
pub fn print_f64(number: f64) -> ImmutableString {
let abs = number.abs();
if abs > 10000000000000.0 || abs < 0.0000000000001 {
format!("{:e}", number).into()
} else {
number.to_string().into()
}
}
#[rhai_fn(name = "print", name = "to_string")]
pub fn print_f32(number: f32) -> ImmutableString {
let abs = number.abs();
if abs > 10000000000000.0 || abs < 0.0000000000001 {
format!("{:e}", number).into()
} else {
number.to_string().into()
}
}
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_f64(number: f64) -> ImmutableString {
number.to_string().into()
}
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_f32(number: f32) -> ImmutableString {
number.to_string().into()
}
2020-10-19 17:49:01 +02:00
}
2020-09-23 06:00:03 +02:00
2020-11-30 04:20:51 +01:00
#[cfg(not(feature = "no_index"))]
pub mod array_functions {
use super::*;
#[rhai_fn(
name = "print",
name = "to_string",
name = "debug",
2021-02-24 04:05:39 +01:00
name = "to_debug",
pure
)]
pub fn format_array(ctx: NativeCallContext, array: &mut Array) -> ImmutableString {
let len = array.len();
2021-02-24 04:05:39 +01:00
let mut result = crate::stdlib::string::String::with_capacity(len * 5 + 2);
result.push_str("[");
2020-11-30 04:20:51 +01:00
array.iter_mut().enumerate().for_each(|(i, x)| {
2020-11-30 04:20:51 +01:00
result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, x));
if i < len - 1 {
result.push_str(", ");
}
});
result.push_str("]");
result.into()
}
}
2020-10-19 17:49:01 +02:00
#[cfg(not(feature = "no_object"))]
pub mod map_functions {
use super::*;
2020-10-08 16:25:50 +02:00
#[rhai_fn(
name = "print",
name = "to_string",
name = "debug",
2021-02-24 04:05:39 +01:00
name = "to_debug",
pure
)]
2020-11-30 04:20:51 +01:00
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString {
let len = map.len();
2021-02-24 04:05:39 +01:00
let mut result = crate::stdlib::string::String::with_capacity(len * 5 + 3);
result.push_str("#{");
2020-11-30 04:20:51 +01:00
map.iter_mut().enumerate().for_each(|(i, (k, v))| {
2020-12-07 15:21:02 +01:00
result.push_str(&format!(
"{:?}: {}{}",
k,
&print_with_func(FUNC_TO_DEBUG, &ctx, v),
if i < len - 1 { ", " } else { "" }
));
2020-11-30 04:20:51 +01:00
});
result.push_str("}");
result.into()
2020-10-19 17:49:01 +02:00
}
}
2020-08-15 06:57:47 +02:00
}