authly_common/
service.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Authly service utilities and helpers

use std::collections::{hash_map, HashMap};

use fnv::FnvHashSet;

use crate::id::{AnyId, ObjId};

/// A property mapping maps human-readable property and attribute labels to [ObjId]s.
#[derive(Clone, Default)]
pub struct PropertyMapping {
    properties: HashMap<String, AttributeMappings>,
}

/// Attribute mappings for a property.
#[derive(Clone, Default)]
pub struct AttributeMappings {
    attributes: HashMap<String, ObjId>,
}

impl PropertyMapping {
    /// Add an property/attribute/attribute-id triple to the mapping.
    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);
    }

    /// Get the object ID of a single property/attribute label pair, if found.
    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()
    }

    /// Translate the given property/attribute labels to underlying [ObjId]s.
    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()
    }
}