one_err/
value.rs

1/// Value type for additional data fields on OneErr instances.
2#[derive(Debug, Clone, PartialEq)]
3pub enum Value {
4    /// Empty / No Data
5    Null,
6
7    /// Boolean Type
8    Bool(bool),
9
10    /// Signed Integer Type
11    I64(i64),
12
13    /// Unsigned Integer Type
14    U64(u64),
15
16    /// Floating Point Type
17    F64(f64),
18
19    /// String Type
20    String(Box<str>),
21}
22
23impl Value {
24    /// If this value is a bool type, return that bool.
25    pub fn as_bool(&self) -> Option<bool> {
26        self.into()
27    }
28
29    /// If this value is an i64 type, return that i64.
30    pub fn as_i64(&self) -> Option<i64> {
31        self.into()
32    }
33
34    /// If this value is a u64 type, return that u64.
35    pub fn as_u64(&self) -> Option<u64> {
36        self.into()
37    }
38
39    /// If this value is an f64 type, return that f64.
40    pub fn as_f64(&self) -> Option<f64> {
41        self.into()
42    }
43
44    /// If this value is a str type, return that str.
45    pub fn as_str(&self) -> Option<&str> {
46        self.into()
47    }
48}
49
50impl From<bool> for Value {
51    fn from(b: bool) -> Self {
52        Self::Bool(b)
53    }
54}
55
56impl From<i64> for Value {
57    fn from(i: i64) -> Self {
58        Self::I64(i)
59    }
60}
61
62impl From<u64> for Value {
63    fn from(u: u64) -> Self {
64        Self::U64(u)
65    }
66}
67
68impl From<f64> for Value {
69    fn from(f: f64) -> Self {
70        Self::F64(f)
71    }
72}
73
74impl From<&str> for Value {
75    fn from(s: &str) -> Self {
76        Self::String(s.into())
77    }
78}
79
80impl From<&String> for Value {
81    fn from(s: &String) -> Self {
82        Self::String(s.as_str().into())
83    }
84}
85
86impl From<String> for Value {
87    fn from(s: String) -> Self {
88        Self::String(s.into())
89    }
90}
91
92impl From<Box<str>> for Value {
93    fn from(s: Box<str>) -> Self {
94        Self::String(s)
95    }
96}
97
98impl<'lt> From<&'lt Value> for Option<bool> {
99    fn from(v: &'lt Value) -> Self {
100        match v {
101            Value::Bool(b) => Some(*b),
102            _ => None,
103        }
104    }
105}
106
107impl<'lt> From<&'lt Value> for Option<i64> {
108    fn from(v: &'lt Value) -> Self {
109        match v {
110            Value::I64(i) => Some(*i),
111            _ => None,
112        }
113    }
114}
115
116impl<'lt> From<&'lt Value> for Option<u64> {
117    fn from(v: &'lt Value) -> Self {
118        match v {
119            Value::U64(u) => Some(*u),
120            _ => None,
121        }
122    }
123}
124
125impl<'lt> From<&'lt Value> for Option<f64> {
126    fn from(v: &'lt Value) -> Self {
127        match v {
128            Value::F64(f) => Some(*f),
129            _ => None,
130        }
131    }
132}
133
134impl<'lt> From<&'lt Value> for Option<&'lt str> {
135    fn from(v: &'lt Value) -> Self {
136        match v {
137            Value::String(s) => Some(s),
138            _ => None,
139        }
140    }
141}
142
143impl serde::Serialize for Value {
144    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
145    where
146        S: serde::Serializer,
147    {
148        match self {
149            Self::Null => serializer.serialize_unit(),
150            Self::Bool(b) => serializer.serialize_bool(*b),
151            Self::I64(i) => serializer.serialize_i64(*i),
152            Self::U64(u) => serializer.serialize_u64(*u),
153            Self::F64(f) => serializer.serialize_f64(*f),
154            Self::String(s) => serializer.serialize_str(s),
155        }
156    }
157}
158
159impl<'de> serde::Deserialize<'de> for Value {
160    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
161    where
162        D: serde::Deserializer<'de>,
163    {
164        struct V;
165
166        impl<'de> serde::de::Visitor<'de> for V {
167            type Value = Value;
168
169            fn expecting(
170                &self,
171                formatter: &mut std::fmt::Formatter<'_>,
172            ) -> std::fmt::Result {
173                formatter.write_str("any valid JSON primitive (arrays / objects not yet supported)")
174            }
175
176            fn visit_bool<E>(self, value: bool) -> Result<Value, E> {
177                Ok(Value::Bool(value))
178            }
179
180            fn visit_i64<E>(self, value: i64) -> Result<Value, E> {
181                Ok(Value::I64(value))
182            }
183
184            fn visit_u64<E>(self, value: u64) -> Result<Value, E> {
185                Ok(Value::U64(value))
186            }
187
188            fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
189                Ok(Value::F64(value))
190            }
191
192            fn visit_str<E>(self, value: &str) -> Result<Value, E>
193            where
194                E: serde::de::Error,
195            {
196                self.visit_string(String::from(value))
197            }
198
199            fn visit_string<E>(self, value: String) -> Result<Value, E> {
200                Ok(Value::String(value.into()))
201            }
202
203            fn visit_none<E>(self) -> Result<Value, E> {
204                Ok(Value::Null)
205            }
206
207            fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error>
208            where
209                D: serde::Deserializer<'de>,
210            {
211                serde::Deserialize::deserialize(deserializer)
212            }
213
214            fn visit_unit<E>(self) -> Result<Value, E> {
215                Ok(Value::Null)
216            }
217        }
218
219        deserializer.deserialize_any(V)
220    }
221}