use std::collections::{hash_map, HashMap};
use fnv::FnvHashSet;
use crate::id::{AnyId, ObjId};
#[derive(Clone, Default)]
pub struct PropertyMapping {
properties: HashMap<String, AttributeMappings>,
}
#[derive(Clone, Default)]
pub struct AttributeMappings {
attributes: HashMap<String, ObjId>,
}
impl PropertyMapping {
pub fn add(&mut self, property_label: String, attribute_label: String, attribute_id: ObjId) {
self.properties
.entry(property_label)
.or_default()
.attributes
.insert(attribute_label, attribute_id);
}
pub fn attribute_object_id(
&self,
property_label: &str,
attribute_label: &str,
) -> Option<ObjId> {
let attribute_mapping = self.properties.get(property_label)?;
attribute_mapping.attributes.get(attribute_label).cloned()
}
pub fn translate<'a>(
&self,
attributes: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> FnvHashSet<AnyId> {
let mut output = FnvHashSet::default();
for (prop, attr) in attributes {
let Some(attr_mappings) = self.properties.get(prop) else {
continue;
};
let Some(attr_id) = attr_mappings.attributes.get(attr) else {
continue;
};
output.insert(attr_id.to_any());
}
output
}
}
impl IntoIterator for PropertyMapping {
type IntoIter = hash_map::IntoIter<String, AttributeMappings>;
type Item = (String, AttributeMappings);
fn into_iter(self) -> Self::IntoIter {
self.properties.into_iter()
}
}
impl<'a> IntoIterator for &'a PropertyMapping {
type IntoIter = hash_map::Iter<'a, String, AttributeMappings>;
type Item = (&'a String, &'a AttributeMappings);
fn into_iter(self) -> Self::IntoIter {
self.properties.iter()
}
}
impl IntoIterator for AttributeMappings {
type IntoIter = hash_map::IntoIter<String, ObjId>;
type Item = (String, ObjId);
fn into_iter(self) -> Self::IntoIter {
self.attributes.into_iter()
}
}
impl<'a> IntoIterator for &'a AttributeMappings {
type IntoIter = hash_map::Iter<'a, String, ObjId>;
type Item = (&'a String, &'a ObjId);
fn into_iter(self) -> Self::IntoIter {
self.attributes.iter()
}
}