| 1 |
use serde::ser::{self, Serialize}; |
| 2 |
use std::cell::Cell; |
| 3 |
use std::error; |
| 4 |
use std::fmt::{self, Display, Write}; |
| 5 |
|
| 6 |
/// Serialize the given data structure as a String of TOML. |
| 7 |
/// |
| 8 |
/// Serialization can fail if `T`'s implementation of `Serialize` decides to |
| 9 |
/// fail, if `T` contains a map with non-string keys, or if `T` attempts to |
| 10 |
/// serialize an unsupported datatype such as an enum, tuple, or tuple struct. |
| 11 |
pub fn to_string<T: ?Sized>(value: &T) -> Result<String, crate::Error> |
| 12 |
where |
| 13 |
T: Serialize, |
| 14 |
{ |
| 15 |
let mut dst = String::with_capacity(128); |
| 16 |
value.serialize(&mut Serializer::new(&mut dst))?; |
| 17 |
Ok(dst) |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
pub(crate) enum Error { |
| 22 |
/// Indicates that a Rust type was requested to be serialized but it was not |
| 23 |
/// supported. |
| 24 |
/// |
| 25 |
/// Currently the TOML format does not support serializing types such as |
| 26 |
/// enums, tuples and tuple structs. |
| 27 |
UnsupportedType, |
| 28 |
|
| 29 |
/// The key of all TOML maps must be strings, but serialization was |
| 30 |
/// attempted where the key of a map was not a string. |
| 31 |
KeyNotString, |
| 32 |
|
| 33 |
/// All values in a TOML table must be emitted before further tables are |
| 34 |
/// emitted. If a value is emitted *after* a table then this error is |
| 35 |
/// generated. |
| 36 |
ValueAfterTable, |
| 37 |
|
| 38 |
/// None was attempted to be serialized, but it's not supported. |
| 39 |
UnsupportedNone, |
| 40 |
|
| 41 |
/// A custom error which could be generated when serializing a particular |
| 42 |
/// type. |
| 43 |
Custom(String), |
| 44 |
} |
| 45 |
|
| 46 |
struct Serializer<'a> { |
| 47 |
dst: &'a mut String, |
| 48 |
state: State<'a>, |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Debug, Copy, Clone)] |
| 52 |
enum ArrayState { |
| 53 |
Started, |
| 54 |
StartedAsATable, |
| 55 |
} |
| 56 |
|
| 57 |
#[derive(Debug, Clone)] |
| 58 |
enum State<'a> { |
| 59 |
Table { |
| 60 |
key: &'a str, |
| 61 |
parent: &'a State<'a>, |
| 62 |
first: &'a Cell<bool>, |
| 63 |
table_emitted: &'a Cell<bool>, |
| 64 |
}, |
| 65 |
Array { |
| 66 |
parent: &'a State<'a>, |
| 67 |
first: &'a Cell<bool>, |
| 68 |
type_: &'a Cell<Option<ArrayState>>, |
| 69 |
len: Option<usize>, |
| 70 |
}, |
| 71 |
End, |
| 72 |
} |
| 73 |
|
| 74 |
struct SerializeSeq<'a, 'b> { |
| 75 |
ser: &'b mut Serializer<'a>, |
| 76 |
first: Cell<bool>, |
| 77 |
type_: Cell<Option<ArrayState>>, |
| 78 |
len: Option<usize>, |
| 79 |
} |
| 80 |
|
| 81 |
struct SerializeTable<'a, 'b> { |
| 82 |
ser: &'b mut Serializer<'a>, |
| 83 |
key: String, |
| 84 |
first: Cell<bool>, |
| 85 |
table_emitted: Cell<bool>, |
| 86 |
} |
| 87 |
|
| 88 |
impl<'a> Serializer<'a> { |
| 89 |
fn new(dst: &'a mut String) -> Serializer<'a> { |
| 90 |
Serializer { |
| 91 |
dst, |
| 92 |
state: State::End, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
fn display<T: Display>(&mut self, t: T, type_: ArrayState) -> Result<(), Error> { |
| 97 |
self.emit_key(type_)?; |
| 98 |
write!(self.dst, "{}", t).map_err(ser::Error::custom)?; |
| 99 |
if let State::Table { .. } = self.state { |
| 100 |
self.dst.push('\n'); |
| 101 |
} |
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
|
| 105 |
fn emit_key(&mut self, type_: ArrayState) -> Result<(), Error> { |
| 106 |
self.array_type(type_); |
| 107 |
let state = self.state.clone(); |
| 108 |
self._emit_key(&state) |
| 109 |
} |
| 110 |
|
| 111 |
// recursive implementation of `emit_key` above |
| 112 |
fn _emit_key(&mut self, state: &State) -> Result<(), Error> { |
| 113 |
match *state { |
| 114 |
State::End => Ok(()), |
| 115 |
State::Array { |
| 116 |
parent, |
| 117 |
first, |
| 118 |
type_, |
| 119 |
len, |
| 120 |
} => { |
| 121 |
assert!(type_.get().is_some()); |
| 122 |
if first.get() { |
| 123 |
self._emit_key(parent)?; |
| 124 |
} |
| 125 |
self.emit_array(first, len); |
| 126 |
Ok(()) |
| 127 |
} |
| 128 |
State::Table { |
| 129 |
parent, |
| 130 |
first, |
| 131 |
table_emitted, |
| 132 |
key, |
| 133 |
} => { |
| 134 |
if table_emitted.get() { |
| 135 |
return Err(Error::ValueAfterTable); |
| 136 |
} |
| 137 |
if first.get() { |
| 138 |
self.emit_table_header(parent)?; |
| 139 |
first.set(false); |
| 140 |
} |
| 141 |
self.escape_key(key)?; |
| 142 |
self.dst.push_str(" = "); |
| 143 |
Ok(()) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
fn emit_array(&mut self, first: &Cell<bool>, _len: Option<usize>) { |
| 149 |
if first.get() { |
| 150 |
self.dst.push('['); |
| 151 |
} else { |
| 152 |
self.dst.push_str(", "); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
fn array_type(&mut self, type_: ArrayState) { |
| 157 |
let prev = match self.state { |
| 158 |
State::Array { type_, .. } => type_, |
| 159 |
_ => return, |
| 160 |
}; |
| 161 |
if prev.get().is_none() { |
| 162 |
prev.set(Some(type_)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
fn escape_key(&mut self, key: &str) -> Result<(), Error> { |
| 167 |
let ok = !key.is_empty() |
| 168 |
&& key.chars().all(|c| match c { |
| 169 |
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true, |
| 170 |
_ => false, |
| 171 |
}); |
| 172 |
if ok { |
| 173 |
write!(self.dst, "{}", key).map_err(ser::Error::custom)?; |
| 174 |
} else { |
| 175 |
self.emit_str(key)?; |
| 176 |
} |
| 177 |
Ok(()) |
| 178 |
} |
| 179 |
|
| 180 |
fn emit_str(&mut self, value: &str) -> Result<(), Error> { |
| 181 |
self.dst.push('"'); |
| 182 |
for ch in value.chars() { |
| 183 |
match ch { |
| 184 |
'\u{8}' => self.dst.push_str("\\b"), |
| 185 |
'\u{9}' => self.dst.push_str("\\t"), |
| 186 |
'\u{a}' => self.dst.push_str("\\n"), |
| 187 |
'\u{c}' => self.dst.push_str("\\f"), |
| 188 |
'\u{d}' => self.dst.push_str("\\r"), |
| 189 |
'\u{22}' => self.dst.push_str("\\\""), |
| 190 |
'\u{5c}' => self.dst.push_str("\\\\"), |
| 191 |
c if c <= '\u{1f}' || c == '\u{7f}' => { |
| 192 |
write!(self.dst, "\\u{:04X}", ch as u32).map_err(ser::Error::custom)?; |
| 193 |
} |
| 194 |
ch => self.dst.push(ch), |
| 195 |
} |
| 196 |
} |
| 197 |
self.dst.push('"'); |
| 198 |
Ok(()) |
| 199 |
} |
| 200 |
|
| 201 |
fn emit_table_header(&mut self, state: &State) -> Result<(), Error> { |
| 202 |
let array_of_tables = match *state { |
| 203 |
State::End => return Ok(()), |
| 204 |
State::Array { .. } => true, |
| 205 |
State::Table { .. } => false, |
| 206 |
}; |
| 207 |
|
| 208 |
// Unlike [..]s, we can't omit [[..]] ancestors, so be sure to emit |
| 209 |
// table headers for them. |
| 210 |
let mut p = state; |
| 211 |
if let State::Array { first, parent, .. } = *state { |
| 212 |
if first.get() { |
| 213 |
p = parent; |
| 214 |
} |
| 215 |
} |
| 216 |
while let State::Table { first, parent, .. } = *p { |
| 217 |
p = parent; |
| 218 |
if !first.get() { |
| 219 |
break; |
| 220 |
} |
| 221 |
if let State::Array { |
| 222 |
parent: &State::Table { .. }, |
| 223 |
.. |
| 224 |
} = *parent |
| 225 |
{ |
| 226 |
self.emit_table_header(parent)?; |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
match *state { |
| 232 |
State::Table { first, .. } => { |
| 233 |
if !first.get() { |
| 234 |
// Newline if we are a table that is not the first table in |
| 235 |
// the document. |
| 236 |
self.dst.push('\n'); |
| 237 |
} |
| 238 |
} |
| 239 |
State::Array { parent, first, .. } => { |
| 240 |
if !first.get() { |
| 241 |
// Always newline if we are not the first item in the |
| 242 |
// table-array |
| 243 |
self.dst.push('\n'); |
| 244 |
} else if let State::Table { first, .. } = *parent { |
| 245 |
if !first.get() { |
| 246 |
// Newline if we are not the first item in the document |
| 247 |
self.dst.push('\n'); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
State::End => {} |
| 252 |
} |
| 253 |
self.dst.push('['); |
| 254 |
if array_of_tables { |
| 255 |
self.dst.push('['); |
| 256 |
} |
| 257 |
self.emit_key_part(state)?; |
| 258 |
if array_of_tables { |
| 259 |
self.dst.push(']'); |
| 260 |
} |
| 261 |
self.dst.push_str("]\n"); |
| 262 |
Ok(()) |
| 263 |
} |
| 264 |
|
| 265 |
fn emit_key_part(&mut self, key: &State) -> Result<bool, Error> { |
| 266 |
match *key { |
| 267 |
State::Array { parent, .. } => self.emit_key_part(parent), |
| 268 |
State::End => Ok(true), |
| 269 |
State::Table { |
| 270 |
key, |
| 271 |
parent, |
| 272 |
table_emitted, |
| 273 |
.. |
| 274 |
} => { |
| 275 |
table_emitted.set(true); |
| 276 |
let first = self.emit_key_part(parent)?; |
| 277 |
if !first { |
| 278 |
self.dst.push('.'); |
| 279 |
} |
| 280 |
self.escape_key(key)?; |
| 281 |
Ok(false) |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
macro_rules! serialize_float { |
| 288 |
($this:expr, $v:expr) => {{ |
| 289 |
$this.emit_key(ArrayState::Started)?; |
| 290 |
match ($v.is_sign_negative(), $v.is_nan(), $v == 0.0) { |
| 291 |
(true, true, _) => write!($this.dst, "-nan"), |
| 292 |
(false, true, _) => write!($this.dst, "nan"), |
| 293 |
(true, false, true) => write!($this.dst, "-0.0"), |
| 294 |
(false, false, true) => write!($this.dst, "0.0"), |
| 295 |
(_, false, false) => write!($this.dst, "{}", $v).and_then(|_| { |
| 296 |
if $v % 1.0 == 0.0 { |
| 297 |
write!($this.dst, ".0") |
| 298 |
} else { |
| 299 |
Ok(()) |
| 300 |
} |
| 301 |
}), |
| 302 |
} |
| 303 |
.map_err(ser::Error::custom)?; |
| 304 |
|
| 305 |
if let State::Table { .. } = $this.state { |
| 306 |
$this.dst.push_str("\n"); |
| 307 |
} |
| 308 |
return Ok(()); |
| 309 |
}}; |
| 310 |
} |
| 311 |
|
| 312 |
impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { |
| 313 |
type Ok = (); |
| 314 |
type Error = Error; |
| 315 |
type SerializeSeq = SerializeSeq<'a, 'b>; |
| 316 |
type SerializeTuple = SerializeSeq<'a, 'b>; |
| 317 |
type SerializeTupleStruct = SerializeSeq<'a, 'b>; |
| 318 |
type SerializeTupleVariant = ser::Impossible<(), Error>; |
| 319 |
type SerializeMap = SerializeTable<'a, 'b>; |
| 320 |
type SerializeStruct = SerializeTable<'a, 'b>; |
| 321 |
type SerializeStructVariant = ser::Impossible<(), Error>; |
| 322 |
|
| 323 |
fn serialize_bool(self, v: bool) -> Result<(), Self::Error> { |
| 324 |
self.display(v, ArrayState::Started) |
| 325 |
} |
| 326 |
|
| 327 |
fn serialize_i8(self, v: i8) -> Result<(), Self::Error> { |
| 328 |
self.display(v, ArrayState::Started) |
| 329 |
} |
| 330 |
|
| 331 |
fn serialize_i16(self, v: i16) -> Result<(), Self::Error> { |
| 332 |
self.display(v, ArrayState::Started) |
| 333 |
} |
| 334 |
|
| 335 |
fn serialize_i32(self, v: i32) -> Result<(), Self::Error> { |
| 336 |
self.display(v, ArrayState::Started) |
| 337 |
} |
| 338 |
|
| 339 |
fn serialize_i64(self, v: i64) -> Result<(), Self::Error> { |
| 340 |
self.display(v, ArrayState::Started) |
| 341 |
} |
| 342 |
|
| 343 |
fn serialize_u8(self, v: u8) -> Result<(), Self::Error> { |
| 344 |
self.display(v, ArrayState::Started) |
| 345 |
} |
| 346 |
|
| 347 |
fn serialize_u16(self, v: u16) -> Result<(), Self::Error> { |
| 348 |
self.display(v, ArrayState::Started) |
| 349 |
} |
| 350 |
|
| 351 |
fn serialize_u32(self, v: u32) -> Result<(), Self::Error> { |
| 352 |
self.display(v, ArrayState::Started) |
| 353 |
} |
| 354 |
|
| 355 |
fn serialize_u64(self, v: u64) -> Result<(), Self::Error> { |
| 356 |
self.display(v, ArrayState::Started) |
| 357 |
} |
| 358 |
|
| 359 |
fn serialize_f32(self, v: f32) -> Result<(), Self::Error> { |
| 360 |
serialize_float!(self, v) |
| 361 |
} |
| 362 |
|
| 363 |
fn serialize_f64(self, v: f64) -> Result<(), Self::Error> { |
| 364 |
serialize_float!(self, v) |
| 365 |
} |
| 366 |
|
| 367 |
fn serialize_char(self, v: char) -> Result<(), Self::Error> { |
| 368 |
let mut buf = [0; 4]; |
| 369 |
self.serialize_str(v.encode_utf8(&mut buf)) |
| 370 |
} |
| 371 |
|
| 372 |
fn serialize_str(self, value: &str) -> Result<(), Self::Error> { |
| 373 |
self.emit_key(ArrayState::Started)?; |
| 374 |
self.emit_str(value)?; |
| 375 |
if let State::Table { .. } = self.state { |
| 376 |
self.dst.push('\n'); |
| 377 |
} |
| 378 |
Ok(()) |
| 379 |
} |
| 380 |
|
| 381 |
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> { |
| 382 |
value.serialize(self) |
| 383 |
} |
| 384 |
|
| 385 |
fn serialize_none(self) -> Result<(), Self::Error> { |
| 386 |
Err(Error::UnsupportedNone) |
| 387 |
} |
| 388 |
|
| 389 |
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Self::Error> |
| 390 |
where |
| 391 |
T: Serialize, |
| 392 |
{ |
| 393 |
value.serialize(self) |
| 394 |
} |
| 395 |
|
| 396 |
fn serialize_unit(self) -> Result<(), Self::Error> { |
| 397 |
Err(Error::UnsupportedType) |
| 398 |
} |
| 399 |
|
| 400 |
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> { |
| 401 |
Err(Error::UnsupportedType) |
| 402 |
} |
| 403 |
|
| 404 |
fn serialize_unit_variant( |
| 405 |
self, |
| 406 |
_name: &'static str, |
| 407 |
_variant_index: u32, |
| 408 |
variant: &'static str, |
| 409 |
) -> Result<(), Self::Error> { |
| 410 |
self.serialize_str(variant) |
| 411 |
} |
| 412 |
|
| 413 |
fn serialize_newtype_struct<T: ?Sized>( |
| 414 |
self, |
| 415 |
_name: &'static str, |
| 416 |
value: &T, |
| 417 |
) -> Result<(), Self::Error> |
| 418 |
where |
| 419 |
T: Serialize, |
| 420 |
{ |
| 421 |
value.serialize(self) |
| 422 |
} |
| 423 |
|
| 424 |
fn serialize_newtype_variant<T: ?Sized>( |
| 425 |
self, |
| 426 |
_name: &'static str, |
| 427 |
_variant_index: u32, |
| 428 |
_variant: &'static str, |
| 429 |
_value: &T, |
| 430 |
) -> Result<(), Self::Error> |
| 431 |
where |
| 432 |
T: Serialize, |
| 433 |
{ |
| 434 |
Err(Error::UnsupportedType) |
| 435 |
} |
| 436 |
|
| 437 |
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 438 |
self.array_type(ArrayState::Started); |
| 439 |
Ok(SerializeSeq { |
| 440 |
ser: self, |
| 441 |
first: Cell::new(true), |
| 442 |
type_: Cell::new(None), |
| 443 |
len, |
| 444 |
}) |
| 445 |
} |
| 446 |
|
| 447 |
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 448 |
self.serialize_seq(Some(len)) |
| 449 |
} |
| 450 |
|
| 451 |
fn serialize_tuple_struct( |
| 452 |
self, |
| 453 |
_name: &'static str, |
| 454 |
len: usize, |
| 455 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 456 |
self.serialize_seq(Some(len)) |
| 457 |
} |
| 458 |
|
| 459 |
fn serialize_tuple_variant( |
| 460 |
self, |
| 461 |
_name: &'static str, |
| 462 |
_variant_index: u32, |
| 463 |
_variant: &'static str, |
| 464 |
_len: usize, |
| 465 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 466 |
Err(Error::UnsupportedType) |
| 467 |
} |
| 468 |
|
| 469 |
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 470 |
self.array_type(ArrayState::StartedAsATable); |
| 471 |
Ok(SerializeTable { |
| 472 |
ser: self, |
| 473 |
key: String::new(), |
| 474 |
first: Cell::new(true), |
| 475 |
table_emitted: Cell::new(false), |
| 476 |
}) |
| 477 |
} |
| 478 |
|
| 479 |
fn serialize_struct( |
| 480 |
self, |
| 481 |
_name: &'static str, |
| 482 |
_len: usize, |
| 483 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 484 |
self.array_type(ArrayState::StartedAsATable); |
| 485 |
Ok(SerializeTable { |
| 486 |
ser: self, |
| 487 |
key: String::new(), |
| 488 |
first: Cell::new(true), |
| 489 |
table_emitted: Cell::new(false), |
| 490 |
}) |
| 491 |
} |
| 492 |
|
| 493 |
fn serialize_struct_variant( |
| 494 |
self, |
| 495 |
_name: &'static str, |
| 496 |
_variant_index: u32, |
| 497 |
_variant: &'static str, |
| 498 |
_len: usize, |
| 499 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 500 |
Err(Error::UnsupportedType) |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> { |
| 505 |
type Ok = (); |
| 506 |
type Error = Error; |
| 507 |
|
| 508 |
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
| 509 |
where |
| 510 |
T: Serialize, |
| 511 |
{ |
| 512 |
value.serialize(&mut Serializer { |
| 513 |
dst: &mut *self.ser.dst, |
| 514 |
state: State::Array { |
| 515 |
parent: &self.ser.state, |
| 516 |
first: &self.first, |
| 517 |
type_: &self.type_, |
| 518 |
len: self.len, |
| 519 |
}, |
| 520 |
})?; |
| 521 |
self.first.set(false); |
| 522 |
Ok(()) |
| 523 |
} |
| 524 |
|
| 525 |
fn end(self) -> Result<(), Error> { |
| 526 |
match self.type_.get() { |
| 527 |
Some(ArrayState::StartedAsATable) => return Ok(()), |
| 528 |
Some(ArrayState::Started) => self.ser.dst.push(']'), |
| 529 |
None => { |
| 530 |
assert!(self.first.get()); |
| 531 |
self.ser.emit_key(ArrayState::Started)?; |
| 532 |
self.ser.dst.push_str("[]"); |
| 533 |
} |
| 534 |
} |
| 535 |
if let State::Table { .. } = self.ser.state { |
| 536 |
self.ser.dst.push('\n'); |
| 537 |
} |
| 538 |
Ok(()) |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
impl<'a, 'b> ser::SerializeTuple for SerializeSeq<'a, 'b> { |
| 543 |
type Ok = (); |
| 544 |
type Error = Error; |
| 545 |
|
| 546 |
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
| 547 |
where |
| 548 |
T: Serialize, |
| 549 |
{ |
| 550 |
ser::SerializeSeq::serialize_element(self, value) |
| 551 |
} |
| 552 |
|
| 553 |
fn end(self) -> Result<(), Error> { |
| 554 |
ser::SerializeSeq::end(self) |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
impl<'a, 'b> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> { |
| 559 |
type Ok = (); |
| 560 |
type Error = Error; |
| 561 |
|
| 562 |
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
| 563 |
where |
| 564 |
T: Serialize, |
| 565 |
{ |
| 566 |
ser::SerializeSeq::serialize_element(self, value) |
| 567 |
} |
| 568 |
|
| 569 |
fn end(self) -> Result<(), Error> { |
| 570 |
ser::SerializeSeq::end(self) |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> { |
| 575 |
type Ok = (); |
| 576 |
type Error = Error; |
| 577 |
|
| 578 |
fn serialize_key<T: ?Sized>(&mut self, input: &T) -> Result<(), Error> |
| 579 |
where |
| 580 |
T: Serialize, |
| 581 |
{ |
| 582 |
self.key = input.serialize(StringExtractor)?; |
| 583 |
Ok(()) |
| 584 |
} |
| 585 |
|
| 586 |
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error> |
| 587 |
where |
| 588 |
T: Serialize, |
| 589 |
{ |
| 590 |
let res = value.serialize(&mut Serializer { |
| 591 |
dst: &mut *self.ser.dst, |
| 592 |
state: State::Table { |
| 593 |
key: &self.key, |
| 594 |
parent: &self.ser.state, |
| 595 |
first: &self.first, |
| 596 |
table_emitted: &self.table_emitted, |
| 597 |
}, |
| 598 |
}); |
| 599 |
match res { |
| 600 |
Ok(()) => self.first.set(false), |
| 601 |
Err(Error::UnsupportedNone) => {} |
| 602 |
Err(e) => return Err(e), |
| 603 |
} |
| 604 |
Ok(()) |
| 605 |
} |
| 606 |
|
| 607 |
fn end(self) -> Result<(), Error> { |
| 608 |
if self.first.get() { |
| 609 |
let state = self.ser.state.clone(); |
| 610 |
self.ser.emit_table_header(&state)?; |
| 611 |
} |
| 612 |
Ok(()) |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> { |
| 617 |
type Ok = (); |
| 618 |
type Error = Error; |
| 619 |
|
| 620 |
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error> |
| 621 |
where |
| 622 |
T: Serialize, |
| 623 |
{ |
| 624 |
let res = value.serialize(&mut Serializer { |
| 625 |
dst: &mut *self.ser.dst, |
| 626 |
state: State::Table { |
| 627 |
key, |
| 628 |
parent: &self.ser.state, |
| 629 |
first: &self.first, |
| 630 |
table_emitted: &self.table_emitted, |
| 631 |
}, |
| 632 |
}); |
| 633 |
match res { |
| 634 |
Ok(()) => self.first.set(false), |
| 635 |
Err(Error::UnsupportedNone) => {} |
| 636 |
Err(e) => return Err(e), |
| 637 |
} |
| 638 |
Ok(()) |
| 639 |
} |
| 640 |
|
| 641 |
fn end(self) -> Result<(), Error> { |
| 642 |
if self.first.get() { |
| 643 |
let state = self.ser.state.clone(); |
| 644 |
self.ser.emit_table_header(&state)?; |
| 645 |
} |
| 646 |
Ok(()) |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
struct StringExtractor; |
| 651 |
|
| 652 |
impl ser::Serializer for StringExtractor { |
| 653 |
type Ok = String; |
| 654 |
type Error = Error; |
| 655 |
type SerializeSeq = ser::Impossible<String, Error>; |
| 656 |
type SerializeTuple = ser::Impossible<String, Error>; |
| 657 |
type SerializeTupleStruct = ser::Impossible<String, Error>; |
| 658 |
type SerializeTupleVariant = ser::Impossible<String, Error>; |
| 659 |
type SerializeMap = ser::Impossible<String, Error>; |
| 660 |
type SerializeStruct = ser::Impossible<String, Error>; |
| 661 |
type SerializeStructVariant = ser::Impossible<String, Error>; |
| 662 |
|
| 663 |
fn serialize_bool(self, _v: bool) -> Result<String, Self::Error> { |
| 664 |
Err(Error::KeyNotString) |
| 665 |
} |
| 666 |
|
| 667 |
fn serialize_i8(self, _v: i8) -> Result<String, Self::Error> { |
| 668 |
Err(Error::KeyNotString) |
| 669 |
} |
| 670 |
|
| 671 |
fn serialize_i16(self, _v: i16) -> Result<String, Self::Error> { |
| 672 |
Err(Error::KeyNotString) |
| 673 |
} |
| 674 |
|
| 675 |
fn serialize_i32(self, _v: i32) -> Result<String, Self::Error> { |
| 676 |
Err(Error::KeyNotString) |
| 677 |
} |
| 678 |
|
| 679 |
fn serialize_i64(self, _v: i64) -> Result<String, Self::Error> { |
| 680 |
Err(Error::KeyNotString) |
| 681 |
} |
| 682 |
|
| 683 |
fn serialize_u8(self, _v: u8) -> Result<String, Self::Error> { |
| 684 |
Err(Error::KeyNotString) |
| 685 |
} |
| 686 |
|
| 687 |
fn serialize_u16(self, _v: u16) -> Result<String, Self::Error> { |
| 688 |
Err(Error::KeyNotString) |
| 689 |
} |
| 690 |
|
| 691 |
fn serialize_u32(self, _v: u32) -> Result<String, Self::Error> { |
| 692 |
Err(Error::KeyNotString) |
| 693 |
} |
| 694 |
|
| 695 |
fn serialize_u64(self, _v: u64) -> Result<String, Self::Error> { |
| 696 |
Err(Error::KeyNotString) |
| 697 |
} |
| 698 |
|
| 699 |
fn serialize_f32(self, _v: f32) -> Result<String, Self::Error> { |
| 700 |
Err(Error::KeyNotString) |
| 701 |
} |
| 702 |
|
| 703 |
fn serialize_f64(self, _v: f64) -> Result<String, Self::Error> { |
| 704 |
Err(Error::KeyNotString) |
| 705 |
} |
| 706 |
|
| 707 |
fn serialize_char(self, _v: char) -> Result<String, Self::Error> { |
| 708 |
Err(Error::KeyNotString) |
| 709 |
} |
| 710 |
|
| 711 |
fn serialize_str(self, value: &str) -> Result<String, Self::Error> { |
| 712 |
Ok(value.to_string()) |
| 713 |
} |
| 714 |
|
| 715 |
fn serialize_bytes(self, _value: &[u8]) -> Result<String, Self::Error> { |
| 716 |
Err(Error::KeyNotString) |
| 717 |
} |
| 718 |
|
| 719 |
fn serialize_none(self) -> Result<String, Self::Error> { |
| 720 |
Err(Error::KeyNotString) |
| 721 |
} |
| 722 |
|
| 723 |
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<String, Self::Error> |
| 724 |
where |
| 725 |
T: Serialize, |
| 726 |
{ |
| 727 |
Err(Error::KeyNotString) |
| 728 |
} |
| 729 |
|
| 730 |
fn serialize_unit(self) -> Result<String, Self::Error> { |
| 731 |
Err(Error::KeyNotString) |
| 732 |
} |
| 733 |
|
| 734 |
fn serialize_unit_struct(self, _name: &'static str) -> Result<String, Self::Error> { |
| 735 |
Err(Error::KeyNotString) |
| 736 |
} |
| 737 |
|
| 738 |
fn serialize_unit_variant( |
| 739 |
self, |
| 740 |
_name: &'static str, |
| 741 |
_variant_index: u32, |
| 742 |
_variant: &'static str, |
| 743 |
) -> Result<String, Self::Error> { |
| 744 |
Err(Error::KeyNotString) |
| 745 |
} |
| 746 |
|
| 747 |
fn serialize_newtype_struct<T: ?Sized>( |
| 748 |
self, |
| 749 |
_name: &'static str, |
| 750 |
value: &T, |
| 751 |
) -> Result<String, Self::Error> |
| 752 |
where |
| 753 |
T: Serialize, |
| 754 |
{ |
| 755 |
value.serialize(self) |
| 756 |
} |
| 757 |
|
| 758 |
fn serialize_newtype_variant<T: ?Sized>( |
| 759 |
self, |
| 760 |
_name: &'static str, |
| 761 |
_variant_index: u32, |
| 762 |
_variant: &'static str, |
| 763 |
_value: &T, |
| 764 |
) -> Result<String, Self::Error> |
| 765 |
where |
| 766 |
T: Serialize, |
| 767 |
{ |
| 768 |
Err(Error::KeyNotString) |
| 769 |
} |
| 770 |
|
| 771 |
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
| 772 |
Err(Error::KeyNotString) |
| 773 |
} |
| 774 |
|
| 775 |
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
| 776 |
Err(Error::KeyNotString) |
| 777 |
} |
| 778 |
|
| 779 |
fn serialize_tuple_struct( |
| 780 |
self, |
| 781 |
_name: &'static str, |
| 782 |
_len: usize, |
| 783 |
) -> Result<Self::SerializeTupleStruct, Self::Error> { |
| 784 |
Err(Error::KeyNotString) |
| 785 |
} |
| 786 |
|
| 787 |
fn serialize_tuple_variant( |
| 788 |
self, |
| 789 |
_name: &'static str, |
| 790 |
_variant_index: u32, |
| 791 |
_variant: &'static str, |
| 792 |
_len: usize, |
| 793 |
) -> Result<Self::SerializeTupleVariant, Self::Error> { |
| 794 |
Err(Error::KeyNotString) |
| 795 |
} |
| 796 |
|
| 797 |
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
| 798 |
Err(Error::KeyNotString) |
| 799 |
} |
| 800 |
|
| 801 |
fn serialize_struct( |
| 802 |
self, |
| 803 |
_name: &'static str, |
| 804 |
_len: usize, |
| 805 |
) -> Result<Self::SerializeStruct, Self::Error> { |
| 806 |
Err(Error::KeyNotString) |
| 807 |
} |
| 808 |
|
| 809 |
fn serialize_struct_variant( |
| 810 |
self, |
| 811 |
_name: &'static str, |
| 812 |
_variant_index: u32, |
| 813 |
_variant: &'static str, |
| 814 |
_len: usize, |
| 815 |
) -> Result<Self::SerializeStructVariant, Self::Error> { |
| 816 |
Err(Error::KeyNotString) |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
impl Display for Error { |
| 821 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 822 |
match *self { |
| 823 |
Error::UnsupportedType => "unsupported Rust type".fmt(f), |
| 824 |
Error::KeyNotString => "map key was not a string".fmt(f), |
| 825 |
Error::ValueAfterTable => "values must be emitted before tables".fmt(f), |
| 826 |
Error::UnsupportedNone => "unsupported None value".fmt(f), |
| 827 |
Error::Custom(ref s) => s.fmt(f), |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
impl error::Error for Error {} |
| 833 |
|
| 834 |
impl ser::Error for Error { |
| 835 |
fn custom<T: Display>(msg: T) -> Error { |
| 836 |
Error::Custom(msg.to_string()) |
| 837 |
} |
| 838 |
} |