1use crate::de::TEXT_KEY;
4use crate::se::simple_type::{SimpleSeq, SimpleTypeSerializer};
5use crate::se::SeError;
6use serde::ser::{Impossible, Serialize, Serializer};
7use std::fmt::Write;
8
9macro_rules! write_primitive {
10 ($method:ident ( $ty:ty )) => {
11 #[inline]
12 fn $method(self, value: $ty) -> Result<Self::Ok, Self::Error> {
13 self.0.$method(value)
14 }
15 };
16}
17
18pub struct TextSerializer<W: Write>(pub SimpleTypeSerializer<W>);
26
27impl<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 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
63 self.0.serialize_none()
64 }
65
66 fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
67 value.serialize(self)
68 }
69
70 #[inline]
71 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
72 self.0.serialize_unit()
73 }
74
75 #[inline]
76 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
77 self.0.serialize_unit_struct(name)
78 }
79
80 #[inline]
81 fn serialize_unit_variant(
82 self,
83 name: &'static str,
84 variant_index: u32,
85 variant: &'static str,
86 ) -> Result<Self::Ok, Self::Error> {
87 if variant == TEXT_KEY {
88 Ok(self.0.writer)
89 } else {
90 self.0.serialize_unit_variant(name, variant_index, variant)
91 }
92 }
93
94 fn serialize_newtype_struct<T: ?Sized + Serialize>(
95 self,
96 _name: &'static str,
97 value: &T,
98 ) -> Result<Self::Ok, Self::Error> {
99 value.serialize(self)
100 }
101
102 #[inline]
103 fn serialize_newtype_variant<T: ?Sized + Serialize>(
104 self,
105 name: &'static str,
106 _variant_index: u32,
107 variant: &'static str,
108 _value: &T,
109 ) -> Result<Self::Ok, Self::Error> {
110 Err(SeError::Unsupported(
111 format!(
112 "cannot serialize enum newtype variant `{}::{}` as text content value",
113 name, variant
114 )
115 .into(),
116 ))
117 }
118
119 #[inline]
120 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
121 self.0.serialize_seq(len)
122 }
123
124 #[inline]
125 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
126 self.0.serialize_tuple(len)
127 }
128
129 #[inline]
130 fn serialize_tuple_struct(
131 self,
132 name: &'static str,
133 len: usize,
134 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
135 self.0.serialize_tuple_struct(name, len)
136 }
137
138 #[inline]
139 fn serialize_tuple_variant(
140 self,
141 name: &'static str,
142 _variant_index: u32,
143 variant: &'static str,
144 _len: usize,
145 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
146 Err(SeError::Unsupported(
147 format!(
148 "cannot serialize enum tuple variant `{}::{}` as text content value",
149 name, variant
150 )
151 .into(),
152 ))
153 }
154
155 #[inline]
156 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
157 Err(SeError::Unsupported(
158 "cannot serialize map as text content value".into(),
159 ))
160 }
161
162 #[inline]
163 fn serialize_struct(
164 self,
165 name: &'static str,
166 _len: usize,
167 ) -> Result<Self::SerializeStruct, Self::Error> {
168 Err(SeError::Unsupported(
169 format!("cannot serialize struct `{}` as text content value", name).into(),
170 ))
171 }
172
173 #[inline]
174 fn serialize_struct_variant(
175 self,
176 name: &'static str,
177 _variant_index: u32,
178 variant: &'static str,
179 _len: usize,
180 ) -> Result<Self::SerializeStructVariant, Self::Error> {
181 Err(SeError::Unsupported(
182 format!(
183 "cannot serialize enum struct variant `{}::{}` as text content value",
184 name, variant
185 )
186 .into(),
187 ))
188 }
189}