use ahash::AHashMap;
use compact_str::CompactString;
use itertools::Itertools;
use rune::ContextError;
use rune::Module;
pub type PropertyKey = CompactString;
pub type PropertyValue = rune::runtime::Value;
#[derive(Debug, Default, rune::Any)]
#[rune(item = ::properties)]
pub(crate) struct Properties {
properties: AHashMap<PropertyKey, PropertyValue>,
}
impl Properties {
#[rune::function]
pub fn get(&self, name: &str) -> Option<PropertyValue> {
self.properties.get(name).cloned()
}
#[rune::function]
pub fn set(&mut self, name: &str, value: PropertyValue) {
self.properties.insert(name.into(), value);
}
#[rune::function]
pub fn has(&self, name: &str) -> bool {
self.properties.contains_key(name)
}
#[rune::function]
pub fn dump(&self) {
for (key, value) in self.properties.iter().sorted_by(|a, b| a.0.cmp(b.0)) {
println!("{key} = {value:?}");
}
}
}
#[rune::module(::properties)]
pub(crate) fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(module_meta)?;
m.ty::<Properties>()?;
m.function_meta(Properties::get)?;
m.function_meta(Properties::set)?;
m.function_meta(Properties::has)?;
m.function_meta(Properties::dump)?;
Ok(m)
}