2020-05-06 13:45:17 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::any::Dynamic;
|
2020-04-21 17:25:12 +02:00
|
|
|
use crate::def_package;
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::engine::Map;
|
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};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-06-21 10:37:05 +02:00
|
|
|
use crate::stdlib::vec::Vec;
|
2020-04-24 06:39:24 +02:00
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
fn map_get_keys(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
2020-06-21 10:37:05 +02:00
|
|
|
Ok(map.iter().map(|(k, _)| k.clone().into()).collect())
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
|
|
|
Ok(map.iter().map(|(_, v)| v.clone()).collect())
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"has",
|
2020-05-25 07:44:28 +02:00
|
|
|
|map: &mut Map, prop: ImmutableString| Ok(map.contains_key(prop.as_str())),
|
2020-04-21 17:01:10 +02:00
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT));
|
|
|
|
lib.set_fn_1_mut("clear", |map: &mut Map| {
|
|
|
|
map.clear();
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
lib.set_fn_2_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"remove",
|
2020-05-25 07:44:28 +02:00
|
|
|
|x: &mut Map, name: ImmutableString| Ok(x.remove(name.as_str()).unwrap_or_else(|| ().into())),
|
2020-04-21 17:01:10 +02:00
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2_mut(
|
2020-04-21 17:01:10 +02:00
|
|
|
"mixin",
|
|
|
|
|map1: &mut Map, map2: Map| {
|
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
|
|
|
map1.insert(key, value);
|
|
|
|
});
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(())
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
2020-05-25 14:14:31 +02:00
|
|
|
lib.set_fn_2_mut(
|
|
|
|
"+=",
|
|
|
|
|map1: &mut Map, map2: Map| {
|
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
|
|
|
map1.insert(key, value);
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2(
|
2020-04-21 17:01:10 +02:00
|
|
|
"+",
|
|
|
|
|mut map1: Map, map2: Map| {
|
|
|
|
map2.into_iter().for_each(|(key, value)| {
|
|
|
|
map1.insert(key, value);
|
|
|
|
});
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(map1)
|
2020-04-21 17:01:10 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Register map access functions
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut("keys", map_get_keys);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_1_mut("values", map_get_values);
|
2020-04-21 17:01:10 +02:00
|
|
|
});
|