Skip to main content

themis/
attr.rs

1use std::cmp::Ordering;
2use std::collections::BTreeMap;
3
4use serde::{Deserialize, Serialize};
5
6/// Attribute value used by ABAC conditions.
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum Value {
10    Null,
11    Bool(bool),
12    Int(i64),
13    Float(f64),
14    Str(String),
15    List(Vec<Value>),
16}
17
18/// Named attributes of a subject, resource or environment.
19pub type Attributes = BTreeMap<String, Value>;
20
21impl Value {
22    /// Ordering between comparable values. Integers and floats compare with each other; other mixed
23    /// types have no ordering.
24    pub fn compare(&self, other: &Value) -> Option<Ordering> {
25        match (self, other) {
26            (Value::Int(a), Value::Int(b)) => Some(a.cmp(b)),
27            (Value::Float(a), Value::Float(b)) => a.partial_cmp(b),
28            (Value::Int(a), Value::Float(b)) => (*a as f64).partial_cmp(b),
29            (Value::Float(a), Value::Int(b)) => a.partial_cmp(&(*b as f64)),
30            (Value::Str(a), Value::Str(b)) => Some(a.cmp(b)),
31            (Value::Bool(a), Value::Bool(b)) => Some(a.cmp(b)),
32            _ => None,
33        }
34    }
35
36    /// Equality that treats `Int(1)` and `Float(1.0)` as equal.
37    pub fn loosely_eq(&self, other: &Value) -> bool {
38        match self.compare(other) {
39            Some(ordering) => ordering == Ordering::Equal,
40            None => self == other,
41        }
42    }
43
44    pub fn as_str(&self) -> Option<&str> {
45        match self {
46            Value::Str(s) => Some(s),
47            _ => None,
48        }
49    }
50}
51
52impl From<bool> for Value {
53    fn from(v: bool) -> Self {
54        Value::Bool(v)
55    }
56}
57
58impl From<i64> for Value {
59    fn from(v: i64) -> Self {
60        Value::Int(v)
61    }
62}
63
64impl From<i32> for Value {
65    fn from(v: i32) -> Self {
66        Value::Int(v.into())
67    }
68}
69
70impl From<u32> for Value {
71    fn from(v: u32) -> Self {
72        Value::Int(v.into())
73    }
74}
75
76impl From<f64> for Value {
77    fn from(v: f64) -> Self {
78        Value::Float(v)
79    }
80}
81
82impl From<&str> for Value {
83    fn from(v: &str) -> Self {
84        Value::Str(v.to_string())
85    }
86}
87
88impl From<String> for Value {
89    fn from(v: String) -> Self {
90        Value::Str(v)
91    }
92}
93
94impl<T: Into<Value>> From<Vec<T>> for Value {
95    fn from(v: Vec<T>) -> Self {
96        Value::List(v.into_iter().map(Into::into).collect())
97    }
98}
99
100impl<T: Into<Value>> From<Option<T>> for Value {
101    fn from(v: Option<T>) -> Self {
102        v.map_or(Value::Null, Into::into)
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn int_and_float_compare() {
112        assert!(Value::Int(1).loosely_eq(&Value::Float(1.0)));
113        assert_eq!(Value::Int(2).compare(&Value::Float(1.5)), Some(Ordering::Greater));
114    }
115
116    #[test]
117    fn different_types_have_no_order() {
118        assert_eq!(Value::Int(1).compare(&Value::from("1")), None);
119        assert!(!Value::Int(1).loosely_eq(&Value::from("1")));
120    }
121
122    #[test]
123    fn json_round_trip() {
124        let v: Value = serde_json::from_str(r#"[1, 2.5, "a", true, null]"#).unwrap();
125        assert_eq!(
126            v,
127            Value::List(vec![
128                Value::Int(1),
129                Value::Float(2.5),
130                Value::from("a"),
131                Value::Bool(true),
132                Value::Null
133            ])
134        );
135    }
136}