From ef24de24a82fe681581cc130f342363c47c0969a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 7 Jun 2024 07:48:48 +0200 Subject: Merging upstream version 1.75.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/toml_edit-0.19.11/src/ser/array.rs | 84 ++++++ vendor/toml_edit-0.19.11/src/ser/key.rs | 173 ++++++++++++ vendor/toml_edit-0.19.11/src/ser/map.rs | 405 +++++++++++++++++++++++++++++ vendor/toml_edit-0.19.11/src/ser/mod.rs | 165 ++++++++++++ vendor/toml_edit-0.19.11/src/ser/pretty.rs | 45 ++++ vendor/toml_edit-0.19.11/src/ser/value.rs | 243 +++++++++++++++++ 6 files changed, 1115 insertions(+) create mode 100644 vendor/toml_edit-0.19.11/src/ser/array.rs create mode 100644 vendor/toml_edit-0.19.11/src/ser/key.rs create mode 100644 vendor/toml_edit-0.19.11/src/ser/map.rs create mode 100644 vendor/toml_edit-0.19.11/src/ser/mod.rs create mode 100644 vendor/toml_edit-0.19.11/src/ser/pretty.rs create mode 100644 vendor/toml_edit-0.19.11/src/ser/value.rs (limited to 'vendor/toml_edit-0.19.11/src/ser') diff --git a/vendor/toml_edit-0.19.11/src/ser/array.rs b/vendor/toml_edit-0.19.11/src/ser/array.rs new file mode 100644 index 000000000..80eba8ba7 --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/array.rs @@ -0,0 +1,84 @@ +use super::Error; + +#[doc(hidden)] +pub struct SerializeValueArray { + values: Vec, +} + +impl SerializeValueArray { + pub(crate) fn new() -> Self { + Self { values: Vec::new() } + } + + pub(crate) fn with_capacity(len: usize) -> Self { + Self { + values: Vec::with_capacity(len), + } + } +} + +impl serde::ser::SerializeSeq for SerializeValueArray { + type Ok = crate::Value; + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<(), Error> + where + T: serde::ser::Serialize, + { + let value = value.serialize(super::ValueSerializer {})?; + self.values.push(crate::Item::Value(value)); + Ok(()) + } + + fn end(self) -> Result { + Ok(crate::Value::Array(crate::Array::with_vec(self.values))) + } +} + +impl serde::ser::SerializeTuple for SerializeValueArray { + type Ok = crate::Value; + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<(), Error> + where + T: serde::ser::Serialize, + { + serde::ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result { + serde::ser::SerializeSeq::end(self) + } +} + +impl serde::ser::SerializeTupleVariant for SerializeValueArray { + type Ok = crate::Value; + type Error = Error; + + fn serialize_field(&mut self, value: &T) -> Result<(), Error> + where + T: serde::ser::Serialize, + { + serde::ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result { + serde::ser::SerializeSeq::end(self) + } +} + +impl serde::ser::SerializeTupleStruct for SerializeValueArray { + type Ok = crate::Value; + type Error = Error; + + fn serialize_field(&mut self, value: &T) -> Result<(), Error> + where + T: serde::ser::Serialize, + { + serde::ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result { + serde::ser::SerializeSeq::end(self) + } +} diff --git a/vendor/toml_edit-0.19.11/src/ser/key.rs b/vendor/toml_edit-0.19.11/src/ser/key.rs new file mode 100644 index 000000000..d5e381bf7 --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/key.rs @@ -0,0 +1,173 @@ +use crate::InternalString; + +use super::Error; + +pub(crate) struct KeySerializer; + +impl serde::ser::Serializer for KeySerializer { + type Ok = InternalString; + type Error = Error; + type SerializeSeq = serde::ser::Impossible; + type SerializeTuple = serde::ser::Impossible; + type SerializeTupleStruct = serde::ser::Impossible; + type SerializeTupleVariant = serde::ser::Impossible; + type SerializeMap = serde::ser::Impossible; + type SerializeStruct = serde::ser::Impossible; + type SerializeStructVariant = serde::ser::Impossible; + + fn serialize_bool(self, _v: bool) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_i8(self, _v: i8) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_i16(self, _v: i16) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_i32(self, _v: i32) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_i64(self, _v: i64) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_u8(self, _v: u8) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_u16(self, _v: u16) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_u32(self, _v: u32) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_u64(self, _v: u64) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_f32(self, _v: f32) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_f64(self, _v: f64) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_char(self, _v: char) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_str(self, value: &str) -> Result { + Ok(InternalString::from(value)) + } + + fn serialize_bytes(self, _value: &[u8]) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_none(self) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_some(self, _value: &T) -> Result + where + T: serde::ser::Serialize, + { + Err(Error::KeyNotString) + } + + fn serialize_unit(self) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { + Ok(variant.into()) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + value.serialize(self) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + Err(Error::KeyNotString) + } + + fn serialize_seq(self, _len: Option) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_map(self, _len: Option) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(Error::KeyNotString) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(Error::KeyNotString) + } +} diff --git a/vendor/toml_edit-0.19.11/src/ser/map.rs b/vendor/toml_edit-0.19.11/src/ser/map.rs new file mode 100644 index 000000000..d743e3d5d --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/map.rs @@ -0,0 +1,405 @@ +use super::{Error, KeySerializer}; + +#[doc(hidden)] +pub enum SerializeMap { + Datetime(SerializeDatetime), + Table(SerializeInlineTable), +} + +impl SerializeMap { + pub(crate) fn table() -> Self { + Self::Table(SerializeInlineTable::new()) + } + + pub(crate) fn table_with_capacity(len: usize) -> Self { + Self::Table(SerializeInlineTable::with_capacity(len)) + } + + pub(crate) fn datetime() -> Self { + Self::Datetime(SerializeDatetime::new()) + } +} + +impl serde::ser::SerializeMap for SerializeMap { + type Ok = crate::Value; + type Error = Error; + + fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + match self { + Self::Datetime(s) => s.serialize_key(input), + Self::Table(s) => s.serialize_key(input), + } + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + match self { + Self::Datetime(s) => s.serialize_value(value), + Self::Table(s) => s.serialize_value(value), + } + } + + fn end(self) -> Result { + match self { + Self::Datetime(s) => s.end().map(|items| items.into()), + Self::Table(s) => s.end().map(|items| items.into()), + } + } +} + +impl serde::ser::SerializeStruct for SerializeMap { + type Ok = crate::Value; + type Error = Error; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + match self { + Self::Datetime(s) => s.serialize_field(key, value), + Self::Table(s) => s.serialize_field(key, value), + } + } + + fn end(self) -> Result { + match self { + Self::Datetime(s) => s.end().map(|items| items.into()), + Self::Table(s) => s.end().map(|items| items.into()), + } + } +} + +#[doc(hidden)] +pub struct SerializeDatetime { + value: Option, +} + +impl SerializeDatetime { + pub(crate) fn new() -> Self { + Self { value: None } + } +} + +impl serde::ser::SerializeMap for SerializeDatetime { + type Ok = crate::Datetime; + type Error = Error; + + fn serialize_key(&mut self, _input: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + unreachable!("datetimes should only be serialized as structs, not maps") + } + + fn serialize_value(&mut self, _value: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + unreachable!("datetimes should only be serialized as structs, not maps") + } + + fn end(self) -> Result { + unreachable!("datetimes should only be serialized as structs, not maps") + } +} + +impl serde::ser::SerializeStruct for SerializeDatetime { + type Ok = crate::Datetime; + type Error = Error; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + if key == toml_datetime::__unstable::FIELD { + self.value = Some(value.serialize(DatetimeFieldSerializer::default())?); + } + + Ok(()) + } + + fn end(self) -> Result { + self.value.ok_or(Error::UnsupportedNone) + } +} + +#[doc(hidden)] +pub struct SerializeInlineTable { + items: crate::table::KeyValuePairs, + key: Option, +} + +impl SerializeInlineTable { + pub(crate) fn new() -> Self { + Self { + items: Default::default(), + key: Default::default(), + } + } + + pub(crate) fn with_capacity(len: usize) -> Self { + let mut s = Self::new(); + s.items.reserve(len); + s + } +} + +impl serde::ser::SerializeMap for SerializeInlineTable { + type Ok = crate::InlineTable; + type Error = Error; + + fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + self.key = None; + self.key = Some(input.serialize(KeySerializer)?); + Ok(()) + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + let res = value.serialize(super::ValueSerializer {}); + match res { + Ok(item) => { + let key = self.key.take().unwrap(); + let kv = crate::table::TableKeyValue::new( + crate::Key::new(&key), + crate::Item::Value(item), + ); + self.items.insert(key, kv); + } + Err(e) => { + if e != Error::UnsupportedNone { + return Err(e); + } + } + } + Ok(()) + } + + fn end(self) -> Result { + Ok(crate::InlineTable::with_pairs(self.items)) + } +} + +impl serde::ser::SerializeStruct for SerializeInlineTable { + type Ok = crate::InlineTable; + type Error = Error; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: serde::ser::Serialize, + { + let res = value.serialize(super::ValueSerializer {}); + match res { + Ok(item) => { + let kv = crate::table::TableKeyValue::new( + crate::Key::new(key), + crate::Item::Value(item), + ); + self.items.insert(crate::InternalString::from(key), kv); + } + Err(e) => { + if e != Error::UnsupportedNone { + return Err(e); + } + } + }; + Ok(()) + } + + fn end(self) -> Result { + Ok(crate::InlineTable::with_pairs(self.items)) + } +} + +#[derive(Default)] +struct DatetimeFieldSerializer {} + +impl serde::ser::Serializer for DatetimeFieldSerializer { + type Ok = toml_datetime::Datetime; + type Error = Error; + type SerializeSeq = serde::ser::Impossible; + type SerializeTuple = serde::ser::Impossible; + type SerializeTupleStruct = serde::ser::Impossible; + type SerializeTupleVariant = serde::ser::Impossible; + type SerializeMap = serde::ser::Impossible; + type SerializeStruct = serde::ser::Impossible; + type SerializeStructVariant = serde::ser::Impossible; + + fn serialize_bool(self, _value: bool) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_i8(self, _value: i8) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_i16(self, _value: i16) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_i32(self, _value: i32) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_i64(self, _value: i64) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_u8(self, _value: u8) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_u16(self, _value: u16) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_u32(self, _value: u32) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_u64(self, _value: u64) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_f32(self, _value: f32) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_f64(self, _value: f64) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_char(self, _value: char) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_str(self, v: &str) -> Result { + v.parse::().map_err(Error::custom) + } + + fn serialize_bytes(self, _value: &[u8]) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_none(self) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_some(self, _value: &T) -> Result + where + T: serde::ser::Serialize, + { + Err(Error::DateInvalid) + } + + fn serialize_unit(self) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + ) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + _value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + Err(Error::DateInvalid) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + Err(Error::DateInvalid) + } + + fn serialize_seq(self, _len: Option) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_map(self, _len: Option) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(Error::DateInvalid) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(Error::DateInvalid) + } +} diff --git a/vendor/toml_edit-0.19.11/src/ser/mod.rs b/vendor/toml_edit-0.19.11/src/ser/mod.rs new file mode 100644 index 000000000..2c310206b --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/mod.rs @@ -0,0 +1,165 @@ +//! Serializing Rust structures into TOML. +//! +//! This module contains all the Serde support for serializing Rust structures into TOML. + +mod array; +mod key; +mod map; +mod pretty; +mod value; + +pub(crate) use array::*; +pub(crate) use key::*; +pub(crate) use map::*; + +use crate::visit_mut::VisitMut; + +/// Errors that can occur when deserializing a type. +#[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub enum Error { + /// Type could not be serialized to TOML + UnsupportedType(Option<&'static str>), + /// Value was out of range for the given type + OutOfRange(Option<&'static str>), + /// `None` could not be serialized to TOML + UnsupportedNone, + /// Key was not convertable to `String` for serializing to TOML + KeyNotString, + /// A serialized date was invalid + DateInvalid, + /// Other serialization error + Custom(String), +} + +impl Error { + pub(crate) fn custom(msg: T) -> Self + where + T: std::fmt::Display, + { + Error::Custom(msg.to_string()) + } +} + +impl serde::ser::Error for Error { + fn custom(msg: T) -> Self + where + T: std::fmt::Display, + { + Self::custom(msg) + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"), + Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"), + Self::OutOfRange(Some(t)) => write!(formatter, "out-of-range value for {t} type"), + Self::OutOfRange(None) => write!(formatter, "out-of-range value"), + Self::UnsupportedNone => "unsupported None value".fmt(formatter), + Self::KeyNotString => "map key was not a string".fmt(formatter), + Self::DateInvalid => "a serialized date was invalid".fmt(formatter), + Self::Custom(s) => s.fmt(formatter), + } + } +} + +impl From for Error { + fn from(e: crate::TomlError) -> Error { + Self::custom(e) + } +} + +impl From for crate::TomlError { + fn from(e: Error) -> crate::TomlError { + Self::custom(e.to_string(), None) + } +} + +impl std::error::Error for Error {} + +/// 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(value: &T) -> Result, Error> +where + T: serde::ser::Serialize, +{ + 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. +/// +/// # Examples +/// +/// ``` +/// use serde::Serialize; +/// +/// #[derive(Serialize)] +/// struct Config { +/// database: Database, +/// } +/// +/// #[derive(Serialize)] +/// struct Database { +/// ip: String, +/// port: Vec, +/// connection_max: u32, +/// enabled: bool, +/// } +/// +/// let config = Config { +/// database: Database { +/// ip: "192.168.1.1".to_string(), +/// port: vec![8001, 8002, 8003], +/// connection_max: 5000, +/// enabled: false, +/// }, +/// }; +/// +/// let toml = toml_edit::ser::to_string(&config).unwrap(); +/// println!("{}", toml) +/// ``` +pub fn to_string(value: &T) -> Result +where + T: serde::ser::Serialize, +{ + to_document(value).map(|e| e.to_string()) +} + +/// 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 `ValueSerializer::pretty` for more details. +pub fn to_string_pretty(value: &T) -> Result +where + T: serde::ser::Serialize, +{ + let mut document = to_document(value)?; + pretty::Pretty.visit_document_mut(&mut document); + Ok(document.to_string()) +} + +/// Serialize the given data structure into a TOML document. +/// +/// This would allow custom formatting to be applied, mixing with format preserving edits, etc. +pub fn to_document(value: &T) -> Result +where + T: serde::ser::Serialize, +{ + let value = value.serialize(ValueSerializer::new())?; + let item = crate::Item::Value(value); + let root = item + .into_table() + .map_err(|_| Error::UnsupportedType(None))?; + Ok(root.into()) +} + +pub use value::ValueSerializer; diff --git a/vendor/toml_edit-0.19.11/src/ser/pretty.rs b/vendor/toml_edit-0.19.11/src/ser/pretty.rs new file mode 100644 index 000000000..2c22f6804 --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/pretty.rs @@ -0,0 +1,45 @@ +pub(crate) struct Pretty; + +impl crate::visit_mut::VisitMut for Pretty { + fn visit_document_mut(&mut self, node: &mut crate::Document) { + crate::visit_mut::visit_document_mut(self, node); + } + + fn visit_item_mut(&mut self, node: &mut crate::Item) { + node.make_item(); + + crate::visit_mut::visit_item_mut(self, node); + } + + fn visit_table_mut(&mut self, node: &mut crate::Table) { + node.decor_mut().clear(); + + // Empty tables could be semantically meaningful, so make sure they are not implicit + if !node.is_empty() { + node.set_implicit(true); + } + + crate::visit_mut::visit_table_mut(self, node); + } + + fn visit_value_mut(&mut self, node: &mut crate::Value) { + node.decor_mut().clear(); + + crate::visit_mut::visit_value_mut(self, node); + } + + fn visit_array_mut(&mut self, node: &mut crate::Array) { + crate::visit_mut::visit_array_mut(self, node); + + if (0..=1).contains(&node.len()) { + node.set_trailing(""); + node.set_trailing_comma(false); + } else { + for item in node.iter_mut() { + item.decor_mut().set_prefix("\n "); + } + node.set_trailing("\n"); + node.set_trailing_comma(true); + } + } +} diff --git a/vendor/toml_edit-0.19.11/src/ser/value.rs b/vendor/toml_edit-0.19.11/src/ser/value.rs new file mode 100644 index 000000000..d29390a4c --- /dev/null +++ b/vendor/toml_edit-0.19.11/src/ser/value.rs @@ -0,0 +1,243 @@ +use super::Error; + +/// Serialization for TOML [values][crate::Value]. +/// +/// 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. +/// +/// # Examples +/// +/// ``` +/// use serde::Serialize; +/// +/// #[derive(Serialize)] +/// struct Config { +/// database: Database, +/// } +/// +/// #[derive(Serialize)] +/// struct Database { +/// ip: String, +/// port: Vec, +/// connection_max: u32, +/// enabled: bool, +/// } +/// +/// let config = Config { +/// database: Database { +/// ip: "192.168.1.1".to_string(), +/// port: vec![8001, 8002, 8003], +/// connection_max: 5000, +/// enabled: false, +/// }, +/// }; +/// +/// let value = serde::Serialize::serialize( +/// &config, +/// toml_edit::ser::ValueSerializer::new() +/// ).unwrap(); +/// println!("{}", value) +/// ``` +#[derive(Default)] +#[non_exhaustive] +pub struct ValueSerializer {} + +impl ValueSerializer { + /// Creates a new serializer generate a TOML document. + pub fn new() -> Self { + Self {} + } +} + +impl serde::ser::Serializer for ValueSerializer { + type Ok = crate::Value; + type Error = Error; + type SerializeSeq = super::SerializeValueArray; + type SerializeTuple = super::SerializeValueArray; + type SerializeTupleStruct = super::SerializeValueArray; + type SerializeTupleVariant = super::SerializeValueArray; + type SerializeMap = super::SerializeMap; + type SerializeStruct = super::SerializeMap; + type SerializeStructVariant = serde::ser::Impossible; + + fn serialize_bool(self, v: bool) -> Result { + Ok(v.into()) + } + + fn serialize_i8(self, v: i8) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_i16(self, v: i16) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_i32(self, v: i32) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_i64(self, v: i64) -> Result { + Ok(v.into()) + } + + fn serialize_u8(self, v: u8) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_u16(self, v: u16) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_u32(self, v: u32) -> Result { + self.serialize_i64(v as i64) + } + + fn serialize_u64(self, v: u64) -> Result { + let v: i64 = v + .try_into() + .map_err(|_err| Error::OutOfRange(Some("u64")))?; + self.serialize_i64(v) + } + + fn serialize_f32(self, v: f32) -> Result { + self.serialize_f64(v as f64) + } + + fn serialize_f64(self, v: f64) -> Result { + Ok(v.into()) + } + + fn serialize_char(self, v: char) -> Result { + let mut buf = [0; 4]; + self.serialize_str(v.encode_utf8(&mut buf)) + } + + fn serialize_str(self, v: &str) -> Result { + Ok(v.into()) + } + + fn serialize_bytes(self, value: &[u8]) -> Result { + use serde::ser::Serialize; + value.serialize(self) + } + + fn serialize_none(self) -> Result { + Err(Error::UnsupportedNone) + } + + fn serialize_some(self, value: &T) -> Result + where + T: serde::ser::Serialize, + { + value.serialize(self) + } + + fn serialize_unit(self) -> Result { + Err(Error::UnsupportedType(Some("unit"))) + } + + fn serialize_unit_struct(self, name: &'static str) -> Result { + Err(Error::UnsupportedType(Some(name))) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { + self.serialize_str(variant) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + value.serialize(self) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result + where + T: serde::ser::Serialize, + { + let value = value.serialize(self)?; + let mut table = crate::InlineTable::new(); + table.insert(variant, value); + Ok(table.into()) + } + + fn serialize_seq(self, len: Option) -> Result { + let serializer = match len { + Some(len) => super::SerializeValueArray::with_capacity(len), + None => super::SerializeValueArray::new(), + }; + Ok(serializer) + } + + fn serialize_tuple(self, len: usize) -> Result { + self.serialize_seq(Some(len)) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + len: usize, + ) -> Result { + self.serialize_seq(Some(len)) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + len: usize, + ) -> Result { + self.serialize_seq(Some(len)) + } + + fn serialize_map(self, len: Option) -> Result { + let serializer = match len { + Some(len) => super::SerializeMap::table_with_capacity(len), + None => super::SerializeMap::table(), + }; + Ok(serializer) + } + + fn serialize_struct( + self, + name: &'static str, + len: usize, + ) -> Result { + if name == toml_datetime::__unstable::NAME { + Ok(super::SerializeMap::datetime()) + } else { + self.serialize_map(Some(len)) + } + } + + fn serialize_struct_variant( + self, + name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(Error::UnsupportedType(Some(name))) + } +} -- cgit v1.2.3