/rust/registry/src/index.crates.io-1949cf8c6b5b557f/serde_dhall-0.13.0/src/serialize.rs
Line | Count | Source |
1 | | use serde::ser; |
2 | | use std::collections::BTreeMap; |
3 | | |
4 | | use dhall::syntax::NumKind; |
5 | | |
6 | | use crate::value::SimpleValue; |
7 | | use crate::{Error, ErrorKind, Result, SimpleType, Value}; |
8 | | use SimpleValue::*; |
9 | | |
10 | | pub trait Sealed {} |
11 | | |
12 | | /// A data structure that can be serialized from a Dhall expression. |
13 | | /// |
14 | | /// This is automatically implemented for any type that [serde] can serialize. |
15 | | /// In fact, this trait cannot be implemented manually. To implement it for your type, |
16 | | /// use serde's derive mechanism. |
17 | | /// |
18 | | /// # Example |
19 | | /// |
20 | | /// ```rust |
21 | | /// # fn main() -> serde_dhall::Result<()> { |
22 | | /// use serde::Serialize; |
23 | | /// |
24 | | /// // Use serde's derive |
25 | | /// #[derive(Serialize)] |
26 | | /// struct Point { |
27 | | /// x: u64, |
28 | | /// y: u64, |
29 | | /// } |
30 | | /// |
31 | | /// // Convert a Point to a Dhall string. |
32 | | /// let point = Point { x: 0, y: 0 }; |
33 | | /// let point_str = serde_dhall::serialize(&point).to_string()?; |
34 | | /// assert_eq!(point_str, "{ x = 0, y = 0 }".to_string()); |
35 | | /// # Ok(()) |
36 | | /// # } |
37 | | /// ``` |
38 | | /// |
39 | | /// [serde]: https://serde.rs |
40 | | pub trait ToDhall: Sealed { |
41 | | #[doc(hidden)] |
42 | | fn to_dhall(&self, ty: Option<&SimpleType>) -> Result<Value>; |
43 | | } |
44 | | |
45 | | impl<T> Sealed for T where T: ser::Serialize {} |
46 | | |
47 | | impl<T> ToDhall for T |
48 | | where |
49 | | T: ser::Serialize, |
50 | | { |
51 | 0 | fn to_dhall(&self, ty: Option<&SimpleType>) -> Result<Value> { |
52 | 0 | let sval: SimpleValue = self.serialize(Serializer)?; |
53 | 0 | sval.into_value(ty) |
54 | 0 | } Unexecuted instantiation: <&anise::almanac::metaload::metaalmanac::MetaAlmanac as serde_dhall::serialize::ToDhall>::to_dhall Unexecuted instantiation: <&anise::structure::dataset::location_dhall::LocationDhallSet as serde_dhall::serialize::ToDhall>::to_dhall Unexecuted instantiation: <&anise::structure::location::Location as serde_dhall::serialize::ToDhall>::to_dhall Unexecuted instantiation: <_ as serde_dhall::serialize::ToDhall>::to_dhall |
55 | | } |
56 | | |
57 | | #[derive(Default, Clone, Copy)] |
58 | | struct Serializer; |
59 | | |
60 | | impl ser::Serializer for Serializer { |
61 | | type Ok = SimpleValue; |
62 | | type Error = Error; |
63 | | |
64 | | type SerializeSeq = SeqSerializer; |
65 | | type SerializeTuple = TupleSerializer; |
66 | | type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>; |
67 | | type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>; |
68 | | type SerializeMap = MapSerializer; |
69 | | type SerializeStruct = StructSerializer; |
70 | | type SerializeStructVariant = StructVariantSerializer; |
71 | | |
72 | 0 | fn serialize_bool(self, v: bool) -> Result<Self::Ok> { |
73 | 0 | Ok(Num(NumKind::Bool(v))) |
74 | 0 | } |
75 | | |
76 | 0 | fn serialize_i8(self, v: i8) -> Result<Self::Ok> { |
77 | 0 | self.serialize_i64(i64::from(v)) |
78 | 0 | } |
79 | 0 | fn serialize_i16(self, v: i16) -> Result<Self::Ok> { |
80 | 0 | self.serialize_i64(i64::from(v)) |
81 | 0 | } |
82 | 0 | fn serialize_i32(self, v: i32) -> Result<Self::Ok> { |
83 | 0 | self.serialize_i64(i64::from(v)) |
84 | 0 | } |
85 | 0 | fn serialize_i64(self, v: i64) -> Result<Self::Ok> { |
86 | 0 | Ok(Num(NumKind::Integer(v))) |
87 | 0 | } |
88 | | |
89 | 0 | fn serialize_u8(self, v: u8) -> Result<Self::Ok> { |
90 | 0 | self.serialize_u64(u64::from(v)) |
91 | 0 | } |
92 | 0 | fn serialize_u16(self, v: u16) -> Result<Self::Ok> { |
93 | 0 | self.serialize_u64(u64::from(v)) |
94 | 0 | } |
95 | 0 | fn serialize_u32(self, v: u32) -> Result<Self::Ok> { |
96 | 0 | self.serialize_u64(u64::from(v)) |
97 | 0 | } |
98 | 0 | fn serialize_u64(self, v: u64) -> Result<Self::Ok> { |
99 | 0 | Ok(Num(NumKind::Natural(v))) |
100 | 0 | } |
101 | | |
102 | 0 | fn serialize_f32(self, v: f32) -> Result<Self::Ok> { |
103 | 0 | self.serialize_f64(f64::from(v)) |
104 | 0 | } |
105 | 0 | fn serialize_f64(self, v: f64) -> Result<Self::Ok> { |
106 | 0 | Ok(Num(NumKind::Double(v.into()))) |
107 | 0 | } |
108 | | |
109 | 0 | fn serialize_char(self, v: char) -> Result<Self::Ok> { |
110 | 0 | self.serialize_str(&v.to_string()) |
111 | 0 | } |
112 | 0 | fn serialize_str(self, v: &str) -> Result<Self::Ok> { |
113 | 0 | Ok(Text(v.to_owned())) |
114 | 0 | } |
115 | | |
116 | 0 | fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok> { |
117 | 0 | Err(ErrorKind::Serialize( |
118 | 0 | "Unsupported data for serialization: byte array".to_owned(), |
119 | 0 | ) |
120 | 0 | .into()) |
121 | 0 | } |
122 | | |
123 | 0 | fn serialize_none(self) -> Result<Self::Ok> { |
124 | 0 | Ok(Optional(None)) |
125 | 0 | } |
126 | 0 | fn serialize_some<T>(self, v: &T) -> Result<Self::Ok> |
127 | 0 | where |
128 | 0 | T: ?Sized + ser::Serialize, |
129 | | { |
130 | 0 | Ok(Optional(Some(Box::new(v.serialize(self)?)))) |
131 | 0 | } Unexecuted instantiation: <serde_dhall::serialize::Serializer as serde_core::ser::Serializer>::serialize_some::<alloc::string::String> Unexecuted instantiation: <serde_dhall::serialize::Serializer as serde_core::ser::Serializer>::serialize_some::<i32> Unexecuted instantiation: <serde_dhall::serialize::Serializer as serde_core::ser::Serializer>::serialize_some::<u32> Unexecuted instantiation: <serde_dhall::serialize::Serializer as serde_core::ser::Serializer>::serialize_some::<_> |
132 | | |
133 | 0 | fn serialize_unit(self) -> Result<Self::Ok> { |
134 | 0 | Ok(Record(Default::default())) |
135 | 0 | } |
136 | | |
137 | 0 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> { |
138 | 0 | self.serialize_unit() |
139 | 0 | } |
140 | 0 | fn serialize_newtype_struct<T>( |
141 | 0 | self, |
142 | 0 | _name: &'static str, |
143 | 0 | _value: &T, |
144 | 0 | ) -> Result<Self::Ok> |
145 | 0 | where |
146 | 0 | T: ?Sized + ser::Serialize, |
147 | | { |
148 | 0 | Err(ErrorKind::Serialize( |
149 | 0 | "Unsupported data for serialization: newtype struct".to_owned(), |
150 | 0 | ) |
151 | 0 | .into()) |
152 | 0 | } |
153 | 0 | fn serialize_struct( |
154 | 0 | self, |
155 | 0 | _name: &'static str, |
156 | 0 | _len: usize, |
157 | 0 | ) -> Result<Self::SerializeStruct> { |
158 | 0 | Ok(StructSerializer::default()) |
159 | 0 | } |
160 | | |
161 | 0 | fn serialize_unit_variant( |
162 | 0 | self, |
163 | 0 | _name: &'static str, |
164 | 0 | _variant_index: u32, |
165 | 0 | variant: &'static str, |
166 | 0 | ) -> Result<Self::Ok> { |
167 | 0 | Ok(Union(variant.to_owned(), None)) |
168 | 0 | } |
169 | 0 | fn serialize_newtype_variant<T>( |
170 | 0 | self, |
171 | 0 | _name: &'static str, |
172 | 0 | _variant_index: u32, |
173 | 0 | variant: &'static str, |
174 | 0 | value: &T, |
175 | 0 | ) -> Result<Self::Ok> |
176 | 0 | where |
177 | 0 | T: ?Sized + ser::Serialize, |
178 | | { |
179 | 0 | let value = value.serialize(self)?; |
180 | 0 | Ok(Union(variant.to_owned(), Some(Box::new(value)))) |
181 | 0 | } |
182 | 0 | fn serialize_tuple_variant( |
183 | 0 | self, |
184 | 0 | _name: &'static str, |
185 | 0 | _variant_index: u32, |
186 | 0 | _variant: &'static str, |
187 | 0 | _len: usize, |
188 | 0 | ) -> Result<Self::SerializeTupleVariant> { |
189 | 0 | Err(ErrorKind::Serialize( |
190 | 0 | "Unsupported data for serialization: tuple variant".to_owned(), |
191 | 0 | ) |
192 | 0 | .into()) |
193 | 0 | } |
194 | 0 | fn serialize_struct_variant( |
195 | 0 | self, |
196 | 0 | _name: &'static str, |
197 | 0 | _variant_index: u32, |
198 | 0 | variant: &'static str, |
199 | 0 | _len: usize, |
200 | 0 | ) -> Result<Self::SerializeStructVariant> { |
201 | 0 | Ok(StructVariantSerializer { |
202 | 0 | variant, |
203 | 0 | value: BTreeMap::new(), |
204 | 0 | }) |
205 | 0 | } |
206 | | |
207 | 0 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
208 | 0 | Ok(TupleSerializer::default()) |
209 | 0 | } |
210 | 0 | fn serialize_tuple_struct( |
211 | 0 | self, |
212 | 0 | _name: &'static str, |
213 | 0 | _len: usize, |
214 | 0 | ) -> Result<Self::SerializeTupleStruct> { |
215 | 0 | Err(ErrorKind::Serialize( |
216 | 0 | "Unsupported data for serialization: tuple struct".to_owned(), |
217 | 0 | ) |
218 | 0 | .into()) |
219 | 0 | } |
220 | | |
221 | 0 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
222 | 0 | Ok(SeqSerializer::default()) |
223 | 0 | } |
224 | | |
225 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
226 | 0 | Ok(MapSerializer::default()) |
227 | 0 | } |
228 | | } |
229 | | |
230 | | #[derive(Default)] |
231 | | struct SeqSerializer(Vec<SimpleValue>); |
232 | | |
233 | | impl ser::SerializeSeq for SeqSerializer { |
234 | | type Ok = SimpleValue; |
235 | | type Error = Error; |
236 | | |
237 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
238 | 0 | where |
239 | 0 | T: ?Sized + ser::Serialize, |
240 | | { |
241 | 0 | self.0.push(value.serialize(Serializer)?); |
242 | 0 | Ok(()) |
243 | 0 | } Unexecuted instantiation: <serde_dhall::serialize::SeqSerializer as serde_core::ser::SerializeSeq>::serialize_element::<&anise::structure::location::TerrainMask> Unexecuted instantiation: <serde_dhall::serialize::SeqSerializer as serde_core::ser::SerializeSeq>::serialize_element::<&anise::almanac::metaload::metafile::MetaFile> Unexecuted instantiation: <serde_dhall::serialize::SeqSerializer as serde_core::ser::SerializeSeq>::serialize_element::<&anise::structure::dataset::location_dhall::LocationDhallSetEntry> Unexecuted instantiation: <serde_dhall::serialize::SeqSerializer as serde_core::ser::SerializeSeq>::serialize_element::<_> |
244 | | |
245 | 0 | fn end(self) -> Result<Self::Ok> { |
246 | 0 | Ok(List(self.0)) |
247 | 0 | } |
248 | | } |
249 | | |
250 | | #[derive(Default)] |
251 | | struct TupleSerializer(Vec<SimpleValue>); |
252 | | |
253 | | impl ser::SerializeTuple for TupleSerializer { |
254 | | type Ok = SimpleValue; |
255 | | type Error = Error; |
256 | | |
257 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
258 | 0 | where |
259 | 0 | T: ?Sized + ser::Serialize, |
260 | | { |
261 | 0 | self.0.push(value.serialize(Serializer)?); |
262 | 0 | Ok(()) |
263 | 0 | } |
264 | | |
265 | 0 | fn end(self) -> Result<Self::Ok> { |
266 | | Ok(Record( |
267 | 0 | self.0 |
268 | 0 | .into_iter() |
269 | 0 | .enumerate() |
270 | 0 | .map(|(i, x)| (format!("_{}", i + 1), x)) |
271 | 0 | .collect(), |
272 | | )) |
273 | 0 | } |
274 | | } |
275 | | |
276 | | #[derive(Default)] |
277 | | struct MapSerializer { |
278 | | map: BTreeMap<String, SimpleValue>, |
279 | | key: Option<String>, |
280 | | val: Option<SimpleValue>, |
281 | | } |
282 | | |
283 | | impl ser::SerializeMap for MapSerializer { |
284 | | type Ok = SimpleValue; |
285 | | type Error = Error; |
286 | | |
287 | 0 | fn serialize_key<T>(&mut self, key: &T) -> Result<()> |
288 | 0 | where |
289 | 0 | T: ?Sized + ser::Serialize, |
290 | | { |
291 | 0 | let key = match key.serialize(Serializer)? { |
292 | 0 | Text(key) => key, |
293 | 0 | _ => return Err(<Error as ser::Error>::custom("not a string")), |
294 | | }; |
295 | 0 | if let Some(val) = self.val.take() { |
296 | 0 | self.map.insert(key, val); |
297 | 0 | } else { |
298 | 0 | self.key = Some(key); |
299 | 0 | } |
300 | 0 | Ok(()) |
301 | 0 | } |
302 | | |
303 | 0 | fn serialize_value<T>(&mut self, val: &T) -> Result<()> |
304 | 0 | where |
305 | 0 | T: ?Sized + ser::Serialize, |
306 | | { |
307 | 0 | let val: SimpleValue = val.serialize(Serializer)?; |
308 | 0 | if let Some(key) = self.key.take() { |
309 | 0 | self.map.insert(key, val); |
310 | 0 | } else { |
311 | 0 | self.val = Some(val); |
312 | 0 | } |
313 | 0 | Ok(()) |
314 | 0 | } |
315 | | |
316 | 0 | fn end(self) -> Result<Self::Ok> { |
317 | 0 | Ok(Record(self.map)) |
318 | 0 | } |
319 | | } |
320 | | |
321 | | #[derive(Default)] |
322 | | struct StructSerializer(BTreeMap<String, SimpleValue>); |
323 | | |
324 | | impl ser::SerializeStruct for StructSerializer { |
325 | | type Ok = SimpleValue; |
326 | | type Error = Error; |
327 | | |
328 | 0 | fn serialize_field<T>(&mut self, key: &'static str, val: &T) -> Result<()> |
329 | 0 | where |
330 | 0 | T: ?Sized + ser::Serialize, |
331 | | { |
332 | 0 | let val: SimpleValue = val.serialize(Serializer)?; |
333 | 0 | self.0.insert(key.into(), val); |
334 | 0 | Ok(()) |
335 | 0 | } Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<alloc::vec::Vec<anise::structure::location::TerrainMask>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<alloc::vec::Vec<anise::almanac::metaload::metafile::MetaFile>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<alloc::vec::Vec<anise::structure::dataset::location_dhall::LocationDhallSetEntry>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<core::option::Option<alloc::string::String>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<core::option::Option<i32>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<core::option::Option<u32>> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<alloc::string::String> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<anise::frames::frameuid::FrameUid> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<anise::structure::location::Location> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<bool> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<f64> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<i32> Unexecuted instantiation: <serde_dhall::serialize::StructSerializer as serde_core::ser::SerializeStruct>::serialize_field::<_> |
336 | | |
337 | 0 | fn end(self) -> Result<Self::Ok> { |
338 | 0 | Ok(Record(self.0)) |
339 | 0 | } |
340 | | } |
341 | | |
342 | | struct StructVariantSerializer { |
343 | | variant: &'static str, |
344 | | value: BTreeMap<String, SimpleValue>, |
345 | | } |
346 | | |
347 | | impl ser::SerializeStructVariant for StructVariantSerializer { |
348 | | type Ok = SimpleValue; |
349 | | type Error = Error; |
350 | | |
351 | 0 | fn serialize_field<T>(&mut self, key: &'static str, val: &T) -> Result<()> |
352 | 0 | where |
353 | 0 | T: ?Sized + ser::Serialize, |
354 | | { |
355 | 0 | let val: SimpleValue = val.serialize(Serializer)?; |
356 | 0 | self.value.insert(key.into(), val); |
357 | 0 | Ok(()) |
358 | 0 | } |
359 | | |
360 | 0 | fn end(self) -> Result<Self::Ok> { |
361 | 0 | Ok(SimpleValue::Union( |
362 | 0 | self.variant.to_string(), |
363 | 0 | Some(Box::new(Record(self.value))), |
364 | 0 | )) |
365 | 0 | } |
366 | | } |
367 | | |
368 | | impl serde::ser::Serialize for SimpleValue { |
369 | 0 | fn serialize<S>( |
370 | 0 | &self, |
371 | 0 | serializer: S, |
372 | 0 | ) -> std::result::Result<S::Ok, S::Error> |
373 | 0 | where |
374 | 0 | S: serde::ser::Serializer, |
375 | | { |
376 | | use serde::ser::{SerializeMap, SerializeSeq}; |
377 | | use NumKind::*; |
378 | | use SimpleValue::*; |
379 | | |
380 | 0 | match self { |
381 | 0 | Num(Bool(x)) => serializer.serialize_bool(*x), |
382 | 0 | Num(Natural(x)) => serializer.serialize_u64(*x), |
383 | 0 | Num(Integer(x)) => serializer.serialize_i64(*x), |
384 | 0 | Num(Double(x)) => serializer.serialize_f64((*x).into()), |
385 | 0 | Text(x) => serializer.serialize_str(x), |
386 | 0 | List(xs) => { |
387 | 0 | let mut seq = serializer.serialize_seq(Some(xs.len()))?; |
388 | 0 | for x in xs { |
389 | 0 | seq.serialize_element(x)?; |
390 | | } |
391 | 0 | seq.end() |
392 | | } |
393 | 0 | Optional(None) => serializer.serialize_none(), |
394 | 0 | Optional(Some(x)) => serializer.serialize_some(x), |
395 | 0 | Record(m) => { |
396 | 0 | let mut map = serializer.serialize_map(Some(m.len()))?; |
397 | 0 | for (k, v) in m { |
398 | 0 | map.serialize_entry(k, v)?; |
399 | | } |
400 | 0 | map.end() |
401 | | } |
402 | | // serde's enum support is yet again really limited. We can't avoid a memleak here :(. |
403 | 0 | Union(field_name, None) => { |
404 | 0 | let field_name: Box<str> = field_name.clone().into(); |
405 | 0 | serializer.serialize_unit_variant( |
406 | | "SimpleValue", |
407 | | 0, |
408 | 0 | Box::leak(field_name), |
409 | | ) |
410 | | } |
411 | 0 | Union(field_name, Some(x)) => { |
412 | 0 | let field_name: Box<str> = field_name.clone().into(); |
413 | 0 | serializer.serialize_newtype_variant( |
414 | | "SimpleValue", |
415 | | 0, |
416 | 0 | Box::leak(field_name), |
417 | 0 | x, |
418 | | ) |
419 | | } |
420 | | } |
421 | 0 | } |
422 | | } |