1use std::borrow::Cow;
2use std::sync::Arc;
3
4use imbl::Vector;
5use yasi::InternedString;
6
7use crate::{InOMap, Value};
8
9impl From<serde_json::Value> for Value {
10 fn from(value: serde_json::Value) -> Self {
11 match value {
12 serde_json::Value::Array(a) => Self::Array(a.into_iter().map(|a| a.into()).collect()),
13 serde_json::Value::Bool(a) => Self::Bool(a),
14 serde_json::Value::Null => Self::Null,
15 serde_json::Value::Number(a) => Self::Number(a),
16 serde_json::Value::Object(a) => {
17 Self::Object(a.into_iter().map(|(a, b)| (a.into(), b.into())).collect())
18 }
19 serde_json::Value::String(a) => Self::String(a.into()),
20 }
21 }
22}
23
24impl From<Value> for serde_json::Value {
25 fn from(value: Value) -> Self {
26 match value {
27 Value::Array(a) => Self::Array(a.into_iter().map(|a| a.into()).collect()),
28 Value::Bool(a) => Self::Bool(a),
29 Value::Null => Self::Null,
30 Value::Number(a) => Self::Number(a),
31 Value::Object(a) => Self::Object(
32 a.into_iter()
33 .map(|(a, b)| ((&*a).to_owned(), b.into()))
34 .collect(),
35 ),
36 Value::String(a) => Self::String((&*a).to_owned()),
37 }
38 }
39}
40
41impl<T: Clone + Into<Value>> From<&[T]> for Value {
42 fn from(value: &[T]) -> Self {
44 Self::Array(value.into_iter().map(|t| t.clone().into()).collect())
45 }
46}
47
48impl From<&str> for Value {
49 fn from(value: &str) -> Self {
51 Value::String(Arc::new(value.into()))
52 }
53}
54
55impl<T: Into<Value>, const N: usize> From<[T; N]> for Value {
56 fn from(value: [T; N]) -> Self {
58 Self::Array(value.into_iter().map(|t| t.into()).collect())
59 }
60}
61
62impl From<()> for Value {
63 fn from(_: ()) -> Self {
65 Value::Null
66 }
67}
68
69impl<'a> From<Cow<'a, str>> for Value {
70 fn from(value: Cow<'a, str>) -> Self {
72 Value::String(Arc::new(value.into_owned()))
73 }
74}
75
76impl From<serde_json::Map<String, serde_json::Value>> for InOMap<InternedString, Value> {
77 fn from(value: serde_json::Map<String, serde_json::Value>) -> Self {
78 value
79 .into_iter()
80 .map(|(k, v)| (InternedString::intern(k), v.into()))
81 .collect()
82 }
83}
84
85impl From<InOMap<InternedString, Value>> for Value {
86 fn from(value: InOMap<InternedString, Value>) -> Self {
87 Self::Object(value)
88 }
89}
90
91impl From<serde_json::Map<String, serde_json::Value>> for Value {
92 fn from(value: serde_json::Map<String, serde_json::Value>) -> Self {
93 Self::Object(value.into())
94 }
95}
96
97impl From<serde_json::Number> for Value {
98 fn from(value: serde_json::Number) -> Self {
100 Self::Number(value)
101 }
102}
103
104impl<T: Into<Value>> From<Option<T>> for Value {
105 fn from(value: Option<T>) -> Self {
107 match value {
108 None => Value::Null,
109 Some(a) => a.into(),
110 }
111 }
112}
113
114impl From<String> for Value {
115 fn from(value: String) -> Self {
117 Value::String(Arc::new(value))
118 }
119}
120
121impl From<Arc<String>> for Value {
122 fn from(value: Arc<String>) -> Self {
124 Value::String(value)
125 }
126}
127
128impl<T: Into<Value>> From<Vec<T>> for Value {
129 fn from(value: Vec<T>) -> Self {
131 Value::Array(value.into_iter().map(|v| v.into()).collect())
132 }
133}
134
135impl<T: Clone + Into<Value>> From<Vector<T>> for Value {
136 fn from(value: Vector<T>) -> Self {
138 Value::Array(value.into_iter().map(|v| v.into()).collect())
139 }
140}
141
142impl From<bool> for Value {
143 fn from(value: bool) -> Self {
145 Value::Bool(value)
146 }
147}
148
149impl From<f32> for Value {
150 fn from(value: f32) -> Self {
152 serde_json::Number::from_f64(value as f64).into()
153 }
154}
155impl From<f64> for Value {
156 fn from(value: f64) -> Self {
158 serde_json::Number::from_f64(value).into()
159 }
160}
161impl From<i8> for Value {
162 fn from(value: i8) -> Self {
163 Self::Number(value.into())
164 }
165}
166impl From<i16> for Value {
167 fn from(value: i16) -> Self {
168 Self::Number(value.into())
169 }
170}
171impl From<i32> for Value {
172 fn from(value: i32) -> Self {
173 Self::Number(value.into())
174 }
175}
176impl From<i64> for Value {
177 fn from(value: i64) -> Self {
178 Self::Number(value.into())
179 }
180}
181impl From<isize> for Value {
182 fn from(value: isize) -> Self {
183 Self::Number(value.into())
184 }
185}
186impl From<u8> for Value {
187 fn from(value: u8) -> Self {
188 Self::Number(value.into())
189 }
190}
191impl From<u16> for Value {
192 fn from(value: u16) -> Self {
193 Self::Number(value.into())
194 }
195}
196impl From<u32> for Value {
197 fn from(value: u32) -> Self {
198 Self::Number(value.into())
199 }
200}
201impl From<u64> for Value {
202 fn from(value: u64) -> Self {
203 Self::Number(value.into())
204 }
205}
206impl From<usize> for Value {
207 fn from(value: usize) -> Self {
208 Self::Number(value.into())
209 }
210}