rhai/src/packages/map_basic.rs

123 lines
3.2 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};
2021-04-17 09:15:54 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
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, {
lib.standard = true;
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 len(map: &mut Map) -> INT {
map.len() as INT
}
2020-08-14 18:04:10 +02:00
pub fn clear(map: &mut Map) {
if !map.is_empty() {
map.clear();
}
2020-08-14 18:04:10 +02:00
}
pub fn remove(map: &mut Map, name: ImmutableString) -> Dynamic {
if !map.is_empty() {
map.remove(name.as_str()).unwrap_or_else(|| Dynamic::UNIT)
} else {
Dynamic::UNIT
}
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "mixin", name = "+=")]
pub fn mixin(map: &mut Map, map2: Map) {
if !map2.is_empty() {
map.extend(map2.into_iter());
}
2020-08-14 18:04:10 +02:00
}
#[rhai_fn(name = "+")]
2021-08-13 07:42:39 +02:00
pub fn merge(map1: Map, map2: Map) -> Map {
if map2.is_empty() {
map1
} else if map1.is_empty() {
map2
} else {
let mut map1 = map1;
map1.extend(map2.into_iter());
map1
}
2020-08-14 18:04:10 +02:00
}
pub fn fill_with(map: &mut Map, map2: Map) {
if !map2.is_empty() {
if map.is_empty() {
*map = map2;
} else {
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,
2021-08-13 07:42:39 +02:00
map1: &mut Map,
map2: Map,
2021-03-22 04:18:09 +01:00
) -> Result<bool, Box<EvalAltResult>> {
2021-08-13 07:42:39 +02:00
if map1.len() != map2.len() {
2021-03-22 04:18:09 +01:00
return Ok(false);
2020-11-08 16:00:37 +01:00
}
if !map1.is_empty() {
let mut map2 = map2;
2021-08-13 07:42:39 +02:00
for (m1, v1) in map1.iter_mut() {
if let Some(v2) = map2.get_mut(m1) {
let equals = ctx
.call_fn_raw(OP_EQUALS, true, false, &mut [v1, v2])
.map(|v| v.as_bool().unwrap_or(false))?;
2020-11-08 16:00:37 +01:00
if !equals {
return Ok(false);
}
} else {
2021-03-22 04:18:09 +01:00
return Ok(false);
2020-11-08 16:00:37 +01:00
}
}
}
2021-03-22 04:18:09 +01:00
Ok(true)
2020-11-08 16:00:37 +01:00
}
#[rhai_fn(name = "!=", return_raw, pure)]
2020-11-08 16:00:37 +01:00
pub fn not_equals(
ctx: NativeCallContext,
2021-08-13 07:42:39 +02:00
map1: &mut Map,
2020-11-08 16:00:37 +01:00
map2: Map,
2021-03-22 04:18:09 +01:00
) -> Result<bool, Box<EvalAltResult>> {
2021-08-13 07:42:39 +02:00
equals(ctx, map1, map2).map(|r| !r)
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"))]
2021-10-20 10:22:12 +02:00
#[rhai_fn(pure)]
pub fn keys(map: &mut Map) -> Array {
if map.is_empty() {
Array::new()
} else {
map.keys().cloned().map(Into::<Dynamic>::into).collect()
2020-08-22 16:26:49 +02:00
}
2021-10-20 10:22:12 +02:00
}
#[cfg(not(feature = "no_index"))]
#[rhai_fn(pure)]
pub fn values(map: &mut Map) -> Array {
if map.is_empty() {
Array::new()
} else {
map.values().cloned().collect()
2020-08-22 16:26:49 +02:00
}
2020-08-14 18:04:10 +02:00
}
}