| 1 |
use super::Error; |
| 2 |
use super::array::SerializeTupleVariant; |
| 3 |
use super::array::SerializeValueArray; |
| 4 |
use super::key::KeySerializer; |
| 5 |
use super::value::ValueSerializer; |
| 6 |
|
| 7 |
#[doc(hidden)] |
| 8 |
#[allow(clippy::large_enum_variant)] |
| 9 |
pub enum SerializeMap { |
| 10 |
Datetime(SerializeDatetime), |
| 11 |
Table(SerializeInlineTable), |
| 12 |
} |
| 13 |
|
| 14 |
impl SerializeMap { |
| 15 |
pub(crate) fn map(len: Option<usize>) -> Self { |
| 16 |
Self::Table(SerializeInlineTable::map(len)) |
| 17 |
} |
| 18 |
|
| 19 |
pub(crate) fn struct_(name: &'static str, len: Option<usize>) -> Self { |
| 20 |
if toml_datetime::ser::is_datetime(name) { |
| 21 |
Self::Datetime(SerializeDatetime::new()) |
| 22 |
} else { |
| 23 |
Self::map(len) |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl serde_core::ser::SerializeMap for SerializeMap { |
| 29 |
type Ok = crate::Value; |
| 30 |
type Error = Error; |
| 31 |
|
| 32 |
fn serialize_key<T>(&mut self, input: &T) -> Result<(), Self::Error> |
| 33 |
where |
| 34 |
T: serde_core::ser::Serialize + ?Sized, |
| 35 |
{ |
| 36 |
match self { |
| 37 |
Self::Datetime(s) => s.serialize_key(input), |
| 38 |
Self::Table(s) => s.serialize_key(input), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 43 |
where |
| 44 |
T: serde_core::ser::Serialize + ?Sized, |
| 45 |
{ |
| 46 |
match self { |
| 47 |
Self::Datetime(s) => s.serialize_value(value), |
| 48 |
Self::Table(s) => s.serialize_value(value), |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 53 |
match self { |
| 54 |
Self::Datetime(s) => s.end().map(|items| items.into()), |
| 55 |
Self::Table(s) => s.end().map(|items| items.into()), |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl serde_core::ser::SerializeStruct for SerializeMap { |
| 61 |
type Ok = crate::Value; |
| 62 |
type Error = Error; |
| 63 |
|
| 64 |
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 65 |
where |
| 66 |
T: serde_core::ser::Serialize + ?Sized, |
| 67 |
{ |
| 68 |
match self { |
| 69 |
Self::Datetime(s) => s.serialize_field(key, value), |
| 70 |
Self::Table(s) => s.serialize_field(key, value), |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 75 |
match self { |
| 76 |
Self::Datetime(s) => s.end().map(|items| items.into()), |
| 77 |
Self::Table(s) => s.end().map(|items| items.into()), |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#[doc(hidden)] |
| 83 |
pub struct SerializeDatetime { |
| 84 |
inner: toml_datetime::ser::DatetimeSerializer, |
| 85 |
} |
| 86 |
|
| 87 |
impl SerializeDatetime { |
| 88 |
pub(crate) fn new() -> Self { |
| 89 |
Self { |
| 90 |
inner: toml_datetime::ser::DatetimeSerializer::new(), |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl serde_core::ser::SerializeMap for SerializeDatetime { |
| 96 |
type Ok = crate::Datetime; |
| 97 |
type Error = Error; |
| 98 |
|
| 99 |
fn serialize_key<T>(&mut self, _input: &T) -> Result<(), Self::Error> |
| 100 |
where |
| 101 |
T: serde_core::ser::Serialize + ?Sized, |
| 102 |
{ |
| 103 |
unreachable!("datetimes should only be serialized as structs, not maps") |
| 104 |
} |
| 105 |
|
| 106 |
fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error> |
| 107 |
where |
| 108 |
T: serde_core::ser::Serialize + ?Sized, |
| 109 |
{ |
| 110 |
unreachable!("datetimes should only be serialized as structs, not maps") |
| 111 |
} |
| 112 |
|
| 113 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 114 |
unreachable!("datetimes should only be serialized as structs, not maps") |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
impl serde_core::ser::SerializeStruct for SerializeDatetime { |
| 119 |
type Ok = crate::Datetime; |
| 120 |
type Error = Error; |
| 121 |
|
| 122 |
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 123 |
where |
| 124 |
T: serde_core::ser::Serialize + ?Sized, |
| 125 |
{ |
| 126 |
self.inner.serialize_field(key, value).map_err(dt_err)?; |
| 127 |
|
| 128 |
Ok(()) |
| 129 |
} |
| 130 |
|
| 131 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 132 |
let value = self.inner.end().map_err(dt_err)?; |
| 133 |
Ok(value) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
fn dt_err(err: toml_datetime::ser::SerializerError) -> Error { |
| 138 |
match err { |
| 139 |
toml_datetime::ser::SerializerError::InvalidFormat(err) => Error::custom(err), |
| 140 |
_ => Error::date_invalid(), |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[doc(hidden)] |
| 145 |
pub struct SerializeInlineTable { |
| 146 |
items: crate::table::KeyValuePairs, |
| 147 |
key: Option<crate::Key>, |
| 148 |
} |
| 149 |
|
| 150 |
impl SerializeInlineTable { |
| 151 |
pub(crate) fn map(len: Option<usize>) -> Self { |
| 152 |
let mut items: crate::table::KeyValuePairs = Default::default(); |
| 153 |
let key = Default::default(); |
| 154 |
if let Some(len) = len { |
| 155 |
items.reserve(len); |
| 156 |
} |
| 157 |
Self { items, key } |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl serde_core::ser::SerializeMap for SerializeInlineTable { |
| 162 |
type Ok = crate::InlineTable; |
| 163 |
type Error = Error; |
| 164 |
|
| 165 |
fn serialize_key<T>(&mut self, input: &T) -> Result<(), Self::Error> |
| 166 |
where |
| 167 |
T: serde_core::ser::Serialize + ?Sized, |
| 168 |
{ |
| 169 |
self.key = Some(input.serialize(KeySerializer)?); |
| 170 |
Ok(()) |
| 171 |
} |
| 172 |
|
| 173 |
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
| 174 |
where |
| 175 |
T: serde_core::ser::Serialize + ?Sized, |
| 176 |
{ |
| 177 |
let mut is_none = false; |
| 178 |
let value_serializer = MapValueSerializer::new(&mut is_none); |
| 179 |
let res = value.serialize(value_serializer); |
| 180 |
match res { |
| 181 |
Ok(item) => { |
| 182 |
let key = self.key.take().unwrap(); |
| 183 |
let item = crate::Item::Value(item); |
| 184 |
self.items.insert(key, item); |
| 185 |
} |
| 186 |
Err(e) => { |
| 187 |
if !(e == Error::unsupported_none() && is_none) { |
| 188 |
return Err(e); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
Ok(()) |
| 193 |
} |
| 194 |
|
| 195 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 196 |
Ok(crate::InlineTable::with_pairs(self.items)) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
impl serde_core::ser::SerializeStruct for SerializeInlineTable { |
| 201 |
type Ok = crate::InlineTable; |
| 202 |
type Error = Error; |
| 203 |
|
| 204 |
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 205 |
where |
| 206 |
T: serde_core::ser::Serialize + ?Sized, |
| 207 |
{ |
| 208 |
let mut is_none = false; |
| 209 |
let value_serializer = MapValueSerializer::new(&mut is_none); |
| 210 |
let res = value.serialize(value_serializer); |
| 211 |
match res { |
| 212 |
Ok(item) => { |
| 213 |
let item = crate::Item::Value(item); |
| 214 |
self.items.insert(crate::Key::new(key), item); |
| 215 |
} |
| 216 |
Err(e) => { |
| 217 |
if !(e == Error::unsupported_none() && is_none) { |
| 218 |
return Err(e); |
| 219 |
} |
| 220 |
} |
| 221 |
}; |
| 222 |
Ok(()) |
| 223 |
} |
| 224 |
|
| 225 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 226 |
Ok(crate::InlineTable::with_pairs(self.items)) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
struct MapValueSerializer<'d> { |
| 231 |
is_none: &'d mut bool, |
| 232 |
} |
| 233 |
|
| 234 |
impl<'d> MapValueSerializer<'d> { |
| 235 |
fn new(is_none: &'d mut bool) -> Self { |
| 236 |
Self { is_none } |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl serde_core::ser::Serializer for MapValueSerializer<'_> { |
| 241 |
type Ok = crate::Value; |
| 242 |
type Error = Error; |
| 243 |
type SerializeSeq = SerializeValueArray; |
| 244 |
type SerializeTuple = SerializeValueArray; |
| 245 |
type SerializeTupleStruct = SerializeValueArray; |
| 246 |
type SerializeTupleVariant = SerializeTupleVariant; |
| 247 |
type SerializeMap = SerializeMap; |
| 248 |
type SerializeStruct = SerializeMap; |
| 249 |
type SerializeStructVariant = SerializeStructVariant; |
| 250 |
|
| 251 |
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
| 252 |
ValueSerializer::new().serialize_bool(v) |
| 253 |
} |
| 254 |
|
| 255 |
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
| 256 |
ValueSerializer::new().serialize_i8(v) |
| 257 |
} |
| 258 |
|
| 259 |
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
| 260 |
ValueSerializer::new().serialize_i16(v) |
| 261 |
} |
| 262 |
|
| 263 |
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
| 264 |
ValueSerializer::new().serialize_i32(v) |
| 265 |
} |
| 266 |
|
| 267 |
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
| 268 |
ValueSerializer::new().serialize_i64(v) |
| 269 |
} |
| 270 |
|
| 271 |
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
| 272 |
ValueSerializer::new().serialize_u8(v) |
| 273 |
} |
| 274 |
|
| 275 |
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
| 276 |
ValueSerializer::new().serialize_u16(v) |
| 277 |
} |
| 278 |
|
| 279 |
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
| 280 |
ValueSerializer::new().serialize_u32(v) |
| 281 |
} |
| 282 |
|
| 283 |
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
| 284 |
ValueSerializer::new().serialize_u64(v) |
| 285 |
} |
| 286 |
|
| 287 |
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
| 288 |
ValueSerializer::new().serialize_f32(v) |
| 289 |
} |
| 290 |
|
| 291 |
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
| 292 |
ValueSerializer::new().serialize_f64(v) |
| 293 |
} |
| 294 |
|
| 295 |
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
| 296 |
ValueSerializer::new().serialize_char(v) |
| 297 |
} |
| 298 |
|
| 299 |
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
| 300 |
ValueSerializer::new().serialize_str(v) |
| 301 |
} |
| 302 |
|
| 303 |
fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> { |
| 304 |
ValueSerializer::new().serialize_bytes(value) |
| 305 |
} |
| 306 |
|
| 307 |
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
| 308 |
*self.is_none = true; |
| 309 |
Err(Error::unsupported_none()) |
| 310 |
} |
| 311 |
|
| 312 |
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> |
| 313 |
where |
| 314 |
T: serde_core::ser::Serialize + ?Sized, |
| 315 |
{ |
| 316 |
ValueSerializer::new().serialize_some(value) |
| 317 |
} |
| 318 |
|
| 319 |
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
| 320 |
ValueSerializer::new().serialize_unit() |
| 321 |
} |
| 322 |
|
| 323 |
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
| 324 |
ValueSerializer::new().serialize_unit_struct(name) |
| 325 |
} |
| 326 |
|
| 327 |
fn serialize_unit_variant( |
| 328 |
self, |
| 329 |
name: &'static str, |
| 330 |
variant_index: u32, |
| 331 |
variant: &'static str, |
| 332 |
) -> Result<Self::Ok, Self::Error> { |
| 333 |
ValueSerializer::new().serialize_unit_variant(name, variant_index, variant) |
| 334 |
} |
| 335 |
|
| 336 |
fn serialize_newtype_struct<T>( |
| 337 |
self, |
| 338 |
_name: &'static str, |
| 339 |
value: &T, |
| 340 |
) -> Result<Self::Ok, Self::Error> |
| 341 |
where |
| 342 |
T: serde_core::ser::Serialize + ?Sized, |
| 343 |
{ |
| 344 |
value.serialize(self) |
| 345 |
} |
| 346 |
|
| 347 |
fn serialize_newtype_variant<T>( |
| 348 |
self, |
| 349 |
name: &'static str, |
| 350 |
variant_index: u32, |
| 351 |
variant: &'static str, |
| 352 |
value: &T, |
| 353 |
) -> Result<Self::Ok, Self::Error> |
| 354 |
where |
| 355 |
T: serde_core::ser::Serialize + ?Sized, |
| 356 |
{ |
| 357 |
ValueSerializer::new().serialize_newtype_variant(name, variant_index, variant, value) |
| 358 |
} |
| 359 |
|
| 360 |
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 361 |
ValueSerializer::new().serialize_seq(len) |
| 362 |
} |
| 363 |
|
| 364 |
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 365 |
ValueSerializer::new().serialize_tuple(len) |
| 366 |
} |
| 367 |
|
| 368 |
fn serialize_tuple_struct( |
| 369 |
self, |
| 370 |
name: &'static str, |
| 371 |
len: usize, |
| 372 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 373 |
ValueSerializer::new().serialize_tuple_struct(name, len) |
| 374 |
} |
| 375 |
|
| 376 |
fn serialize_tuple_variant( |
| 377 |
self, |
| 378 |
name: &'static str, |
| 379 |
variant_index: u32, |
| 380 |
variant: &'static str, |
| 381 |
len: usize, |
| 382 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 383 |
ValueSerializer::new().serialize_tuple_variant(name, variant_index, variant, len) |
| 384 |
} |
| 385 |
|
| 386 |
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 387 |
ValueSerializer::new().serialize_map(len) |
| 388 |
} |
| 389 |
|
| 390 |
fn serialize_struct( |
| 391 |
self, |
| 392 |
name: &'static str, |
| 393 |
len: usize, |
| 394 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 395 |
ValueSerializer::new().serialize_struct(name, len) |
| 396 |
} |
| 397 |
|
| 398 |
fn serialize_struct_variant( |
| 399 |
self, |
| 400 |
name: &'static str, |
| 401 |
variant_index: u32, |
| 402 |
variant: &'static str, |
| 403 |
len: usize, |
| 404 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 405 |
ValueSerializer::new().serialize_struct_variant(name, variant_index, variant, len) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
pub struct SerializeStructVariant { |
| 410 |
variant: &'static str, |
| 411 |
inner: SerializeInlineTable, |
| 412 |
} |
| 413 |
|
| 414 |
impl SerializeStructVariant { |
| 415 |
pub(crate) fn struct_(variant: &'static str, len: usize) -> Self { |
| 416 |
Self { |
| 417 |
variant, |
| 418 |
inner: SerializeInlineTable::map(Some(len)), |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
impl serde_core::ser::SerializeStructVariant for SerializeStructVariant { |
| 424 |
type Ok = crate::Value; |
| 425 |
type Error = Error; |
| 426 |
|
| 427 |
#[inline] |
| 428 |
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
| 429 |
where |
| 430 |
T: serde_core::ser::Serialize + ?Sized, |
| 431 |
{ |
| 432 |
serde_core::ser::SerializeStruct::serialize_field(&mut self.inner, key, value) |
| 433 |
} |
| 434 |
|
| 435 |
#[inline] |
| 436 |
fn end(self) -> Result<Self::Ok, Self::Error> { |
| 437 |
let inner = serde_core::ser::SerializeStruct::end(self.inner)?.into(); |
| 438 |
let mut items = crate::table::KeyValuePairs::new(); |
| 439 |
let value = crate::Item::Value(inner); |
| 440 |
items.insert(crate::Key::new(self.variant), value); |
| 441 |
Ok(crate::Value::InlineTable(crate::InlineTable::with_pairs( |
| 442 |
items, |
| 443 |
))) |
| 444 |
} |
| 445 |
} |