rusty_value/
value_trait.rs

1use std::{collections::HashMap, ffi::OsString, path::PathBuf};
2
3use crate::{Fields, Float, HashablePrimitive, HashableValue, Primitive, Struct, Value};
4
5pub trait RustyValue {
6    fn into_rusty_value(self) -> Value;
7}
8
9pub trait HashableRustyValue {
10    fn into_hashable_rusty_value(self) -> HashableValue;
11}
12
13impl HashableRustyValue for usize {
14    #[inline]
15    fn into_hashable_rusty_value(self) -> HashableValue {
16        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::USize(self)))
17    }
18}
19
20impl HashableRustyValue for isize {
21    #[inline]
22    fn into_hashable_rusty_value(self) -> HashableValue {
23        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::ISize(self)))
24    }
25}
26
27impl HashableRustyValue for u8 {
28    #[inline]
29    fn into_hashable_rusty_value(self) -> HashableValue {
30        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U8(self)))
31    }
32}
33
34impl HashableRustyValue for i8 {
35    #[inline]
36    fn into_hashable_rusty_value(self) -> HashableValue {
37        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I8(self)))
38    }
39}
40
41impl HashableRustyValue for u16 {
42    #[inline]
43    fn into_hashable_rusty_value(self) -> HashableValue {
44        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U16(self)))
45    }
46}
47
48impl HashableRustyValue for i16 {
49    #[inline]
50    fn into_hashable_rusty_value(self) -> HashableValue {
51        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I16(self)))
52    }
53}
54
55impl HashableRustyValue for u32 {
56    #[inline]
57    fn into_hashable_rusty_value(self) -> HashableValue {
58        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U32(self)))
59    }
60}
61
62impl HashableRustyValue for i32 {
63    #[inline]
64    fn into_hashable_rusty_value(self) -> HashableValue {
65        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I32(self)))
66    }
67}
68
69impl HashableRustyValue for u64 {
70    #[inline]
71    fn into_hashable_rusty_value(self) -> HashableValue {
72        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U64(self)))
73    }
74}
75
76impl HashableRustyValue for i64 {
77    #[inline]
78    fn into_hashable_rusty_value(self) -> HashableValue {
79        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I64(self)))
80    }
81}
82
83impl HashableRustyValue for u128 {
84    #[inline]
85    fn into_hashable_rusty_value(self) -> HashableValue {
86        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U128(self)))
87    }
88}
89
90impl HashableRustyValue for i128 {
91    #[inline]
92    fn into_hashable_rusty_value(self) -> HashableValue {
93        HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I128(self)))
94    }
95}
96
97impl HashableRustyValue for String {
98    #[inline]
99    fn into_hashable_rusty_value(self) -> HashableValue {
100        HashableValue::Primitive(HashablePrimitive::String(self))
101    }
102}
103
104impl<'a> HashableRustyValue for &'a str {
105    fn into_hashable_rusty_value(self) -> HashableValue {
106        self.to_string().into_hashable_rusty_value()
107    }
108}
109
110impl HashableRustyValue for bool {
111    #[inline]
112    fn into_hashable_rusty_value(self) -> HashableValue {
113        HashableValue::Primitive(HashablePrimitive::Bool(self))
114    }
115}
116
117impl HashableRustyValue for () {
118    fn into_hashable_rusty_value(self) -> HashableValue {
119        HashableValue::None
120    }
121}
122
123impl RustyValue for HashableValue {
124    fn into_rusty_value(self) -> Value {
125        match self {
126            HashableValue::Primitive(p) => match p {
127                HashablePrimitive::Integer(i) => Value::Primitive(Primitive::Integer(i)),
128                HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)),
129                HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)),
130                HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)),
131                HashablePrimitive::OsString(o) => Value::Primitive(Primitive::OsString(o)),
132            },
133            HashableValue::List(l) => {
134                Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect())
135            }
136            HashableValue::None => Value::None,
137        }
138    }
139}
140
141impl<H: HashableRustyValue> RustyValue for H {
142    #[inline]
143    fn into_rusty_value(self) -> Value {
144        self.into_hashable_rusty_value().into_rusty_value()
145    }
146}
147
148impl RustyValue for f32 {
149    #[inline]
150    fn into_rusty_value(self) -> Value {
151        Value::Primitive(Primitive::Float(Float::F32(self)))
152    }
153}
154
155impl RustyValue for f64 {
156    #[inline]
157    fn into_rusty_value(self) -> Value {
158        Value::Primitive(Primitive::Float(Float::F64(self)))
159    }
160}
161
162impl HashableRustyValue for OsString {
163    #[inline]
164    fn into_hashable_rusty_value(self) -> HashableValue {
165        HashableValue::Primitive(HashablePrimitive::OsString(self))
166    }
167}
168
169impl RustyValue for PathBuf {
170    #[inline]
171    fn into_rusty_value(self) -> Value {
172        Value::Struct(Struct {
173            name: String::from("PathBuf"),
174            fields: Fields::Unnamed(vec![self.into_os_string().into_rusty_value()]),
175        })
176    }
177}
178
179impl<T: RustyValue> RustyValue for Option<T> {
180    #[inline]
181    fn into_rusty_value(self) -> Value {
182        match self {
183            Some(val) => val.into_rusty_value(),
184            None => Value::None,
185        }
186    }
187}
188
189impl<R: RustyValue> RustyValue for Vec<R> {
190    fn into_rusty_value(self) -> Value {
191        let value_vec = self
192            .into_iter()
193            .map(|v| v.into_rusty_value())
194            .collect::<Vec<_>>();
195
196        Value::List(value_vec)
197    }
198}
199
200impl<R: RustyValue, H: HashableRustyValue> RustyValue for HashMap<H, R> {
201    fn into_rusty_value(self) -> Value {
202        let map = self
203            .into_iter()
204            .map(|(k, v)| (k.into_hashable_rusty_value(), v.into_rusty_value()))
205            .collect::<HashMap<_, _>>();
206
207        Value::Map(map)
208    }
209}