use std::{collections::HashMap, hash::Hash, sync::Arc};
use serde_json::Value;
use crate::core::item::AnyItem;
pub fn downcast_item<T: Clone + 'static>(item: &Arc<dyn AnyItem>) -> Option<T> {
item.as_any().downcast_ref::<T>().cloned()
}
#[must_use]
pub fn mask_filter(filter: &Value, candidate: &Value) -> bool {
match (filter, candidate) {
(Value::Object(map_a), Value::Object(map_b)) => {
for (key, value_a) in map_a {
match map_b.get(key) {
Some(value_b) => {
if !mask_filter(value_a, value_b) {
return false;
}
}
None => return false,
}
}
true
}
_ => filter == candidate,
}
}
pub fn assert_default_for_key<K, V, S>(hash_map: &mut HashMap<K, V, S>, key: &K)
where
K: Hash + Eq + Clone,
V: Default,
S: std::hash::BuildHasher,
{
if !hash_map.contains_key(key) {
hash_map.insert(key.clone(), V::default());
}
}