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
//! Authly service utilities and helpers

use std::collections::HashMap;

use fnv::FnvHashSet;

use crate::id::ObjId;

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

#[derive(Default)]
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);
    }

    /// Translate the given property/attribute labels to underlying [ObjId]s.
    pub fn translate<'a>(
        &self,
        attributes: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> FnvHashSet<u128> {
        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.value());
        }

        output
    }
}