/rust/registry/src/index.crates.io-1949cf8c6b5b557f/quick-xml-0.41.0/src/se/text.rs
Line | Count | Source |
1 | | //! Contains serializer for a special `&text` field |
2 | | |
3 | | use crate::de::TEXT_KEY; |
4 | | use crate::se::simple_type::{SimpleSeq, SimpleTypeSerializer}; |
5 | | use crate::se::SeError; |
6 | | use serde::ser::{Impossible, Serialize, Serializer}; |
7 | | use std::fmt::Write; |
8 | | |
9 | | macro_rules! write_primitive { |
10 | | ($method:ident ( $ty:ty )) => { |
11 | | #[inline] |
12 | 0 | fn $method(self, value: $ty) -> Result<Self::Ok, Self::Error> { |
13 | 0 | self.0.$method(value) |
14 | 0 | } Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_i8 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_u8 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_f32 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_f64 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_i16 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_i32 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_i64 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_str Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_u16 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_u32 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_u64 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_bool Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_char Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_i128 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_u128 Unexecuted instantiation: <quick_xml::se::text::TextSerializer<_> as serde_core::ser::Serializer>::serialize_bytes |
15 | | }; |
16 | | } |
17 | | |
18 | | //////////////////////////////////////////////////////////////////////////////////////////////////// |
19 | | |
20 | | /// A serializer used to serialize a `$text` field of a struct or map. |
21 | | /// |
22 | | /// This serializer a very similar to [`SimpleTypeSerializer`], but different |
23 | | /// from it in how it processes unit enum variants. Unlike [`SimpleTypeSerializer`] |
24 | | /// this serializer does not write anything for the unit variant. |
25 | | pub struct TextSerializer<W: Write>(pub SimpleTypeSerializer<W>); |
26 | | |
27 | | impl<W: Write> Serializer for TextSerializer<W> { |
28 | | type Ok = W; |
29 | | type Error = SeError; |
30 | | |
31 | | type SerializeSeq = SimpleSeq<W>; |
32 | | type SerializeTuple = SimpleSeq<W>; |
33 | | type SerializeTupleStruct = SimpleSeq<W>; |
34 | | type SerializeTupleVariant = SimpleSeq<W>; |
35 | | type SerializeMap = Impossible<Self::Ok, Self::Error>; |
36 | | type SerializeStruct = Impossible<Self::Ok, Self::Error>; |
37 | | type SerializeStructVariant = Impossible<Self::Ok, Self::Error>; |
38 | | |
39 | | write_primitive!(serialize_bool(bool)); |
40 | | |
41 | | write_primitive!(serialize_i8(i8)); |
42 | | write_primitive!(serialize_i16(i16)); |
43 | | write_primitive!(serialize_i32(i32)); |
44 | | write_primitive!(serialize_i64(i64)); |
45 | | |
46 | | write_primitive!(serialize_u8(u8)); |
47 | | write_primitive!(serialize_u16(u16)); |
48 | | write_primitive!(serialize_u32(u32)); |
49 | | write_primitive!(serialize_u64(u64)); |
50 | | |
51 | | write_primitive!(serialize_i128(i128)); |
52 | | write_primitive!(serialize_u128(u128)); |
53 | | |
54 | | write_primitive!(serialize_f32(f32)); |
55 | | write_primitive!(serialize_f64(f64)); |
56 | | |
57 | | write_primitive!(serialize_char(char)); |
58 | | write_primitive!(serialize_str(&str)); |
59 | | write_primitive!(serialize_bytes(&[u8])); |
60 | | |
61 | | #[inline] |
62 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
63 | 0 | self.0.serialize_none() |
64 | 0 | } |
65 | | |
66 | 0 | fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> { |
67 | 0 | value.serialize(self) |
68 | 0 | } |
69 | | |
70 | | #[inline] |
71 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
72 | 0 | self.0.serialize_unit() |
73 | 0 | } |
74 | | |
75 | | #[inline] |
76 | 0 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
77 | 0 | self.0.serialize_unit_struct(name) |
78 | 0 | } |
79 | | |
80 | | #[inline] |
81 | 0 | fn serialize_unit_variant( |
82 | 0 | self, |
83 | 0 | name: &'static str, |
84 | 0 | variant_index: u32, |
85 | 0 | variant: &'static str, |
86 | 0 | ) -> Result<Self::Ok, Self::Error> { |
87 | 0 | if variant == TEXT_KEY { |
88 | 0 | Ok(self.0.writer) |
89 | | } else { |
90 | 0 | self.0.serialize_unit_variant(name, variant_index, variant) |
91 | | } |
92 | 0 | } |
93 | | |
94 | 0 | fn serialize_newtype_struct<T: ?Sized + Serialize>( |
95 | 0 | self, |
96 | 0 | _name: &'static str, |
97 | 0 | value: &T, |
98 | 0 | ) -> Result<Self::Ok, Self::Error> { |
99 | 0 | value.serialize(self) |
100 | 0 | } |
101 | | |
102 | | #[inline] |
103 | 0 | fn serialize_newtype_variant<T: ?Sized + Serialize>( |
104 | 0 | self, |
105 | 0 | name: &'static str, |
106 | 0 | _variant_index: u32, |
107 | 0 | variant: &'static str, |
108 | 0 | _value: &T, |
109 | 0 | ) -> Result<Self::Ok, Self::Error> { |
110 | 0 | Err(SeError::Unsupported( |
111 | 0 | format!( |
112 | 0 | "cannot serialize enum newtype variant `{}::{}` as text content value", |
113 | 0 | name, variant |
114 | 0 | ) |
115 | 0 | .into(), |
116 | 0 | )) |
117 | 0 | } |
118 | | |
119 | | #[inline] |
120 | 0 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
121 | 0 | self.0.serialize_seq(len) |
122 | 0 | } |
123 | | |
124 | | #[inline] |
125 | 0 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
126 | 0 | self.0.serialize_tuple(len) |
127 | 0 | } |
128 | | |
129 | | #[inline] |
130 | 0 | fn serialize_tuple_struct( |
131 | 0 | self, |
132 | 0 | name: &'static str, |
133 | 0 | len: usize, |
134 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
135 | 0 | self.0.serialize_tuple_struct(name, len) |
136 | 0 | } |
137 | | |
138 | | #[inline] |
139 | 0 | fn serialize_tuple_variant( |
140 | 0 | self, |
141 | 0 | name: &'static str, |
142 | 0 | _variant_index: u32, |
143 | 0 | variant: &'static str, |
144 | 0 | _len: usize, |
145 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
146 | 0 | Err(SeError::Unsupported( |
147 | 0 | format!( |
148 | 0 | "cannot serialize enum tuple variant `{}::{}` as text content value", |
149 | 0 | name, variant |
150 | 0 | ) |
151 | 0 | .into(), |
152 | 0 | )) |
153 | 0 | } |
154 | | |
155 | | #[inline] |
156 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
157 | 0 | Err(SeError::Unsupported( |
158 | 0 | "cannot serialize map as text content value".into(), |
159 | 0 | )) |
160 | 0 | } |
161 | | |
162 | | #[inline] |
163 | 0 | fn serialize_struct( |
164 | 0 | self, |
165 | 0 | name: &'static str, |
166 | 0 | _len: usize, |
167 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
168 | 0 | Err(SeError::Unsupported( |
169 | 0 | format!("cannot serialize struct `{}` as text content value", name).into(), |
170 | 0 | )) |
171 | 0 | } |
172 | | |
173 | | #[inline] |
174 | 0 | fn serialize_struct_variant( |
175 | 0 | self, |
176 | 0 | name: &'static str, |
177 | 0 | _variant_index: u32, |
178 | 0 | variant: &'static str, |
179 | 0 | _len: usize, |
180 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
181 | 0 | Err(SeError::Unsupported( |
182 | 0 | format!( |
183 | 0 | "cannot serialize enum struct variant `{}::{}` as text content value", |
184 | 0 | name, variant |
185 | 0 | ) |
186 | 0 | .into(), |
187 | 0 | )) |
188 | 0 | } |
189 | | } |