1use serde::ser::{self, Serialize};
2use std::cell::Cell;
3use std::error;
4use 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.
11pub fn to_string<T>(value: &T) -> Result<String, crate::Error>
12where
13 T: ?Sized + 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)]
21pub(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
46struct Serializer<'a> {
47 dst: &'a mut String,
48 state: State<'a>,
49}
50
51#[derive(Debug, Copy, Clone)]
52enum ArrayState {
53 Started,
54 StartedAsATable,
55}
56
57#[derive(Debug, Clone)]
58enum 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
74struct SerializeSeq<'a, 'b> {
75 ser: &'b mut Serializer<'a>,
76 first: Cell<bool>,
77 type_: Cell<Option<ArrayState>>,
78 len: Option<usize>,
79}
80
81struct SerializeTable<'a, 'b> {
82 ser: &'b mut Serializer<'a>,
83 key: String,
84 first: Cell<bool>,
85 table_emitted: Cell<bool>,
86}
87
88impl<'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
287macro_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, _) => write!($this.dst, "nan"),
292 (true, false, true) => write!($this.dst, "-0.0"),
293 (false, false, true) => write!($this.dst, "0.0"),
294 (_, false, false) => write!($this.dst, "{}", $v).and_then(|()| {
295 if $v % 1.0 == 0.0 {
296 write!($this.dst, ".0")
297 } else {
298 Ok(())
299 }
300 }),
301 }
302 .map_err(ser::Error::custom)?;
303
304 if let State::Table { .. } = $this.state {
305 $this.dst.push_str("\n");
306 }
307 return Ok(());
308 }};
309}
310
311impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
312 type Ok = ();
313 type Error = Error;
314 type SerializeSeq = SerializeSeq<'a, 'b>;
315 type SerializeTuple = SerializeSeq<'a, 'b>;
316 type SerializeTupleStruct = SerializeSeq<'a, 'b>;
317 type SerializeTupleVariant = ser::Impossible<(), Error>;
318 type SerializeMap = SerializeTable<'a, 'b>;
319 type SerializeStruct = SerializeTable<'a, 'b>;
320 type SerializeStructVariant = ser::Impossible<(), Error>;
321
322 fn serialize_bool(self, v: bool) -> Result<(), Self::Error> {
323 self.display(v, ArrayState::Started)
324 }
325
326 fn serialize_i8(self, v: i8) -> Result<(), Self::Error> {
327 self.display(v, ArrayState::Started)
328 }
329
330 fn serialize_i16(self, v: i16) -> Result<(), Self::Error> {
331 self.display(v, ArrayState::Started)
332 }
333
334 fn serialize_i32(self, v: i32) -> Result<(), Self::Error> {
335 self.display(v, ArrayState::Started)
336 }
337
338 fn serialize_i64(self, v: i64) -> Result<(), Self::Error> {
339 self.display(v, ArrayState::Started)
340 }
341
342 fn serialize_u8(self, v: u8) -> Result<(), Self::Error> {
343 self.display(v, ArrayState::Started)
344 }
345
346 fn serialize_u16(self, v: u16) -> Result<(), Self::Error> {
347 self.display(v, ArrayState::Started)
348 }
349
350 fn serialize_u32(self, v: u32) -> Result<(), Self::Error> {
351 self.display(v, ArrayState::Started)
352 }
353
354 fn serialize_u64(self, v: u64) -> Result<(), Self::Error> {
355 self.display(v, ArrayState::Started)
356 }
357
358 fn serialize_f32(self, v: f32) -> Result<(), Self::Error> {
359 serialize_float!(self, v)
360 }
361
362 fn serialize_f64(self, v: f64) -> Result<(), Self::Error> {
363 serialize_float!(self, v)
364 }
365
366 fn serialize_char(self, v: char) -> Result<(), Self::Error> {
367 let mut buf = [0; 4];
368 self.serialize_str(v.encode_utf8(&mut buf))
369 }
370
371 fn serialize_str(self, value: &str) -> Result<(), Self::Error> {
372 self.emit_key(ArrayState::Started)?;
373 self.emit_str(value)?;
374 if let State::Table { .. } = self.state {
375 self.dst.push('\n');
376 }
377 Ok(())
378 }
379
380 fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
381 value.serialize(self)
382 }
383
384 fn serialize_none(self) -> Result<(), Self::Error> {
385 Err(Error::UnsupportedNone)
386 }
387
388 fn serialize_some<T>(self, value: &T) -> Result<(), Self::Error>
389 where
390 T: ?Sized + Serialize,
391 {
392 value.serialize(self)
393 }
394
395 fn serialize_unit(self) -> Result<(), Self::Error> {
396 Err(Error::UnsupportedType)
397 }
398
399 fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
400 Err(Error::UnsupportedType)
401 }
402
403 fn serialize_unit_variant(
404 self,
405 _name: &'static str,
406 _variant_index: u32,
407 variant: &'static str,
408 ) -> Result<(), Self::Error> {
409 self.serialize_str(variant)
410 }
411
412 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<(), Self::Error>
413 where
414 T: ?Sized + Serialize,
415 {
416 value.serialize(self)
417 }
418
419 fn serialize_newtype_variant<T>(
420 self,
421 _name: &'static str,
422 _variant_index: u32,
423 _variant: &'static str,
424 _value: &T,
425 ) -> Result<(), Self::Error>
426 where
427 T: ?Sized + Serialize,
428 {
429 Err(Error::UnsupportedType)
430 }
431
432 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
433 self.array_type(ArrayState::Started);
434 Ok(SerializeSeq {
435 ser: self,
436 first: Cell::new(true),
437 type_: Cell::new(None),
438 len,
439 })
440 }
441
442 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
443 self.serialize_seq(Some(len))
444 }
445
446 fn serialize_tuple_struct(
447 self,
448 _name: &'static str,
449 len: usize,
450 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
451 self.serialize_seq(Some(len))
452 }
453
454 fn serialize_tuple_variant(
455 self,
456 _name: &'static str,
457 _variant_index: u32,
458 _variant: &'static str,
459 _len: usize,
460 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
461 Err(Error::UnsupportedType)
462 }
463
464 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
465 self.array_type(ArrayState::StartedAsATable);
466 Ok(SerializeTable {
467 ser: self,
468 key: String::new(),
469 first: Cell::new(true),
470 table_emitted: Cell::new(false),
471 })
472 }
473
474 fn serialize_struct(
475 self,
476 _name: &'static str,
477 _len: usize,
478 ) -> Result<Self::SerializeStruct, Self::Error> {
479 self.array_type(ArrayState::StartedAsATable);
480 Ok(SerializeTable {
481 ser: self,
482 key: String::new(),
483 first: Cell::new(true),
484 table_emitted: Cell::new(false),
485 })
486 }
487
488 fn serialize_struct_variant(
489 self,
490 _name: &'static str,
491 _variant_index: u32,
492 _variant: &'static str,
493 _len: usize,
494 ) -> Result<Self::SerializeStructVariant, Self::Error> {
495 Err(Error::UnsupportedType)
496 }
497}
498
499impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
500 type Ok = ();
501 type Error = Error;
502
503 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
504 where
505 T: ?Sized + Serialize,
506 {
507 value.serialize(&mut Serializer {
508 dst: &mut *self.ser.dst,
509 state: State::Array {
510 parent: &self.ser.state,
511 first: &self.first,
512 type_: &self.type_,
513 len: self.len,
514 },
515 })?;
516 self.first.set(false);
517 Ok(())
518 }
519
520 fn end(self) -> Result<(), Error> {
521 match self.type_.get() {
522 Some(ArrayState::StartedAsATable) => return Ok(()),
523 Some(ArrayState::Started) => self.ser.dst.push(']'),
524 None => {
525 assert!(self.first.get());
526 self.ser.emit_key(ArrayState::Started)?;
527 self.ser.dst.push_str("[]");
528 }
529 }
530 if let State::Table { .. } = self.ser.state {
531 self.ser.dst.push('\n');
532 }
533 Ok(())
534 }
535}
536
537impl<'a, 'b> ser::SerializeTuple for SerializeSeq<'a, 'b> {
538 type Ok = ();
539 type Error = Error;
540
541 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
542 where
543 T: ?Sized + Serialize,
544 {
545 ser::SerializeSeq::serialize_element(self, value)
546 }
547
548 fn end(self) -> Result<(), Error> {
549 ser::SerializeSeq::end(self)
550 }
551}
552
553impl<'a, 'b> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> {
554 type Ok = ();
555 type Error = Error;
556
557 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
558 where
559 T: ?Sized + Serialize,
560 {
561 ser::SerializeSeq::serialize_element(self, value)
562 }
563
564 fn end(self) -> Result<(), Error> {
565 ser::SerializeSeq::end(self)
566 }
567}
568
569impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
570 type Ok = ();
571 type Error = Error;
572
573 fn serialize_key<T>(&mut self, input: &T) -> Result<(), Error>
574 where
575 T: ?Sized + Serialize,
576 {
577 self.key = input.serialize(StringExtractor)?;
578 Ok(())
579 }
580
581 fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error>
582 where
583 T: ?Sized + Serialize,
584 {
585 let res = value.serialize(&mut Serializer {
586 dst: &mut *self.ser.dst,
587 state: State::Table {
588 key: &self.key,
589 parent: &self.ser.state,
590 first: &self.first,
591 table_emitted: &self.table_emitted,
592 },
593 });
594 match res {
595 Ok(()) => self.first.set(false),
596 Err(Error::UnsupportedNone) => {}
597 Err(e) => return Err(e),
598 }
599 Ok(())
600 }
601
602 fn end(self) -> Result<(), Error> {
603 if self.first.get() {
604 let state = self.ser.state.clone();
605 self.ser.emit_table_header(&state)?;
606 }
607 Ok(())
608 }
609}
610
611impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
612 type Ok = ();
613 type Error = Error;
614
615 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
616 where
617 T: ?Sized + Serialize,
618 {
619 let res = value.serialize(&mut Serializer {
620 dst: &mut *self.ser.dst,
621 state: State::Table {
622 key,
623 parent: &self.ser.state,
624 first: &self.first,
625 table_emitted: &self.table_emitted,
626 },
627 });
628 match res {
629 Ok(()) => self.first.set(false),
630 Err(Error::UnsupportedNone) => {}
631 Err(e) => return Err(e),
632 }
633 Ok(())
634 }
635
636 fn end(self) -> Result<(), Error> {
637 if self.first.get() {
638 let state = self.ser.state.clone();
639 self.ser.emit_table_header(&state)?;
640 }
641 Ok(())
642 }
643}
644
645struct StringExtractor;
646
647impl ser::Serializer for StringExtractor {
648 type Ok = String;
649 type Error = Error;
650 type SerializeSeq = ser::Impossible<String, Error>;
651 type SerializeTuple = ser::Impossible<String, Error>;
652 type SerializeTupleStruct = ser::Impossible<String, Error>;
653 type SerializeTupleVariant = ser::Impossible<String, Error>;
654 type SerializeMap = ser::Impossible<String, Error>;
655 type SerializeStruct = ser::Impossible<String, Error>;
656 type SerializeStructVariant = ser::Impossible<String, Error>;
657
658 fn serialize_bool(self, _v: bool) -> Result<String, Self::Error> {
659 Err(Error::KeyNotString)
660 }
661
662 fn serialize_i8(self, _v: i8) -> Result<String, Self::Error> {
663 Err(Error::KeyNotString)
664 }
665
666 fn serialize_i16(self, _v: i16) -> Result<String, Self::Error> {
667 Err(Error::KeyNotString)
668 }
669
670 fn serialize_i32(self, _v: i32) -> Result<String, Self::Error> {
671 Err(Error::KeyNotString)
672 }
673
674 fn serialize_i64(self, _v: i64) -> Result<String, Self::Error> {
675 Err(Error::KeyNotString)
676 }
677
678 fn serialize_u8(self, _v: u8) -> Result<String, Self::Error> {
679 Err(Error::KeyNotString)
680 }
681
682 fn serialize_u16(self, _v: u16) -> Result<String, Self::Error> {
683 Err(Error::KeyNotString)
684 }
685
686 fn serialize_u32(self, _v: u32) -> Result<String, Self::Error> {
687 Err(Error::KeyNotString)
688 }
689
690 fn serialize_u64(self, _v: u64) -> Result<String, Self::Error> {
691 Err(Error::KeyNotString)
692 }
693
694 fn serialize_f32(self, _v: f32) -> Result<String, Self::Error> {
695 Err(Error::KeyNotString)
696 }
697
698 fn serialize_f64(self, _v: f64) -> Result<String, Self::Error> {
699 Err(Error::KeyNotString)
700 }
701
702 fn serialize_char(self, _v: char) -> Result<String, Self::Error> {
703 Err(Error::KeyNotString)
704 }
705
706 fn serialize_str(self, value: &str) -> Result<String, Self::Error> {
707 Ok(value.to_string())
708 }
709
710 fn serialize_bytes(self, _value: &[u8]) -> Result<String, Self::Error> {
711 Err(Error::KeyNotString)
712 }
713
714 fn serialize_none(self) -> Result<String, Self::Error> {
715 Err(Error::KeyNotString)
716 }
717
718 fn serialize_some<T>(self, _value: &T) -> Result<String, Self::Error>
719 where
720 T: ?Sized + Serialize,
721 {
722 Err(Error::KeyNotString)
723 }
724
725 fn serialize_unit(self) -> Result<String, Self::Error> {
726 Err(Error::KeyNotString)
727 }
728
729 fn serialize_unit_struct(self, _name: &'static str) -> Result<String, Self::Error> {
730 Err(Error::KeyNotString)
731 }
732
733 fn serialize_unit_variant(
734 self,
735 _name: &'static str,
736 _variant_index: u32,
737 _variant: &'static str,
738 ) -> Result<String, Self::Error> {
739 Err(Error::KeyNotString)
740 }
741
742 fn serialize_newtype_struct<T>(
743 self,
744 _name: &'static str,
745 value: &T,
746 ) -> Result<String, Self::Error>
747 where
748 T: ?Sized + Serialize,
749 {
750 value.serialize(self)
751 }
752
753 fn serialize_newtype_variant<T>(
754 self,
755 _name: &'static str,
756 _variant_index: u32,
757 _variant: &'static str,
758 _value: &T,
759 ) -> Result<String, Self::Error>
760 where
761 T: ?Sized + Serialize,
762 {
763 Err(Error::KeyNotString)
764 }
765
766 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
767 Err(Error::KeyNotString)
768 }
769
770 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
771 Err(Error::KeyNotString)
772 }
773
774 fn serialize_tuple_struct(
775 self,
776 _name: &'static str,
777 _len: usize,
778 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
779 Err(Error::KeyNotString)
780 }
781
782 fn serialize_tuple_variant(
783 self,
784 _name: &'static str,
785 _variant_index: u32,
786 _variant: &'static str,
787 _len: usize,
788 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
789 Err(Error::KeyNotString)
790 }
791
792 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
793 Err(Error::KeyNotString)
794 }
795
796 fn serialize_struct(
797 self,
798 _name: &'static str,
799 _len: usize,
800 ) -> Result<Self::SerializeStruct, Self::Error> {
801 Err(Error::KeyNotString)
802 }
803
804 fn serialize_struct_variant(
805 self,
806 _name: &'static str,
807 _variant_index: u32,
808 _variant: &'static str,
809 _len: usize,
810 ) -> Result<Self::SerializeStructVariant, Self::Error> {
811 Err(Error::KeyNotString)
812 }
813}
814
815impl Display for Error {
816 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
817 match *self {
818 Error::UnsupportedType => "unsupported Rust type".fmt(f),
819 Error::KeyNotString => "map key was not a string".fmt(f),
820 Error::ValueAfterTable => "values must be emitted before tables".fmt(f),
821 Error::UnsupportedNone => "unsupported None value".fmt(f),
822 Error::Custom(ref s) => s.fmt(f),
823 }
824 }
825}
826
827impl error::Error for Error {}
828
829impl ser::Error for Error {
830 fn custom<T: Display>(msg: T) -> Error {
831 Error::Custom(msg.to_string())
832 }
833}
834