Skip to main content

security/
value.rs

1use std::collections::BTreeMap;
2
3use crate::error::{Result, SecurityError};
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum Value {
7    Null,
8    Bool(bool),
9    Integer(i64),
10    Unsigned(u64),
11    Real(f64),
12    String(String),
13    Data(Vec<u8>),
14    Date(f64),
15    Url(String),
16    Array(Vec<Self>),
17    Object(BTreeMap<String, Self>),
18}
19
20impl Value {
21    pub const fn is_array(&self) -> bool {
22        matches!(self, Self::Array(_))
23    }
24
25    pub const fn is_object(&self) -> bool {
26        matches!(self, Self::Object(_))
27    }
28
29    pub fn dictionary<const N: usize>(entries: [(&str, Self); N]) -> Self {
30        Self::Object(
31            entries
32                .into_iter()
33                .map(|(key, value)| (key.to_owned(), value))
34                .collect(),
35        )
36    }
37
38    pub fn get(&self, key: &str) -> Option<&Self> {
39        match self {
40            Self::Object(values) => values.get(key),
41            _ => None,
42        }
43    }
44
45    pub fn as_object(&self) -> Option<&BTreeMap<String, Self>> {
46        match self {
47            Self::Object(values) => Some(values),
48            _ => None,
49        }
50    }
51
52    pub fn as_array(&self) -> Option<&[Self]> {
53        match self {
54            Self::Array(values) => Some(values),
55            _ => None,
56        }
57    }
58
59    pub fn as_str(&self) -> Option<&str> {
60        match self {
61            Self::String(value) => Some(value),
62            _ => None,
63        }
64    }
65
66    pub const fn as_bool(&self) -> Option<bool> {
67        match self {
68            Self::Bool(value) => Some(*value),
69            _ => None,
70        }
71    }
72
73    pub fn as_i64(&self) -> Option<i64> {
74        match self {
75            Self::Integer(value) => Some(*value),
76            Self::Unsigned(value) => i64::try_from(*value).ok(),
77            _ => None,
78        }
79    }
80
81    pub fn as_u64(&self) -> Option<u64> {
82        match self {
83            Self::Unsigned(value) => Some(*value),
84            Self::Integer(value) => u64::try_from(*value).ok(),
85            _ => None,
86        }
87    }
88
89    #[allow(clippy::cast_precision_loss)]
90    pub fn as_f64(&self) -> Option<f64> {
91        match self {
92            Self::Real(value) => Some(*value),
93            Self::Integer(value) => Some(*value as f64),
94            Self::Unsigned(value) => Some(*value as f64),
95            _ => None,
96        }
97    }
98
99    pub fn as_data(&self) -> Option<&[u8]> {
100        match self {
101            Self::Data(value) => Some(value),
102            _ => None,
103        }
104    }
105
106    pub const fn as_date(&self) -> Option<f64> {
107        match self {
108            Self::Date(value) => Some(*value),
109            _ => None,
110        }
111    }
112
113    pub(crate) fn into_strings(self, operation: &'static str) -> Result<Vec<String>> {
114        let Self::Array(values) = self else {
115            return Err(SecurityError::UnexpectedType {
116                operation,
117                expected: "string array",
118            });
119        };
120        values
121            .into_iter()
122            .map(|value| match value {
123                Self::String(value) => Ok(value),
124                _ => Err(SecurityError::UnexpectedType {
125                    operation,
126                    expected: "string",
127                }),
128            })
129            .collect()
130    }
131}