2017-01-29 18:53:20 -06:00
|
|
|
//! Serializing Rust structures into TOML.
|
|
|
|
//!
|
|
|
|
//! This module contains all the Serde support for serializing Rust structures
|
|
|
|
//! into TOML documents (as strings). Note that some top-level functions here
|
|
|
|
//! are also provided at the top of the crate.
|
2017-02-10 17:29:36 -06:00
|
|
|
//!
|
|
|
|
//! Note that the TOML format has a restriction that if a table itself contains
|
|
|
|
//! tables, all keys with non-table values must be emitted first. This is
|
|
|
|
//! typically easy to ensure happens when you're defining a `struct` as you can
|
|
|
|
//! reorder the fields manually, but when working with maps (such as `BTreeMap`
|
|
|
|
//! or `HashMap`) this can lead to serialization errors. In those situations you
|
|
|
|
//! may use the `tables_last` function in this module like so:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2019-05-08 19:37:38 -05:00
|
|
|
//! # use serde_derive::Serialize;
|
2017-02-10 17:29:36 -06:00
|
|
|
//! # use std::collections::HashMap;
|
|
|
|
//! #[derive(Serialize)]
|
|
|
|
//! struct Manifest {
|
|
|
|
//! package: Package,
|
|
|
|
//! #[serde(serialize_with = "toml::ser::tables_last")]
|
|
|
|
//! dependencies: HashMap<String, Dependency>,
|
|
|
|
//! }
|
|
|
|
//! # type Package = String;
|
|
|
|
//! # type Dependency = String;
|
|
|
|
//! # fn main() {}
|
|
|
|
//! ```
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
use std::cell::Cell;
|
2017-02-10 17:29:36 -06:00
|
|
|
use std::error;
|
|
|
|
use std::fmt::{self, Write};
|
|
|
|
use std::marker;
|
2017-07-28 16:12:39 -05:00
|
|
|
use std::rc::Rc;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2019-05-08 14:12:14 -05:00
|
|
|
use crate::datetime;
|
2018-12-17 19:45:35 -06:00
|
|
|
use serde::ser;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
/// Serialize the given data structure as a TOML byte vector.
|
|
|
|
///
|
|
|
|
/// Serialization can fail if `T`'s implementation of `Serialize` decides to
|
|
|
|
/// fail, if `T` contains a map with non-string keys, or if `T` attempts to
|
|
|
|
/// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
|
|
|
|
pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
to_string(value).map(|e| e.into_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialize the given data structure as a String of TOML.
|
|
|
|
///
|
|
|
|
/// Serialization can fail if `T`'s implementation of `Serialize` decides to
|
|
|
|
/// fail, if `T` contains a map with non-string keys, or if `T` attempts to
|
|
|
|
/// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
|
2017-05-10 10:39:19 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-05-08 19:37:38 -05:00
|
|
|
/// use serde_derive::Serialize;
|
2017-05-10 10:39:19 -05:00
|
|
|
///
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct Config {
|
|
|
|
/// database: Database,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct Database {
|
|
|
|
/// ip: String,
|
|
|
|
/// port: Vec<u16>,
|
|
|
|
/// connection_max: u32,
|
|
|
|
/// enabled: bool,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let config = Config {
|
|
|
|
/// database: Database {
|
|
|
|
/// ip: "192.168.1.1".to_string(),
|
|
|
|
/// port: vec![8001, 8002, 8003],
|
|
|
|
/// connection_max: 5000,
|
|
|
|
/// enabled: false,
|
|
|
|
/// },
|
|
|
|
/// };
|
2017-05-30 18:30:19 -05:00
|
|
|
///
|
2017-05-10 10:39:19 -05:00
|
|
|
/// let toml = toml::to_string(&config).unwrap();
|
|
|
|
/// println!("{}", toml)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-01-29 18:53:20 -06:00
|
|
|
pub fn to_string<T: ?Sized>(value: &T) -> Result<String, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
let mut dst = String::with_capacity(128);
|
|
|
|
value.serialize(&mut Serializer::new(&mut dst))?;
|
|
|
|
Ok(dst)
|
|
|
|
}
|
|
|
|
|
2017-07-22 16:11:23 -05:00
|
|
|
/// Serialize the given data structure as a "pretty" String of TOML.
|
|
|
|
///
|
|
|
|
/// This is identical to `to_string` except the output string has a more
|
|
|
|
/// "pretty" output. See `Serializer::pretty` for more details.
|
|
|
|
pub fn to_string_pretty<T: ?Sized>(value: &T) -> Result<String, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-07-22 16:11:23 -05:00
|
|
|
{
|
|
|
|
let mut dst = String::with_capacity(128);
|
|
|
|
value.serialize(&mut Serializer::pretty(&mut dst))?;
|
|
|
|
Ok(dst)
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
/// Errors that can occur when serializing a type.
|
2017-02-10 08:37:47 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2017-01-29 18:53:20 -06:00
|
|
|
pub enum Error {
|
|
|
|
/// Indicates that a Rust type was requested to be serialized but it was not
|
|
|
|
/// supported.
|
|
|
|
///
|
|
|
|
/// Currently the TOML format does not support serializing types such as
|
|
|
|
/// enums, tuples and tuple structs.
|
|
|
|
UnsupportedType,
|
|
|
|
|
|
|
|
/// The key of all TOML maps must be strings, but serialization was
|
|
|
|
/// attempted where the key of a map was not a string.
|
|
|
|
KeyNotString,
|
|
|
|
|
2017-06-01 16:47:24 -05:00
|
|
|
/// An error that we never omit but keep for backwards compatibility
|
|
|
|
#[doc(hidden)]
|
|
|
|
KeyNewline,
|
|
|
|
|
2019-11-22 10:28:29 -06:00
|
|
|
/// An array had to be homogenous, but now it is allowed to be heterogenous.
|
|
|
|
#[doc(hidden)]
|
2017-01-29 18:53:20 -06:00
|
|
|
ArrayMixedType,
|
|
|
|
|
|
|
|
/// All values in a TOML table must be emitted before further tables are
|
|
|
|
/// emitted. If a value is emitted *after* a table then this error is
|
|
|
|
/// generated.
|
|
|
|
ValueAfterTable,
|
|
|
|
|
|
|
|
/// A serialized date was invalid.
|
|
|
|
DateInvalid,
|
|
|
|
|
2017-05-30 18:30:19 -05:00
|
|
|
/// A serialized number was invalid.
|
|
|
|
NumberInvalid,
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
/// None was attempted to be serialized, but it's not supported.
|
|
|
|
UnsupportedNone,
|
|
|
|
|
|
|
|
/// A custom error which could be generated when serializing a particular
|
|
|
|
/// type.
|
|
|
|
Custom(String),
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
__Nonexhaustive,
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:38:48 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2017-07-20 13:47:51 -05:00
|
|
|
/// Internal place for holding array setings
|
|
|
|
struct ArraySettings {
|
|
|
|
indent: usize,
|
2017-07-09 16:20:29 -05:00
|
|
|
trailing_comma: bool,
|
2017-07-09 15:38:48 -05:00
|
|
|
}
|
|
|
|
|
2017-07-20 13:47:51 -05:00
|
|
|
impl ArraySettings {
|
|
|
|
fn pretty() -> ArraySettings {
|
|
|
|
ArraySettings {
|
|
|
|
indent: 4,
|
|
|
|
trailing_comma: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:12:39 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2017-08-13 17:15:15 -05:00
|
|
|
/// String settings
|
|
|
|
struct StringSettings {
|
|
|
|
/// Whether to use literal strings when possible
|
|
|
|
literal: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StringSettings {
|
|
|
|
fn pretty() -> StringSettings {
|
2018-12-17 19:45:35 -06:00
|
|
|
StringSettings { literal: true }
|
2017-08-13 17:15:15 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 16:12:39 -05:00
|
|
|
|
2017-07-09 15:38:48 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2017-07-20 13:47:51 -05:00
|
|
|
/// Internal struct for holding serialization settings
|
|
|
|
struct Settings {
|
2017-07-09 15:38:48 -05:00
|
|
|
array: Option<ArraySettings>,
|
2017-07-28 16:12:39 -05:00
|
|
|
string: Option<StringSettings>,
|
2017-07-09 15:38:48 -05:00
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
/// Serialization implementation for TOML.
|
|
|
|
///
|
|
|
|
/// This structure implements serialization support for TOML to serialize an
|
|
|
|
/// arbitrary type to TOML. Note that the TOML format does not support all
|
|
|
|
/// datatypes in Rust, such as enums, tuples, and tuple structs. These types
|
|
|
|
/// will generate an error when serialized.
|
|
|
|
///
|
|
|
|
/// Currently a serializer always writes its output to an in-memory `String`,
|
|
|
|
/// which is passed in when creating the serializer itself.
|
|
|
|
pub struct Serializer<'a> {
|
|
|
|
dst: &'a mut String,
|
|
|
|
state: State<'a>,
|
2017-07-28 16:12:39 -05:00
|
|
|
settings: Rc<Settings>,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-11-22 10:28:29 -06:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum ArrayState {
|
|
|
|
Started,
|
|
|
|
StartedAsATable,
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum State<'a> {
|
|
|
|
Table {
|
|
|
|
key: &'a str,
|
|
|
|
parent: &'a State<'a>,
|
|
|
|
first: &'a Cell<bool>,
|
|
|
|
table_emitted: &'a Cell<bool>,
|
|
|
|
},
|
|
|
|
Array {
|
|
|
|
parent: &'a State<'a>,
|
|
|
|
first: &'a Cell<bool>,
|
2019-11-22 10:28:29 -06:00
|
|
|
type_: &'a Cell<Option<ArrayState>>,
|
2017-07-27 23:44:58 -05:00
|
|
|
len: Option<usize>,
|
2017-01-29 18:53:20 -06:00
|
|
|
},
|
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-08-12 11:12:39 -05:00
|
|
|
pub struct SerializeSeq<'a, 'b> {
|
2017-01-29 18:53:20 -06:00
|
|
|
ser: &'b mut Serializer<'a>,
|
|
|
|
first: Cell<bool>,
|
2019-11-22 10:28:29 -06:00
|
|
|
type_: Cell<Option<ArrayState>>,
|
2017-07-27 23:44:58 -05:00
|
|
|
len: Option<usize>,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-08-12 11:12:39 -05:00
|
|
|
pub enum SerializeTable<'a, 'b> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Datetime(&'b mut Serializer<'a>),
|
|
|
|
Table {
|
|
|
|
ser: &'b mut Serializer<'a>,
|
|
|
|
key: String,
|
|
|
|
first: Cell<bool>,
|
|
|
|
table_emitted: Cell<bool>,
|
2018-12-17 19:45:35 -06:00
|
|
|
},
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Serializer<'a> {
|
|
|
|
/// Creates a new serializer which will emit TOML into the buffer provided.
|
|
|
|
///
|
|
|
|
/// The serializer can then be used to serialize a type after which the data
|
|
|
|
/// will be present in `dst`.
|
|
|
|
pub fn new(dst: &'a mut String) -> Serializer<'a> {
|
|
|
|
Serializer {
|
2019-08-14 21:52:24 -05:00
|
|
|
dst,
|
2017-01-29 18:53:20 -06:00
|
|
|
state: State::End,
|
2017-07-28 16:12:39 -05:00
|
|
|
settings: Rc::new(Settings::default()),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 13:47:51 -05:00
|
|
|
/// Instantiate a "pretty" formatter
|
|
|
|
///
|
2017-07-22 16:11:23 -05:00
|
|
|
/// By default this will use:
|
|
|
|
///
|
|
|
|
/// - pretty strings: strings with newlines will use the `'''` syntax. See
|
|
|
|
/// `Serializer::pretty_string`
|
|
|
|
/// - pretty arrays: each item in arrays will be on a newline, have an indentation of 4 and
|
|
|
|
/// have a trailing comma. See `Serializer::pretty_array`
|
2017-07-09 15:58:48 -05:00
|
|
|
pub fn pretty(dst: &'a mut String) -> Serializer<'a> {
|
|
|
|
Serializer {
|
2019-08-14 21:52:24 -05:00
|
|
|
dst,
|
2017-07-09 15:58:48 -05:00
|
|
|
state: State::End,
|
2017-07-28 16:12:39 -05:00
|
|
|
settings: Rc::new(Settings {
|
2017-07-20 13:47:51 -05:00
|
|
|
array: Some(ArraySettings::pretty()),
|
2017-08-13 17:15:15 -05:00
|
|
|
string: Some(StringSettings::pretty()),
|
2017-07-28 16:12:39 -05:00
|
|
|
}),
|
2017-07-09 15:58:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 13:47:51 -05:00
|
|
|
/// Enable or Disable pretty strings
|
2017-07-22 16:11:23 -05:00
|
|
|
///
|
2017-07-28 16:12:39 -05:00
|
|
|
/// If enabled, literal strings will be used when possible and strings with
|
|
|
|
/// one or more newlines will use triple quotes (i.e.: `'''` or `"""`)
|
2017-07-22 16:11:23 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Instead of:
|
|
|
|
///
|
2017-07-30 11:18:00 -05:00
|
|
|
/// ```toml,ignore
|
2017-07-22 16:11:23 -05:00
|
|
|
/// single = "no newlines"
|
|
|
|
/// text = "\nfoo\nbar\n"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You will have:
|
|
|
|
///
|
2017-07-30 11:18:00 -05:00
|
|
|
/// ```toml,ignore
|
|
|
|
/// single = 'no newlines'
|
2017-07-22 16:11:23 -05:00
|
|
|
/// text = '''
|
|
|
|
/// foo
|
|
|
|
/// bar
|
|
|
|
/// '''
|
|
|
|
/// ```
|
2017-07-20 13:47:51 -05:00
|
|
|
pub fn pretty_string(&mut self, value: bool) -> &mut Self {
|
2017-07-28 16:12:39 -05:00
|
|
|
Rc::get_mut(&mut self.settings).unwrap().string = if value {
|
2017-08-13 17:15:15 -05:00
|
|
|
Some(StringSettings::pretty())
|
2017-07-28 16:12:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-07-20 13:47:51 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-08-28 09:40:48 -05:00
|
|
|
/// Enable or Disable Literal strings for pretty strings
|
2017-08-13 17:15:15 -05:00
|
|
|
///
|
|
|
|
/// If enabled, literal strings will be used when possible and strings with
|
|
|
|
/// one or more newlines will use triple quotes (i.e.: `'''` or `"""`)
|
|
|
|
///
|
|
|
|
/// If disabled, literal strings will NEVER be used and strings with one or
|
|
|
|
/// more newlines will use `"""`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Instead of:
|
|
|
|
///
|
|
|
|
/// ```toml,ignore
|
|
|
|
/// single = "no newlines"
|
|
|
|
/// text = "\nfoo\nbar\n"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You will have:
|
|
|
|
///
|
|
|
|
/// ```toml,ignore
|
|
|
|
/// single = "no newlines"
|
|
|
|
/// text = """
|
|
|
|
/// foo
|
|
|
|
/// bar
|
|
|
|
/// """
|
|
|
|
/// ```
|
|
|
|
pub fn pretty_string_literal(&mut self, value: bool) -> &mut Self {
|
2019-08-14 21:52:24 -05:00
|
|
|
let use_default = if let Some(ref mut s) = Rc::get_mut(&mut self.settings).unwrap().string {
|
|
|
|
s.literal = value;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2017-08-13 17:15:15 -05:00
|
|
|
|
|
|
|
if use_default {
|
|
|
|
let mut string = StringSettings::pretty();
|
|
|
|
string.literal = value;
|
|
|
|
Rc::get_mut(&mut self.settings).unwrap().string = Some(string);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-07-20 13:47:51 -05:00
|
|
|
/// Enable or Disable pretty arrays
|
2017-07-22 16:11:23 -05:00
|
|
|
///
|
|
|
|
/// If enabled, arrays will always have each item on their own line.
|
|
|
|
///
|
|
|
|
/// Some specific features can be controlled via other builder methods:
|
|
|
|
///
|
|
|
|
/// - `Serializer::pretty_array_indent`: set the indent to a value other
|
|
|
|
/// than 4.
|
|
|
|
/// - `Serializer::pretty_array_trailing_comma`: enable/disable the trailing
|
|
|
|
/// comma on the last item.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Instead of:
|
|
|
|
///
|
2017-07-30 11:18:00 -05:00
|
|
|
/// ```toml,ignore
|
2017-07-22 16:11:23 -05:00
|
|
|
/// array = ["foo", "bar"]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You will have:
|
|
|
|
///
|
2017-07-30 11:18:00 -05:00
|
|
|
/// ```toml,ignore
|
2017-07-22 16:11:23 -05:00
|
|
|
/// array = [
|
|
|
|
/// "foo",
|
|
|
|
/// "bar",
|
|
|
|
/// ]
|
|
|
|
/// ```
|
2017-07-20 13:47:51 -05:00
|
|
|
pub fn pretty_array(&mut self, value: bool) -> &mut Self {
|
2017-07-28 16:12:39 -05:00
|
|
|
Rc::get_mut(&mut self.settings).unwrap().array = if value {
|
2017-07-24 10:46:24 -05:00
|
|
|
Some(ArraySettings::pretty())
|
2017-07-20 13:47:51 -05:00
|
|
|
} else {
|
2017-07-24 10:46:24 -05:00
|
|
|
None
|
|
|
|
};
|
2017-07-20 13:47:51 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-07-22 16:11:23 -05:00
|
|
|
/// Set the indent for pretty arrays
|
|
|
|
///
|
|
|
|
/// See `Serializer::pretty_array` for more details.
|
2017-07-20 13:47:51 -05:00
|
|
|
pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
|
2019-08-14 21:52:24 -05:00
|
|
|
let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array {
|
|
|
|
a.indent = value;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2017-07-20 13:47:51 -05:00
|
|
|
|
|
|
|
if use_default {
|
|
|
|
let mut array = ArraySettings::pretty();
|
|
|
|
array.indent = value;
|
2017-07-28 16:12:39 -05:00
|
|
|
Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
|
2017-07-20 13:47:51 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify whether to use a trailing comma when serializing pretty arrays
|
2017-07-22 16:11:23 -05:00
|
|
|
///
|
|
|
|
/// See `Serializer::pretty_array` for more details.
|
2017-07-20 13:47:51 -05:00
|
|
|
pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
|
2019-08-14 21:52:24 -05:00
|
|
|
let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array {
|
|
|
|
a.trailing_comma = value;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2017-07-20 13:47:51 -05:00
|
|
|
|
|
|
|
if use_default {
|
|
|
|
let mut array = ArraySettings::pretty();
|
|
|
|
array.trailing_comma = value;
|
2017-07-28 16:12:39 -05:00
|
|
|
Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
|
2017-07-20 13:47:51 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:28:29 -06:00
|
|
|
fn display<T: fmt::Display>(&mut self, t: T, type_: ArrayState) -> Result<(), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
self.emit_key(type_)?;
|
2019-08-20 11:59:28 -05:00
|
|
|
write!(self.dst, "{}", t).map_err(ser::Error::custom)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
if let State::Table { .. } = self.state {
|
|
|
|
self.dst.push_str("\n");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:28:29 -06:00
|
|
|
fn emit_key(&mut self, type_: ArrayState) -> Result<(), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
self.array_type(type_)?;
|
|
|
|
let state = self.state.clone();
|
|
|
|
self._emit_key(&state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursive implementation of `emit_key` above
|
2019-05-08 19:37:38 -05:00
|
|
|
fn _emit_key(&mut self, state: &State<'_>) -> Result<(), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
match *state {
|
|
|
|
State::End => Ok(()),
|
2018-12-17 19:45:35 -06:00
|
|
|
State::Array {
|
|
|
|
parent,
|
|
|
|
first,
|
|
|
|
type_,
|
|
|
|
len,
|
|
|
|
} => {
|
2017-01-29 18:53:20 -06:00
|
|
|
assert!(type_.get().is_some());
|
|
|
|
if first.get() {
|
|
|
|
self._emit_key(parent)?;
|
|
|
|
}
|
2017-07-27 23:44:58 -05:00
|
|
|
self.emit_array(first, len)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
State::Table {
|
|
|
|
parent,
|
|
|
|
first,
|
|
|
|
table_emitted,
|
|
|
|
key,
|
|
|
|
} => {
|
2017-01-29 18:53:20 -06:00
|
|
|
if table_emitted.get() {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(Error::ValueAfterTable);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
if first.get() {
|
|
|
|
self.emit_table_header(parent)?;
|
|
|
|
first.set(false);
|
|
|
|
}
|
|
|
|
self.escape_key(key)?;
|
|
|
|
self.dst.push_str(" = ");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 23:44:58 -05:00
|
|
|
fn emit_array(&mut self, first: &Cell<bool>, len: Option<usize>) -> Result<(), Error> {
|
|
|
|
match (len, &self.settings.array) {
|
2019-05-08 19:37:38 -05:00
|
|
|
(Some(0..=1), _) | (_, &None) => {
|
2017-07-27 23:44:58 -05:00
|
|
|
if first.get() {
|
|
|
|
self.dst.push_str("[")
|
|
|
|
} else {
|
|
|
|
self.dst.push_str(", ")
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-07-27 23:44:58 -05:00
|
|
|
(_, &Some(ref a)) => {
|
2017-07-09 16:10:36 -05:00
|
|
|
if first.get() {
|
|
|
|
self.dst.push_str("[\n")
|
|
|
|
} else {
|
|
|
|
self.dst.push_str(",\n")
|
|
|
|
}
|
|
|
|
for _ in 0..a.indent {
|
|
|
|
self.dst.push_str(" ");
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:28:29 -06:00
|
|
|
fn array_type(&mut self, type_: ArrayState) -> Result<(), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let prev = match self.state {
|
|
|
|
State::Array { type_, .. } => type_,
|
|
|
|
_ => return Ok(()),
|
|
|
|
};
|
2019-11-22 10:28:29 -06:00
|
|
|
if let None = prev.get() {
|
2017-01-29 18:53:20 -06:00
|
|
|
prev.set(Some(type_));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn escape_key(&mut self, key: &str) -> Result<(), Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
let ok = key.chars().all(|c| match c {
|
2019-05-08 19:37:38 -05:00
|
|
|
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true,
|
2018-12-17 19:45:35 -06:00
|
|
|
_ => false,
|
2017-01-29 18:53:20 -06:00
|
|
|
});
|
|
|
|
if ok {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!(self.dst, "{}", key).map_err(ser::Error::custom)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
} else {
|
2017-07-27 17:37:30 -05:00
|
|
|
self.emit_str(key, true)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:37:30 -05:00
|
|
|
fn emit_str(&mut self, value: &str, is_key: bool) -> Result<(), Error> {
|
2017-07-27 23:44:58 -05:00
|
|
|
#[derive(PartialEq)]
|
2017-07-28 10:12:21 -05:00
|
|
|
enum Type {
|
|
|
|
NewlineTripple,
|
|
|
|
OnelineTripple,
|
|
|
|
OnelineSingle,
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
|
|
|
|
enum Repr {
|
|
|
|
/// represent as a literal string (using '')
|
|
|
|
Literal(String, Type),
|
|
|
|
/// represent the std way (using "")
|
|
|
|
Std(Type),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_pretty(value: &str) -> Repr {
|
2017-07-27 23:44:58 -05:00
|
|
|
// For doing pretty prints we store in a new String
|
|
|
|
// because there are too many cases where pretty cannot
|
|
|
|
// work. We need to determine:
|
|
|
|
// - if we are a "multi-line" pretty (if there are \n)
|
|
|
|
// - if ['''] appears if multi or ['] if single
|
|
|
|
// - if there are any invalid control characters
|
|
|
|
//
|
|
|
|
// Doing it any other way would require multiple passes
|
|
|
|
// to determine if a pretty string works or not.
|
|
|
|
let mut out = String::with_capacity(value.len() * 2);
|
2017-07-28 10:12:21 -05:00
|
|
|
let mut ty = Type::OnelineSingle;
|
2017-07-27 23:44:58 -05:00
|
|
|
// found consecutive single quotes
|
|
|
|
let mut max_found_singles = 0;
|
|
|
|
let mut found_singles = 0;
|
2017-07-28 10:12:21 -05:00
|
|
|
let mut can_be_pretty = true;
|
2017-07-27 23:44:58 -05:00
|
|
|
|
|
|
|
for ch in value.chars() {
|
2017-07-28 10:12:21 -05:00
|
|
|
if can_be_pretty {
|
|
|
|
if ch == '\'' {
|
|
|
|
found_singles += 1;
|
|
|
|
if found_singles >= 3 {
|
|
|
|
can_be_pretty = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if found_singles > max_found_singles {
|
|
|
|
max_found_singles = found_singles;
|
|
|
|
}
|
|
|
|
found_singles = 0
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
match ch {
|
2018-12-17 19:45:35 -06:00
|
|
|
'\t' => {}
|
2017-07-28 10:12:21 -05:00
|
|
|
'\n' => ty = Type::NewlineTripple,
|
|
|
|
// note that the following are invalid: \b \f \r
|
|
|
|
c if c < '\u{1f}' => can_be_pretty = false, // Invalid control character
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
out.push(ch);
|
2017-07-27 23:44:58 -05:00
|
|
|
} else {
|
2017-07-28 10:12:21 -05:00
|
|
|
// the string cannot be represented as pretty,
|
|
|
|
// still check if it should be multiline
|
|
|
|
if ch == '\n' {
|
|
|
|
ty = Type::NewlineTripple;
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
}
|
2018-10-01 03:58:57 -05:00
|
|
|
if can_be_pretty && found_singles > 0 && value.ends_with('\'') {
|
|
|
|
// We cannot escape the ending quote so we must use """
|
|
|
|
can_be_pretty = false;
|
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
if !can_be_pretty {
|
|
|
|
debug_assert!(ty != Type::OnelineTripple);
|
|
|
|
return Repr::Std(ty);
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
|
|
|
if found_singles > max_found_singles {
|
|
|
|
max_found_singles = found_singles;
|
|
|
|
}
|
|
|
|
debug_assert!(max_found_singles < 3);
|
2017-07-28 10:12:21 -05:00
|
|
|
if ty == Type::OnelineSingle && max_found_singles >= 1 {
|
2017-07-27 23:44:58 -05:00
|
|
|
// no newlines, but must use ''' because it has ' in it
|
2017-07-28 10:12:21 -05:00
|
|
|
ty = Type::OnelineTripple;
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
Repr::Literal(out, ty)
|
2017-07-27 23:44:58 -05:00
|
|
|
}
|
|
|
|
|
2017-07-28 16:12:39 -05:00
|
|
|
let repr = if !is_key && self.settings.string.is_some() {
|
2017-08-13 17:15:15 -05:00
|
|
|
match (&self.settings.string, do_pretty(value)) {
|
2018-12-17 19:45:35 -06:00
|
|
|
(&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) => {
|
|
|
|
Repr::Std(ty)
|
|
|
|
}
|
2019-08-14 21:52:24 -05:00
|
|
|
(_, r) => r,
|
2017-08-13 17:15:15 -05:00
|
|
|
}
|
2017-07-09 15:58:48 -05:00
|
|
|
} else {
|
2017-07-28 10:12:21 -05:00
|
|
|
Repr::Std(Type::OnelineSingle)
|
2017-07-09 15:58:48 -05:00
|
|
|
};
|
2017-07-28 10:12:21 -05:00
|
|
|
match repr {
|
|
|
|
Repr::Literal(literal, ty) => {
|
|
|
|
// A pretty string
|
|
|
|
match ty {
|
|
|
|
Type::NewlineTripple => self.dst.push_str("'''\n"),
|
|
|
|
Type::OnelineTripple => self.dst.push_str("'''"),
|
|
|
|
Type::OnelineSingle => self.dst.push('\''),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
self.dst.push_str(&literal);
|
|
|
|
match ty {
|
|
|
|
Type::OnelineSingle => self.dst.push('\''),
|
|
|
|
_ => self.dst.push_str("'''"),
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
Repr::Std(ty) => {
|
|
|
|
match ty {
|
2018-12-17 19:45:35 -06:00
|
|
|
Type::NewlineTripple => self.dst.push_str("\"\"\"\n"),
|
2017-08-13 17:15:15 -05:00
|
|
|
// note: OnelineTripple can happen if do_pretty wants to do
|
2017-08-28 09:40:48 -05:00
|
|
|
// '''it's one line'''
|
2017-08-13 17:15:15 -05:00
|
|
|
// but settings.string.literal == false
|
2018-12-17 19:45:35 -06:00
|
|
|
Type::OnelineSingle | Type::OnelineTripple => self.dst.push('"'),
|
2017-07-28 10:12:21 -05:00
|
|
|
}
|
|
|
|
for ch in value.chars() {
|
|
|
|
match ch {
|
|
|
|
'\u{8}' => self.dst.push_str("\\b"),
|
|
|
|
'\u{9}' => self.dst.push_str("\\t"),
|
2018-12-17 19:45:35 -06:00
|
|
|
'\u{a}' => match ty {
|
|
|
|
Type::NewlineTripple => self.dst.push('\n'),
|
|
|
|
Type::OnelineSingle => self.dst.push_str("\\n"),
|
|
|
|
_ => unreachable!(),
|
2017-07-28 10:12:21 -05:00
|
|
|
},
|
|
|
|
'\u{c}' => self.dst.push_str("\\f"),
|
|
|
|
'\u{d}' => self.dst.push_str("\\r"),
|
|
|
|
'\u{22}' => self.dst.push_str("\\\""),
|
|
|
|
'\u{5c}' => self.dst.push_str("\\\\"),
|
2019-08-18 19:23:40 -05:00
|
|
|
c if c < '\u{1f}' => {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!(self.dst, "\\u{:04X}", ch as u32).map_err(ser::Error::custom)?;
|
2019-08-18 19:23:40 -05:00
|
|
|
}
|
2017-07-28 10:12:21 -05:00
|
|
|
ch => self.dst.push(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match ty {
|
2018-12-17 19:45:35 -06:00
|
|
|
Type::NewlineTripple => self.dst.push_str("\"\"\""),
|
2017-08-13 17:15:15 -05:00
|
|
|
Type::OnelineSingle | Type::OnelineTripple => self.dst.push('"'),
|
2017-07-28 10:12:21 -05:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-07-09 15:58:48 -05:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
fn emit_table_header(&mut self, state: &State<'_>) -> Result<(), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let array_of_tables = match *state {
|
|
|
|
State::End => return Ok(()),
|
|
|
|
State::Array { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2017-06-01 05:38:56 -05:00
|
|
|
|
|
|
|
// Unlike [..]s, we can't omit [[..]] ancestors, so be sure to emit table
|
|
|
|
// headers for them.
|
|
|
|
let mut p = state;
|
|
|
|
if let State::Array { first, parent, .. } = *state {
|
|
|
|
if first.get() {
|
|
|
|
p = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while let State::Table { first, parent, .. } = *p {
|
|
|
|
p = parent;
|
|
|
|
if !first.get() {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
if let State::Array {
|
|
|
|
parent: &State::Table { .. },
|
|
|
|
..
|
|
|
|
} = *parent
|
|
|
|
{
|
2017-06-01 05:38:56 -05:00
|
|
|
self.emit_table_header(parent)?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
match *state {
|
2017-08-13 16:23:50 -05:00
|
|
|
State::Table { first, .. } => {
|
2017-01-29 18:53:20 -06:00
|
|
|
if !first.get() {
|
2017-08-13 16:23:50 -05:00
|
|
|
// Newline if we are a table that is not the first
|
|
|
|
// table in the document.
|
|
|
|
self.dst.push('\n');
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-08-13 16:23:50 -05:00
|
|
|
State::Array { parent, first, .. } => {
|
|
|
|
if !first.get() {
|
|
|
|
// Always newline if we are not the first item in the
|
|
|
|
// table-array
|
|
|
|
self.dst.push('\n');
|
|
|
|
} else if let State::Table { first, .. } = *parent {
|
|
|
|
if !first.get() {
|
2017-08-28 09:40:48 -05:00
|
|
|
// Newline if we are not the first item in the document
|
2017-08-13 16:23:50 -05:00
|
|
|
self.dst.push('\n');
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
self.dst.push_str("[");
|
|
|
|
if array_of_tables {
|
|
|
|
self.dst.push_str("[");
|
|
|
|
}
|
2017-03-30 06:42:27 -05:00
|
|
|
self.emit_key_part(state)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
if array_of_tables {
|
|
|
|
self.dst.push_str("]");
|
|
|
|
}
|
|
|
|
self.dst.push_str("]\n");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
fn emit_key_part(&mut self, key: &State<'_>) -> Result<bool, Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
match *key {
|
|
|
|
State::Array { parent, .. } => self.emit_key_part(parent),
|
|
|
|
State::End => Ok(true),
|
2018-12-17 19:45:35 -06:00
|
|
|
State::Table {
|
|
|
|
key,
|
|
|
|
parent,
|
|
|
|
table_emitted,
|
|
|
|
..
|
|
|
|
} => {
|
2017-01-29 18:53:20 -06:00
|
|
|
table_emitted.set(true);
|
2018-12-17 19:45:35 -06:00
|
|
|
let first = self.emit_key_part(parent)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
if !first {
|
|
|
|
self.dst.push_str(".");
|
|
|
|
}
|
|
|
|
self.escape_key(key)?;
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:27:58 -05:00
|
|
|
macro_rules! serialize_float {
|
|
|
|
($this:expr, $v:expr) => {{
|
2019-11-22 10:28:29 -06:00
|
|
|
$this.emit_key(ArrayState::Started)?;
|
2018-07-10 18:27:58 -05:00
|
|
|
if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!($this.dst, "-").map_err(ser::Error::custom)?;
|
2018-07-10 18:27:58 -05:00
|
|
|
}
|
|
|
|
if $v.is_nan() {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!($this.dst, "nan").map_err(ser::Error::custom)?;
|
2018-07-10 18:27:58 -05:00
|
|
|
} else {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!($this.dst, "{}", $v).map_err(ser::Error::custom)?;
|
2018-07-10 18:27:58 -05:00
|
|
|
}
|
|
|
|
if $v % 1.0 == 0.0 {
|
2019-08-20 11:59:28 -05:00
|
|
|
write!($this.dst, ".0").map_err(ser::Error::custom)?;
|
2018-07-10 18:27:58 -05:00
|
|
|
}
|
|
|
|
if let State::Table { .. } = $this.state {
|
|
|
|
$this.dst.push_str("\n");
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
type SerializeSeq = SerializeSeq<'a, 'b>;
|
2018-05-24 09:25:15 -05:00
|
|
|
type SerializeTuple = SerializeSeq<'a, 'b>;
|
|
|
|
type SerializeTupleStruct = SerializeSeq<'a, 'b>;
|
|
|
|
type SerializeTupleVariant = SerializeSeq<'a, 'b>;
|
2017-01-29 18:53:20 -06:00
|
|
|
type SerializeMap = SerializeTable<'a, 'b>;
|
|
|
|
type SerializeStruct = SerializeTable<'a, 'b>;
|
|
|
|
type SerializeStructVariant = ser::Impossible<(), Error>;
|
|
|
|
|
|
|
|
fn serialize_bool(self, v: bool) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i8(self, v: i8) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i16(self, v: i16) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(self, v: i32) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i64(self, v: i64) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u8(self, v: u8) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u16(self, v: u16) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(self, v: u32) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(self, v: u64) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.display(v, ArrayState::Started)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:40:48 -05:00
|
|
|
fn serialize_f32(self, v: f32) -> Result<(), Self::Error> {
|
2018-07-10 18:27:58 -05:00
|
|
|
serialize_float!(self, v)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:40:48 -05:00
|
|
|
fn serialize_f64(self, v: f64) -> Result<(), Self::Error> {
|
2018-07-10 18:27:58 -05:00
|
|
|
serialize_float!(self, v)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_char(self, v: char) -> Result<(), Self::Error> {
|
|
|
|
let mut buf = [0; 4];
|
|
|
|
self.serialize_str(v.encode_utf8(&mut buf))
|
|
|
|
}
|
|
|
|
|
2017-08-28 09:40:48 -05:00
|
|
|
fn serialize_str(self, value: &str) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.emit_key(ArrayState::Started)?;
|
2017-07-27 17:37:30 -05:00
|
|
|
self.emit_str(value, false)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
if let State::Table { .. } = self.state {
|
|
|
|
self.dst.push_str("\n");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:21:00 -06:00
|
|
|
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
use serde::ser::Serialize;
|
|
|
|
value.serialize(self)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::UnsupportedNone)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Self::Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::UnsupportedType)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::UnsupportedType)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
) -> Result<(), Self::Error> {
|
2017-04-27 23:00:37 -05:00
|
|
|
self.serialize_str(variant)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::UnsupportedType)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.array_type(ArrayState::Started)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(SerializeSeq {
|
|
|
|
ser: self,
|
|
|
|
first: Cell::new(true),
|
|
|
|
type_: Cell::new(None),
|
2019-08-14 21:52:24 -05:00
|
|
|
len,
|
2017-01-29 18:53:20 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
self.serialize_seq(Some(len))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
self.serialize_seq(Some(len))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
self.serialize_seq(Some(len))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.array_type(ArrayState::StartedAsATable)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(SerializeTable::Table {
|
|
|
|
ser: self,
|
|
|
|
key: String::new(),
|
|
|
|
first: Cell::new(true),
|
|
|
|
table_emitted: Cell::new(false),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
2018-05-06 20:47:09 -05:00
|
|
|
if name == datetime::NAME {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.array_type(ArrayState::Started)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(SerializeTable::Datetime(self))
|
|
|
|
} else {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.array_type(ArrayState::StartedAsATable)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(SerializeTable::Table {
|
|
|
|
ser: self,
|
|
|
|
key: String::new(),
|
|
|
|
first: Cell::new(true),
|
|
|
|
table_emitted: Cell::new(false),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::UnsupportedType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
value.serialize(&mut Serializer {
|
|
|
|
dst: &mut *self.ser.dst,
|
|
|
|
state: State::Array {
|
|
|
|
parent: &self.ser.state,
|
|
|
|
first: &self.first,
|
|
|
|
type_: &self.type_,
|
2017-07-27 23:44:58 -05:00
|
|
|
len: self.len,
|
2017-01-29 18:53:20 -06:00
|
|
|
},
|
2017-07-09 15:38:48 -05:00
|
|
|
settings: self.ser.settings.clone(),
|
2017-01-29 18:53:20 -06:00
|
|
|
})?;
|
|
|
|
self.first.set(false);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
|
|
|
match self.type_.get() {
|
2019-11-22 10:28:29 -06:00
|
|
|
Some(ArrayState::StartedAsATable) => return Ok(()),
|
|
|
|
Some(ArrayState::Started) => match (self.len, &self.ser.settings.array) {
|
2019-05-08 19:37:38 -05:00
|
|
|
(Some(0..=1), _) | (_, &None) => {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.ser.dst.push_str("]");
|
2017-07-09 16:10:36 -05:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
(_, &Some(ref a)) => {
|
|
|
|
if a.trailing_comma {
|
|
|
|
self.ser.dst.push_str(",");
|
|
|
|
}
|
|
|
|
self.ser.dst.push_str("\n]");
|
|
|
|
}
|
|
|
|
},
|
2017-01-29 18:53:20 -06:00
|
|
|
None => {
|
|
|
|
assert!(self.first.get());
|
2019-11-22 10:28:29 -06:00
|
|
|
self.ser.emit_key(ArrayState::Started)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
self.ser.dst.push_str("[]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let State::Table { .. } = self.ser.state {
|
|
|
|
self.ser.dst.push_str("\n");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:25:15 -05:00
|
|
|
impl<'a, 'b> ser::SerializeTuple for SerializeSeq<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2018-05-24 09:25:15 -05:00
|
|
|
{
|
|
|
|
ser::SerializeSeq::serialize_element(self, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
|
|
|
ser::SerializeSeq::end(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> ser::SerializeTupleVariant for SerializeSeq<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2018-05-24 09:25:15 -05:00
|
|
|
{
|
|
|
|
ser::SerializeSeq::serialize_element(self, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
|
|
|
ser::SerializeSeq::end(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2018-05-24 09:25:15 -05:00
|
|
|
{
|
|
|
|
ser::SerializeSeq::serialize_element(self, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
|
|
|
ser::SerializeSeq::end(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_key<T: ?Sized>(&mut self, input: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
SerializeTable::Datetime(_) => panic!(), // shouldn't be possible
|
|
|
|
SerializeTable::Table { ref mut key, .. } => {
|
|
|
|
key.truncate(0);
|
|
|
|
*key = input.serialize(StringExtractor)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
SerializeTable::Datetime(_) => panic!(), // shouldn't be possible
|
|
|
|
SerializeTable::Table {
|
|
|
|
ref mut ser,
|
|
|
|
ref key,
|
|
|
|
ref first,
|
|
|
|
ref table_emitted,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let res = value.serialize(&mut Serializer {
|
|
|
|
dst: &mut *ser.dst,
|
|
|
|
state: State::Table {
|
2019-08-14 21:52:24 -05:00
|
|
|
key,
|
2017-01-29 18:53:20 -06:00
|
|
|
parent: &ser.state,
|
2019-08-14 21:52:24 -05:00
|
|
|
first,
|
|
|
|
table_emitted,
|
2017-01-29 18:53:20 -06:00
|
|
|
},
|
2017-07-09 15:38:48 -05:00
|
|
|
settings: ser.settings.clone(),
|
2017-01-29 18:53:20 -06:00
|
|
|
});
|
|
|
|
match res {
|
|
|
|
Ok(()) => first.set(false),
|
2018-12-17 19:45:35 -06:00
|
|
|
Err(Error::UnsupportedNone) => {}
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
|
|
|
match self {
|
|
|
|
SerializeTable::Datetime(_) => panic!(), // shouldn't be possible
|
2018-12-17 19:45:35 -06:00
|
|
|
SerializeTable::Table { ser, first, .. } => {
|
2017-01-29 18:53:20 -06:00
|
|
|
if first.get() {
|
|
|
|
let state = ser.state.clone();
|
|
|
|
ser.emit_table_header(&state)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
SerializeTable::Datetime(ref mut ser) => {
|
2018-05-06 20:47:09 -05:00
|
|
|
if key == datetime::FIELD {
|
2017-01-29 18:53:20 -06:00
|
|
|
value.serialize(DateStrEmitter(&mut *ser))?;
|
|
|
|
} else {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(Error::DateInvalid);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SerializeTable::Table {
|
|
|
|
ref mut ser,
|
|
|
|
ref first,
|
|
|
|
ref table_emitted,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let res = value.serialize(&mut Serializer {
|
|
|
|
dst: &mut *ser.dst,
|
|
|
|
state: State::Table {
|
2019-08-14 21:52:24 -05:00
|
|
|
key,
|
2017-01-29 18:53:20 -06:00
|
|
|
parent: &ser.state,
|
2019-08-14 21:52:24 -05:00
|
|
|
first,
|
|
|
|
table_emitted,
|
2017-01-29 18:53:20 -06:00
|
|
|
},
|
2017-07-09 15:38:48 -05:00
|
|
|
settings: ser.settings.clone(),
|
2017-01-29 18:53:20 -06:00
|
|
|
});
|
|
|
|
match res {
|
|
|
|
Ok(()) => first.set(false),
|
2018-12-17 19:45:35 -06:00
|
|
|
Err(Error::UnsupportedNone) => {}
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-07-26 23:08:22 -05:00
|
|
|
match self {
|
2018-12-17 19:45:35 -06:00
|
|
|
SerializeTable::Datetime(_) => {}
|
|
|
|
SerializeTable::Table { ser, first, .. } => {
|
2017-07-26 23:08:22 -05:00
|
|
|
if first.get() {
|
|
|
|
let state = ser.state.clone();
|
|
|
|
ser.emit_table_header(&state)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:12:39 -05:00
|
|
|
struct DateStrEmitter<'a, 'b>(&'b mut Serializer<'a>);
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
type SerializeSeq = ser::Impossible<(), Error>;
|
|
|
|
type SerializeTuple = ser::Impossible<(), Error>;
|
|
|
|
type SerializeTupleStruct = ser::Impossible<(), Error>;
|
|
|
|
type SerializeTupleVariant = ser::Impossible<(), Error>;
|
|
|
|
type SerializeMap = ser::Impossible<(), Error>;
|
|
|
|
type SerializeStruct = ser::Impossible<(), Error>;
|
|
|
|
type SerializeStructVariant = ser::Impossible<(), Error>;
|
|
|
|
|
|
|
|
fn serialize_bool(self, _v: bool) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i8(self, _v: i8) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i16(self, _v: i16) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(self, _v: i32) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i64(self, _v: i64) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u8(self, _v: u8) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u16(self, _v: u16) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(self, _v: u32) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(self, _v: u64) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f32(self, _v: f32) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f64(self, _v: f64) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_char(self, _v: char) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, value: &str) -> Result<(), Self::Error> {
|
2019-11-22 10:28:29 -06:00
|
|
|
self.0.display(value, ArrayState::Started)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bytes(self, _value: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<(), Self::Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<(), Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
) -> Result<(), Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::DateInvalid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StringExtractor;
|
|
|
|
|
|
|
|
impl ser::Serializer for StringExtractor {
|
|
|
|
type Ok = String;
|
|
|
|
type Error = Error;
|
|
|
|
type SerializeSeq = ser::Impossible<String, Error>;
|
|
|
|
type SerializeTuple = ser::Impossible<String, Error>;
|
|
|
|
type SerializeTupleStruct = ser::Impossible<String, Error>;
|
|
|
|
type SerializeTupleVariant = ser::Impossible<String, Error>;
|
|
|
|
type SerializeMap = ser::Impossible<String, Error>;
|
|
|
|
type SerializeStruct = ser::Impossible<String, Error>;
|
|
|
|
type SerializeStructVariant = ser::Impossible<String, Error>;
|
|
|
|
|
|
|
|
fn serialize_bool(self, _v: bool) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i8(self, _v: i8) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i16(self, _v: i16) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(self, _v: i32) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i64(self, _v: i64) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u8(self, _v: u8) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u16(self, _v: u16) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(self, _v: u32) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(self, _v: u64) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f32(self, _v: f32) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f64(self, _v: f64) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_char(self, _v: char) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, value: &str) -> Result<String, Self::Error> {
|
|
|
|
Ok(value.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bytes(self, _value: &[u8]) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<String, Self::Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<String, Self::Error> {
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<String, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
) -> Result<String, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<String, Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2018-10-27 20:28:18 -05:00
|
|
|
value.serialize(self)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_value: &T,
|
|
|
|
) -> Result<String, Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Err(Error::KeyNotString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
2019-05-08 19:37:38 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-01-29 18:53:20 -06:00
|
|
|
match *self {
|
|
|
|
Error::UnsupportedType => "unsupported Rust type".fmt(f),
|
|
|
|
Error::KeyNotString => "map key was not a string".fmt(f),
|
|
|
|
Error::ValueAfterTable => "values must be emitted before tables".fmt(f),
|
2017-05-30 18:30:19 -05:00
|
|
|
Error::DateInvalid => "a serialized date was invalid".fmt(f),
|
|
|
|
Error::NumberInvalid => "a serialized number was invalid".fmt(f),
|
2017-01-29 18:53:20 -06:00
|
|
|
Error::UnsupportedNone => "unsupported None value".fmt(f),
|
|
|
|
Error::Custom(ref s) => s.fmt(f),
|
2017-06-01 16:47:24 -05:00
|
|
|
Error::KeyNewline => unreachable!(),
|
2019-11-22 10:28:29 -06:00
|
|
|
Error::ArrayMixedType => unreachable!(),
|
2017-01-29 18:53:20 -06:00
|
|
|
Error::__Nonexhaustive => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 17:06:00 -06:00
|
|
|
impl error::Error for Error {}
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
impl ser::Error for Error {
|
|
|
|
fn custom<T: fmt::Display>(msg: T) -> Error {
|
|
|
|
Error::Custom(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 17:29:36 -06:00
|
|
|
|
|
|
|
enum Category {
|
|
|
|
Primitive,
|
|
|
|
Array,
|
|
|
|
Table,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function to serialize items in a map in an order valid with
|
|
|
|
/// TOML.
|
|
|
|
///
|
|
|
|
/// TOML carries the restriction that keys in a table must be serialized last if
|
|
|
|
/// their value is a table itself. This isn't always easy to guarantee, so this
|
|
|
|
/// helper can be used like so:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-05-08 19:37:38 -05:00
|
|
|
/// # use serde_derive::Serialize;
|
2017-02-10 17:29:36 -06:00
|
|
|
/// # use std::collections::HashMap;
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct Manifest {
|
|
|
|
/// package: Package,
|
|
|
|
/// #[serde(serialize_with = "toml::ser::tables_last")]
|
|
|
|
/// dependencies: HashMap<String, Dependency>,
|
|
|
|
/// }
|
|
|
|
/// # type Package = String;
|
|
|
|
/// # type Dependency = String;
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-12-17 19:45:35 -06:00
|
|
|
pub fn tables_last<'a, I, K, V, S>(data: &'a I, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
&'a I: IntoIterator<Item = (K, V)>,
|
|
|
|
K: ser::Serialize,
|
|
|
|
V: ser::Serialize,
|
|
|
|
S: ser::Serializer,
|
2017-02-10 17:29:36 -06:00
|
|
|
{
|
|
|
|
use serde::ser::SerializeMap;
|
|
|
|
|
|
|
|
let mut map = serializer.serialize_map(None)?;
|
|
|
|
for (k, v) in data {
|
|
|
|
if let Category::Primitive = v.serialize(Categorize::new())? {
|
|
|
|
map.serialize_entry(&k, &v)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k, v) in data {
|
|
|
|
if let Category::Array = v.serialize(Categorize::new())? {
|
|
|
|
map.serialize_entry(&k, &v)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k, v) in data {
|
|
|
|
if let Category::Table = v.serialize(Categorize::new())? {
|
|
|
|
map.serialize_entry(&k, &v)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Categorize<E>(marker::PhantomData<E>);
|
|
|
|
|
|
|
|
impl<E> Categorize<E> {
|
|
|
|
fn new() -> Self {
|
|
|
|
Categorize(marker::PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::Serializer for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
type SerializeSeq = Self;
|
2018-05-24 09:25:15 -05:00
|
|
|
type SerializeTuple = Self;
|
|
|
|
type SerializeTupleStruct = Self;
|
|
|
|
type SerializeTupleVariant = Self;
|
2017-02-10 17:29:36 -06:00
|
|
|
type SerializeMap = Self;
|
|
|
|
type SerializeStruct = Self;
|
|
|
|
type SerializeStructVariant = ser::Impossible<Category, E>;
|
|
|
|
|
|
|
|
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Primitive)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
|
2017-02-21 08:21:00 -06:00
|
|
|
Ok(Category::Array)
|
2017-02-10 17:29:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized + ser::Serialize>(self, v: &T) -> Result<Self::Ok, Self::Error> {
|
|
|
|
v.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
_: u32,
|
|
|
|
_: &'static str,
|
|
|
|
) -> Result<Self::Ok, Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
v: &T,
|
|
|
|
) -> Result<Self::Ok, Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
v.serialize(self)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
_: u32,
|
|
|
|
_: &'static str,
|
|
|
|
_: &T,
|
|
|
|
) -> Result<Self::Ok, Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_seq(self, _: Option<usize>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(self)
|
2017-02-10 17:29:36 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
_: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(self)
|
2017-02-10 17:29:36 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
_: u32,
|
|
|
|
_: &'static str,
|
|
|
|
_: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(self)
|
2017-02-10 17:29:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map(self, _: Option<usize>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self, Self::Error> {
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_: &'static str,
|
|
|
|
_: u32,
|
|
|
|
_: &'static str,
|
|
|
|
_: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Err(ser::Error::custom("unsupported"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeSeq for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(Category::Array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeTuple for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeTupleVariant for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeTupleStruct for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2018-05-24 09:25:15 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Ok(Category::Array)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeMap for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
|
2017-02-10 17:29:36 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: ser::Error> ser::SerializeStruct for Categorize<E> {
|
|
|
|
type Ok = Category;
|
|
|
|
type Error = E;
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, _: &'static str, _: &T) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: ser::Serialize,
|
2017-02-10 17:29:36 -06:00
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(Category::Table)
|
|
|
|
}
|
|
|
|
}
|