1 | use super::Error; |
2 | |
3 | #[doc (hidden)] |
4 | pub struct SerializeValueArray { |
5 | values: Vec<crate::Item>, |
6 | } |
7 | |
8 | impl SerializeValueArray { |
9 | pub(crate) fn new() -> Self { |
10 | Self { values: Vec::new() } |
11 | } |
12 | |
13 | pub(crate) fn with_capacity(len: usize) -> Self { |
14 | Self { |
15 | values: Vec::with_capacity(len), |
16 | } |
17 | } |
18 | } |
19 | |
20 | impl serde::ser::SerializeSeq for SerializeValueArray { |
21 | type Ok = crate::Value; |
22 | type Error = Error; |
23 | |
24 | fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
25 | where |
26 | T: serde::ser::Serialize, |
27 | { |
28 | let value: Value = value.serialize(serializer:super::ValueSerializer {})?; |
29 | self.values.push(crate::Item::Value(value)); |
30 | Ok(()) |
31 | } |
32 | |
33 | fn end(self) -> Result<Self::Ok, Self::Error> { |
34 | Ok(crate::Value::Array(crate::Array::with_vec(self.values))) |
35 | } |
36 | } |
37 | |
38 | impl serde::ser::SerializeTuple for SerializeValueArray { |
39 | type Ok = crate::Value; |
40 | type Error = Error; |
41 | |
42 | fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
43 | where |
44 | T: serde::ser::Serialize, |
45 | { |
46 | serde::ser::SerializeSeq::serialize_element(self, value) |
47 | } |
48 | |
49 | fn end(self) -> Result<Self::Ok, Self::Error> { |
50 | serde::ser::SerializeSeq::end(self) |
51 | } |
52 | } |
53 | |
54 | impl serde::ser::SerializeTupleVariant for SerializeValueArray { |
55 | type Ok = crate::Value; |
56 | type Error = Error; |
57 | |
58 | fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
59 | where |
60 | T: serde::ser::Serialize, |
61 | { |
62 | serde::ser::SerializeSeq::serialize_element(self, value) |
63 | } |
64 | |
65 | fn end(self) -> Result<Self::Ok, Self::Error> { |
66 | serde::ser::SerializeSeq::end(self) |
67 | } |
68 | } |
69 | |
70 | impl serde::ser::SerializeTupleStruct for SerializeValueArray { |
71 | type Ok = crate::Value; |
72 | type Error = Error; |
73 | |
74 | fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
75 | where |
76 | T: serde::ser::Serialize, |
77 | { |
78 | serde::ser::SerializeSeq::serialize_element(self, value) |
79 | } |
80 | |
81 | fn end(self) -> Result<Self::Ok, Self::Error> { |
82 | serde::ser::SerializeSeq::end(self) |
83 | } |
84 | } |
85 | |