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