Skip to main content

saml_rs/model/
attributes.rs

1/// A single SAML attribute value.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct AttributeValue(String);
4
5impl AttributeValue {
6    /// Wrap an attribute value.
7    pub fn new(value: impl Into<String>) -> Self {
8        Self(value.into())
9    }
10
11    /// Borrow the attribute value.
12    pub fn as_str(&self) -> &str {
13        &self.0
14    }
15}
16
17/// SAML attribute with one or more values.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Attribute {
20    name: String,
21    name_format: Option<String>,
22    values: Vec<AttributeValue>,
23}
24
25impl Attribute {
26    /// Create a SAML attribute.
27    pub fn new(
28        name: impl Into<String>,
29        name_format: Option<String>,
30        values: Vec<AttributeValue>,
31    ) -> Self {
32        Self {
33            name: name.into(),
34            name_format,
35            values,
36        }
37    }
38
39    /// Attribute name.
40    pub fn name(&self) -> &str {
41        &self.name
42    }
43
44    /// Attribute name format, when extracted.
45    pub fn name_format(&self) -> Option<&str> {
46        self.name_format.as_deref()
47    }
48
49    /// Attribute values.
50    pub fn values(&self) -> &[AttributeValue] {
51        &self.values
52    }
53}
54
55/// Collection of SAML attributes.
56#[derive(Debug, Clone, Default, PartialEq, Eq)]
57pub struct Attributes(Vec<Attribute>);
58
59impl Attributes {
60    /// Create an attribute collection.
61    pub fn new(values: Vec<Attribute>) -> Self {
62        Self(values)
63    }
64
65    /// Borrow the attributes as a slice.
66    pub fn as_slice(&self) -> &[Attribute] {
67        &self.0
68    }
69
70    /// Find an attribute by name.
71    pub fn get(&self, name: &str) -> Option<&Attribute> {
72        self.0.iter().find(|attribute| attribute.name() == name)
73    }
74}
75
76impl IntoIterator for Attributes {
77    type Item = Attribute;
78    type IntoIter = std::vec::IntoIter<Attribute>;
79
80    fn into_iter(self) -> Self::IntoIter {
81        self.0.into_iter()
82    }
83}