rhai/src/packages/map_basic.rs

98 lines
2.7 KiB
Rust
Raw Normal View History

#![cfg(not(feature = "no_object"))]
use crate::engine::OP_EQUALS;
2020-08-14 18:04:10 +02:00
use crate::plugin::*;
2020-11-16 16:10:14 +01:00
use crate::{def_package, Dynamic, ImmutableString, Map, INT};
2020-10-15 16:11:18 +02:00
#[cfg(not(feature = "no_index"))]
2020-11-16 09:28:04 +01:00
use crate::Array;
2020-04-24 06:39:24 +02:00
2020-04-22 08:55:40 +02:00
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
2020-09-13 16:12:11 +02:00
combine_with_exported_module!(lib, "map", map_functions);
2020-08-14 18:04:10 +02:00
});
#[export_module]
mod map_functions {
#[rhai_fn(pure)]
2020-08-14 18:04:10 +02:00
pub fn has(map: &mut Map, prop: ImmutableString) -> bool {
map.contains_key(&prop)
}
#[rhai_fn(pure)]
2020-08-14 18:04:10 +02:00
pub fn len(map: &mut Map) -> INT {
map.len() as INT
}
2020-08-14 18:04:10 +02:00
pub fn clear(map: &mut Map) {
2020-05-13 13:21:42 +02:00
map.clear();
2020-08-14 18:04:10 +02:00
}
pub fn remove(map: &mut Map, name: ImmutableString) -> Dynamic {
map.remove(&name).unwrap_or_else(|| ().into())
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "mixin", name = "+=")]
pub fn mixin(map: &mut Map, map2: Map) {
2020-08-14 18:04:10 +02:00
map2.into_iter().for_each(|(key, value)| {
map.insert(key, value);
2020-08-14 18:04:10 +02:00
});
}
#[rhai_fn(name = "+")]
pub fn merge(mut map: Map, map2: Map) -> Map {
2020-08-14 18:04:10 +02:00
map2.into_iter().for_each(|(key, value)| {
map.insert(key, value);
2020-08-14 18:04:10 +02:00
});
map
2020-08-14 18:04:10 +02:00
}
pub fn fill_with(map: &mut Map, map2: Map) {
2020-08-14 18:04:10 +02:00
map2.into_iter().for_each(|(key, value)| {
map.entry(key).or_insert(value);
2020-08-14 18:04:10 +02:00
});
}
#[rhai_fn(name = "==", return_raw, pure)]
2020-11-08 16:00:37 +01:00
pub fn equals(
2020-11-30 04:20:51 +01:00
ctx: NativeCallContext,
map: &mut Map,
2020-11-08 16:00:37 +01:00
mut map2: Map,
) -> Result<Dynamic, Box<EvalAltResult>> {
if map.len() != map2.len() {
2020-11-08 16:00:37 +01:00
return Ok(false.into());
}
if map.is_empty() {
2020-11-08 16:00:37 +01:00
return Ok(true.into());
}
for (m1, v1) in map.iter_mut() {
2020-11-08 16:00:37 +01:00
if let Some(v2) = map2.get_mut(m1) {
let equals = ctx
2021-03-01 08:39:49 +01:00
.call_fn_dynamic_raw(OP_EQUALS, true, &mut [v1, v2])
2020-11-08 16:00:37 +01:00
.map(|v| v.as_bool().unwrap_or(false))?;
if !equals {
return Ok(false.into());
}
} else {
return Ok(false.into());
}
}
Ok(true.into())
}
#[rhai_fn(name = "!=", return_raw, pure)]
2020-11-08 16:00:37 +01:00
pub fn not_equals(
ctx: NativeCallContext,
map: &mut Map,
2020-11-08 16:00:37 +01:00
map2: Map,
) -> Result<Dynamic, Box<EvalAltResult>> {
equals(ctx, map, map2).map(|r| (!r.as_bool().unwrap()).into())
2020-11-08 16:00:37 +01:00
}
2020-04-21 17:01:10 +02:00
2020-08-22 16:26:49 +02:00
#[cfg(not(feature = "no_index"))]
pub mod indexing {
#[rhai_fn(pure)]
2020-10-15 16:11:18 +02:00
pub fn keys(map: &mut Map) -> Array {
2020-08-22 16:26:49 +02:00
map.iter().map(|(k, _)| k.clone().into()).collect()
}
#[rhai_fn(pure)]
2020-10-15 16:11:18 +02:00
pub fn values(map: &mut Map) -> Array {
2020-08-22 16:26:49 +02:00
map.iter().map(|(_, v)| v.clone()).collect()
}
2020-08-14 18:04:10 +02:00
}
}