Skip to main content

utopia_span/
value.rs

1//! Attribute values.
2
3use php_std::Str;
4
5/// The value of a span attribute: a scalar or null.
6///
7/// Strings are bytes, as attribute values copied from requests can be any
8/// bytes.
9///
10/// PHP: `string|int|float|bool|null`.
11///
12/// ```
13/// use utopia_span::Value;
14///
15/// assert_eq!(Value::from("GET").as_str(), Some("GET"));
16/// assert_eq!(Value::from(200), Value::Int(200));
17/// assert_eq!(Value::from(None::<i64>), Value::Null);
18/// ```
19#[derive(Debug, Clone, PartialEq, Default)]
20pub enum Value {
21    /// No value. Reading an attribute that is not set returns it too.
22    #[default]
23    Null,
24    /// A boolean.
25    Bool(bool),
26    /// An integer.
27    Int(i64),
28    /// A float.
29    Float(f64),
30    /// A string, as bytes.
31    Str(Str),
32}
33
34impl Value {
35    /// The string, when the value is a UTF-8 string.
36    pub fn as_str(&self) -> Option<&str> {
37        match self {
38            Value::Str(s) => s.as_str(),
39            _ => None,
40        }
41    }
42
43    /// The bytes, when the value is a string.
44    pub fn as_bytes(&self) -> Option<&[u8]> {
45        match self {
46            Value::Str(s) => Some(s.as_bytes()),
47            _ => None,
48        }
49    }
50
51    /// The float, when the value is a float.
52    pub fn as_float(&self) -> Option<f64> {
53        match self {
54            Value::Float(f) => Some(*f),
55            _ => None,
56        }
57    }
58
59    /// The integer, when the value is an integer.
60    pub fn as_int(&self) -> Option<i64> {
61        match self {
62            Value::Int(i) => Some(*i),
63            _ => None,
64        }
65    }
66
67    /// The boolean, when the value is a boolean.
68    pub fn as_bool(&self) -> Option<bool> {
69        match self {
70            Value::Bool(b) => Some(*b),
71            _ => None,
72        }
73    }
74
75    /// Whether the value is [`Value::Null`].
76    pub fn is_null(&self) -> bool {
77        matches!(self, Value::Null)
78    }
79
80    /// The value as PHP prints it: `(string) $value` (`true` is `1`, `false`
81    /// and `null` are empty, floats at the `precision` setting).
82    pub fn to_php_string(&self) -> Vec<u8> {
83        match self {
84            Value::Null | Value::Bool(false) => Vec::new(),
85            Value::Bool(true) => b"1".to_vec(),
86            Value::Int(i) => i.to_string().into_bytes(),
87            Value::Float(f) => php_std::number::to_string(*f).into_bytes(),
88            Value::Str(s) => s.as_bytes().to_vec(),
89        }
90    }
91
92    /// The value as a float: `(float) $value`.
93    pub fn to_number(&self) -> f64 {
94        self.to_php().to_double()
95    }
96
97    /// The value in PHP's value model.
98    pub fn to_php(&self) -> php_std::Value {
99        match self {
100            Value::Null => php_std::Value::Null,
101            Value::Bool(b) => php_std::Value::Bool(*b),
102            Value::Int(i) => php_std::Value::Int(*i),
103            Value::Float(f) => php_std::Value::Float(*f),
104            Value::Str(s) => php_std::Value::Str(s.clone()),
105        }
106    }
107}
108
109impl From<bool> for Value {
110    fn from(b: bool) -> Self {
111        Value::Bool(b)
112    }
113}
114
115macro_rules! from_int {
116    ($($t:ty),*) => {$(
117        impl From<$t> for Value {
118            fn from(i: $t) -> Self {
119                Value::Int(i64::from(i))
120            }
121        }
122    )*};
123}
124from_int!(i8, i16, i32, i64, u8, u16, u32);
125
126impl From<f64> for Value {
127    fn from(f: f64) -> Self {
128        Value::Float(f)
129    }
130}
131
132impl From<f32> for Value {
133    fn from(f: f32) -> Self {
134        Value::Float(f64::from(f))
135    }
136}
137
138impl From<&str> for Value {
139    fn from(s: &str) -> Self {
140        Value::Str(Str::from(s))
141    }
142}
143
144impl From<String> for Value {
145    fn from(s: String) -> Self {
146        Value::Str(Str::from(s))
147    }
148}
149
150impl From<&[u8]> for Value {
151    fn from(s: &[u8]) -> Self {
152        Value::Str(Str::from(s))
153    }
154}
155
156impl From<Vec<u8>> for Value {
157    fn from(s: Vec<u8>) -> Self {
158        Value::Str(Str::from(s))
159    }
160}
161
162impl From<Str> for Value {
163    fn from(s: Str) -> Self {
164        Value::Str(s)
165    }
166}
167
168impl<T: Into<Value>> From<Option<T>> for Value {
169    fn from(v: Option<T>) -> Self {
170        v.map_or(Value::Null, Into::into)
171    }
172}