use std::collections::{hash_map, HashMap};
use fnv::FnvHashSet;
use crate::id::AttrId;
#[derive(Clone, Default)]
pub struct NamespacePropertyMapping {
namespaces: HashMap<String, PropertyMappings>,
}
#[derive(Clone, Default)]
pub struct PropertyMappings {
properties: HashMap<String, AttributeMappings>,
}
#[derive(Clone, Default)]
pub struct AttributeMappings {
attributes: HashMap<String, AttrId>,
}
impl NamespacePropertyMapping {
pub fn namespace_mut(&mut self, namespace_label: String) -> &mut PropertyMappings {
self.namespaces.entry(namespace_label).or_default()
}
pub fn attribute_object_id(
&self,
namespace_label: &str,
property_label: &str,
attribute_label: &str,
) -> Option<AttrId> {
self.namespaces
.get(namespace_label)?
.properties
.get(property_label)?
.attributes
.get(attribute_label)
.cloned()
}
pub fn translate<'a>(
&self,
attributes: impl IntoIterator<Item = (&'a str, &'a str, &'a str)>,
) -> FnvHashSet<AttrId> {
let mut output = FnvHashSet::default();
for (namespace, prop, attr) in attributes {
let Some(prop_mappings) = self.namespaces.get(namespace) else {
continue;
};
let Some(attr_mappings) = prop_mappings.properties.get(prop) else {
continue;
};
let Some(attr_id) = attr_mappings.attributes.get(attr) else {
continue;
};
output.insert(*attr_id);
}
output
}
}
impl PropertyMappings {
pub fn property_mut(&mut self, property_label: String) -> &mut AttributeMappings {
self.properties.entry(property_label).or_default()
}
}
impl AttributeMappings {
pub fn put(&mut self, attribute_label: String, attribute_id: AttrId) {
self.attributes
.entry(attribute_label)
.insert_entry(attribute_id);
}
}
impl IntoIterator for NamespacePropertyMapping {
type IntoIter = hash_map::IntoIter<String, PropertyMappings>;
type Item = (String, PropertyMappings);
fn into_iter(self) -> Self::IntoIter {
self.namespaces.into_iter()
}
}
impl<'a> IntoIterator for &'a NamespacePropertyMapping {
type IntoIter = hash_map::Iter<'a, String, PropertyMappings>;
type Item = (&'a String, &'a PropertyMappings);
fn into_iter(self) -> Self::IntoIter {
self.namespaces.iter()
}
}
impl IntoIterator for PropertyMappings {
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 PropertyMappings {
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, AttrId>;
type Item = (String, AttrId);
fn into_iter(self) -> Self::IntoIter {
self.attributes.into_iter()
}
}
impl<'a> IntoIterator for &'a AttributeMappings {
type IntoIter = hash_map::Iter<'a, String, AttrId>;
type Item = (&'a String, &'a AttrId);
fn into_iter(self) -> Self::IntoIter {
self.attributes.iter()
}
}