| 1 |
use super::style::Style; |
| 2 |
use super::Buffer; |
| 3 |
use super::Error; |
| 4 |
use super::Serializer; |
| 5 |
use super::Table; |
| 6 |
use crate::alloc_prelude::*; |
| 7 |
|
| 8 |
pub(crate) struct ArrayOfTablesSerializer<'d> { |
| 9 |
buf: &'d mut Buffer, |
| 10 |
parent: Table, |
| 11 |
key: String, |
| 12 |
style: Style, |
| 13 |
} |
| 14 |
|
| 15 |
impl<'d> ArrayOfTablesSerializer<'d> { |
| 16 |
/// Creates a new serializer which will emit TOML into the buffer provided. |
| 17 |
/// |
| 18 |
/// The serializer can then be used to serialize a type after which the data |
| 19 |
/// will be present in `dst`. |
| 20 |
pub(crate) fn new(buf: &'d mut Buffer, parent: Table, key: String, style: Style) -> Self { |
| 21 |
Self { |
| 22 |
buf, |
| 23 |
parent, |
| 24 |
key, |
| 25 |
style, |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl<'d> serde_core::ser::Serializer for ArrayOfTablesSerializer<'d> { |
| 31 |
type Ok = &'d mut Buffer; |
| 32 |
type Error = Error; |
| 33 |
type SerializeSeq = SerializeArrayOfTablesSerializer<'d>; |
| 34 |
type SerializeTuple = SerializeArrayOfTablesSerializer<'d>; |
| 35 |
type SerializeTupleStruct = SerializeArrayOfTablesSerializer<'d>; |
| 36 |
type SerializeTupleVariant = serde_core::ser::Impossible<Self::Ok, Self::Error>; |
| 37 |
type SerializeMap = serde_core::ser::Impossible<Self::Ok, Self::Error>; |
| 38 |
type SerializeStruct = serde_core::ser::Impossible<Self::Ok, Self::Error>; |
| 39 |
type SerializeStructVariant = serde_core::ser::Impossible<Self::Ok, Self::Error>; |
| 40 |
|
| 41 |
fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { |
| 42 |
Err(Error::unsupported_type(Some("bool"))) |
| 43 |
} |
| 44 |
|
| 45 |
fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { |
| 46 |
Err(Error::unsupported_type(Some("i8"))) |
| 47 |
} |
| 48 |
|
| 49 |
fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { |
| 50 |
Err(Error::unsupported_type(Some("i16"))) |
| 51 |
} |
| 52 |
|
| 53 |
fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { |
| 54 |
Err(Error::unsupported_type(Some("i32"))) |
| 55 |
} |
| 56 |
|
| 57 |
fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { |
| 58 |
Err(Error::unsupported_type(Some("i64"))) |
| 59 |
} |
| 60 |
|
| 61 |
fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { |
| 62 |
Err(Error::unsupported_type(Some("u8"))) |
| 63 |
} |
| 64 |
|
| 65 |
fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { |
| 66 |
Err(Error::unsupported_type(Some("u16"))) |
| 67 |
} |
| 68 |
|
| 69 |
fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { |
| 70 |
Err(Error::unsupported_type(Some("u32"))) |
| 71 |
} |
| 72 |
|
| 73 |
fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { |
| 74 |
Err(Error::unsupported_type(Some("u64"))) |
| 75 |
} |
| 76 |
|
| 77 |
fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { |
| 78 |
Err(Error::unsupported_type(Some("f32"))) |
| 79 |
} |
| 80 |
|
| 81 |
fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { |
| 82 |
Err(Error::unsupported_type(Some("f64"))) |
| 83 |
} |
| 84 |
|
| 85 |
fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { |
| 86 |
Err(Error::unsupported_type(Some("char"))) |
| 87 |
} |
| 88 |
|
| 89 |
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> { |
| 90 |
Err(Error::unsupported_type(Some("str"))) |
| 91 |
} |
| 92 |
|
| 93 |
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 94 |
Err(Error::unsupported_type(Some("bytes"))) |
| 95 |
} |
| 96 |
|
| 97 |
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 98 |
Err(Error::unsupported_none()) |
| 99 |
} |
| 100 |
|
| 101 |
fn serialize_some<T>(self, v: &T) -> Result<Self::Ok, Self::Error> |
| 102 |
where |
| 103 |
T: serde_core::ser::Serialize + ?Sized, |
| 104 |
{ |
| 105 |
v.serialize(self) |
| 106 |
} |
| 107 |
|
| 108 |
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 109 |
Err(Error::unsupported_type(Some("unit"))) |
| 110 |
} |
| 111 |
|
| 112 |
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 113 |
Err(Error::unsupported_type(Some(name))) |
| 114 |
} |
| 115 |
|
| 116 |
fn serialize_unit_variant( |
| 117 |
self, |
| 118 |
name: &'static str, |
| 119 |
_variant_index: u32, |
| 120 |
_variant: &'static str, |
| 121 |
) -> Result<Self::Ok, Self::Error> { |
| 122 |
Err(Error::unsupported_type(Some(name))) |
| 123 |
} |
| 124 |
|
| 125 |
fn serialize_newtype_struct<T>( |
| 126 |
self, |
| 127 |
_name: &'static str, |
| 128 |
v: &T, |
| 129 |
) -> Result<Self::Ok, Self::Error> |
| 130 |
where |
| 131 |
T: serde_core::ser::Serialize + ?Sized, |
| 132 |
{ |
| 133 |
v.serialize(self) |
| 134 |
} |
| 135 |
|
| 136 |
fn serialize_newtype_variant<T>( |
| 137 |
self, |
| 138 |
_name: &'static str, |
| 139 |
_variant_index: u32, |
| 140 |
variant: &'static str, |
| 141 |
_value: &T, |
| 142 |
) -> Result<Self::Ok, Self::Error> |
| 143 |
where |
| 144 |
T: serde_core::ser::Serialize + ?Sized, |
| 145 |
{ |
| 146 |
Err(Error::unsupported_type(Some(variant))) |
| 147 |
} |
| 148 |
|
| 149 |
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 150 |
Ok(SerializeArrayOfTablesSerializer::seq( |
| 151 |
self.buf, |
| 152 |
self.parent, |
| 153 |
self.key, |
| 154 |
self.style, |
| 155 |
)) |
| 156 |
} |
| 157 |
|
| 158 |
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 159 |
self.serialize_seq(Some(len)) |
| 160 |
} |
| 161 |
|
| 162 |
fn serialize_tuple_struct( |
| 163 |
self, |
| 164 |
_name: &'static str, |
| 165 |
len: usize, |
| 166 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 167 |
self.serialize_seq(Some(len)) |
| 168 |
} |
| 169 |
|
| 170 |
fn serialize_tuple_variant( |
| 171 |
self, |
| 172 |
_name: &'static str, |
| 173 |
_variant_index: u32, |
| 174 |
variant: &'static str, |
| 175 |
_len: usize, |
| 176 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 177 |
Err(Error::unsupported_type(Some(variant))) |
| 178 |
} |
| 179 |
|
| 180 |
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 181 |
Err(Error::unsupported_type(Some("map"))) |
| 182 |
} |
| 183 |
|
| 184 |
fn serialize_struct( |
| 185 |
self, |
| 186 |
name: &'static str, |
| 187 |
_len: usize, |
| 188 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 189 |
Err(Error::unsupported_type(Some(name))) |
| 190 |
} |
| 191 |
|
| 192 |
fn serialize_struct_variant( |
| 193 |
self, |
| 194 |
_name: &'static str, |
| 195 |
_variant_index: u32, |
| 196 |
variant: &'static str, |
| 197 |
_len: usize, |
| 198 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 199 |
Err(Error::unsupported_type(Some(variant))) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
#[doc(hidden)] |
| 204 |
pub(crate) struct SerializeArrayOfTablesSerializer<'d> { |
| 205 |
buf: &'d mut Buffer, |
| 206 |
parent: Table, |
| 207 |
key: String, |
| 208 |
style: Style, |
| 209 |
} |
| 210 |
|
| 211 |
impl<'d> SerializeArrayOfTablesSerializer<'d> { |
| 212 |
pub(crate) fn seq(buf: &'d mut Buffer, parent: Table, key: String, style: Style) -> Self { |
| 213 |
Self { |
| 214 |
buf, |
| 215 |
parent, |
| 216 |
key, |
| 217 |
style, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
fn end(self) -> Result<&'d mut Buffer, Error> { |
| 222 |
Ok(self.buf) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl<'d> serde_core::ser::SerializeSeq for SerializeArrayOfTablesSerializer<'d> { |
| 227 |
type Ok = &'d mut Buffer; |
| 228 |
type Error = Error; |
| 229 |
|
| 230 |
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error> |
| 231 |
where |
| 232 |
T: serde_core::ser::Serialize + ?Sized, |
| 233 |
{ |
| 234 |
let child = self.buf.element_table(&mut self.parent, self.key.clone()); |
| 235 |
let value_serializer = Serializer::with_table(self.buf, child, self.style); |
| 236 |
value.serialize(value_serializer)?; |
| 237 |
Ok(()) |
| 238 |
} |
| 239 |
|
| 240 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 241 |
self.end() |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
impl<'d> serde_core::ser::SerializeTuple for SerializeArrayOfTablesSerializer<'d> { |
| 246 |
type Ok = &'d mut Buffer; |
| 247 |
type Error = Error; |
| 248 |
|
| 249 |
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error> |
| 250 |
where |
| 251 |
T: serde_core::ser::Serialize + ?Sized, |
| 252 |
{ |
| 253 |
serde_core::ser::SerializeSeq::serialize_element(self, value) |
| 254 |
} |
| 255 |
|
| 256 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 257 |
serde_core::ser::SerializeSeq::end(self) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
impl<'d> serde_core::ser::SerializeTupleStruct for SerializeArrayOfTablesSerializer<'d> { |
| 262 |
type Ok = &'d mut Buffer; |
| 263 |
type Error = Error; |
| 264 |
|
| 265 |
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error> |
| 266 |
where |
| 267 |
T: serde_core::ser::Serialize + ?Sized, |
| 268 |
{ |
| 269 |
serde_core::ser::SerializeSeq::serialize_element(self, value) |
| 270 |
} |
| 271 |
|
| 272 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 273 |
serde_core::ser::SerializeSeq::end(self) |
| 274 |
} |
| 275 |
} |