From 23e727dcbd8ebc26e246e6c4861ecaed19ed3cc6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 18 Jan 2023 21:18:41 -0600 Subject: [PATCH] fix(ser)!: Clarify its a `toml_edit::ser::ValueSerializer` As a value can easily be turned into a document, if supported, I'm not going to support a regular `Serializer`. --- crates/toml_edit/src/easy/mod.rs | 2 +- crates/toml_edit/src/easy/value.rs | 8 +-- crates/toml_edit/src/ser/array.rs | 28 ++++---- crates/toml_edit/src/ser/mod.rs | 10 +-- crates/toml_edit/src/ser/table.rs | 64 +++++++++---------- .../toml_edit/src/ser/{item.rs => value.rs} | 36 +++++------ 6 files changed, 71 insertions(+), 77 deletions(-) rename crates/toml_edit/src/ser/{item.rs => value.rs} (85%) diff --git a/crates/toml_edit/src/easy/mod.rs b/crates/toml_edit/src/easy/mod.rs index fbf081c1..86e3d509 100644 --- a/crates/toml_edit/src/easy/mod.rs +++ b/crates/toml_edit/src/easy/mod.rs @@ -146,7 +146,7 @@ pub use crate::toml; #[doc(no_inline)] pub use de::{from_document, from_slice, from_str, Deserializer}; #[doc(no_inline)] -pub use ser::{to_document, to_string, to_string_pretty, to_vec, Serializer}; +pub use ser::{to_document, to_string, to_string_pretty, to_vec, ValueSerializer}; pub use serde_spanned::Spanned; #[doc(no_inline)] pub use value::Value; diff --git a/crates/toml_edit/src/easy/value.rs b/crates/toml_edit/src/easy/value.rs index 9e706518..4be4cad2 100644 --- a/crates/toml_edit/src/easy/value.rs +++ b/crates/toml_edit/src/easy/value.rs @@ -52,8 +52,7 @@ impl Value { where T: serde::ser::Serialize, { - let item = value.serialize(super::Serializer::new())?; - let value = item.into_value().expect("`to_item` only generates values"); + let value = value.serialize(super::ValueSerializer::new())?; let value = value.into_deserializer(); let target = Self::deserialize(value)?; Ok(target) @@ -72,8 +71,7 @@ impl Value { where T: serde::de::DeserializeOwned, { - let item = (&self).serialize(super::Serializer::new())?; - let value = item.into_value().expect("`to_item` only generates values"); + let value = (&self).serialize(super::ValueSerializer::new())?; let value = value.into_deserializer(); let target = T::deserialize(value)?; Ok(target) @@ -245,7 +243,7 @@ impl std::fmt::Display for Value { | Value::Boolean(..) | Value::Datetime(..) | Value::Array(..) => self - .serialize(super::Serializer::new()) + .serialize(super::ValueSerializer::new()) .map_err(|_| std::fmt::Error)? .fmt(f), Value::Table(_) => crate::ser::to_string_pretty(self) diff --git a/crates/toml_edit/src/ser/array.rs b/crates/toml_edit/src/ser/array.rs index 03eaa28b..80eba8ba 100644 --- a/crates/toml_edit/src/ser/array.rs +++ b/crates/toml_edit/src/ser/array.rs @@ -1,11 +1,11 @@ use super::Error; #[doc(hidden)] -pub struct SerializeItemArray { +pub struct SerializeValueArray { values: Vec, } -impl SerializeItemArray { +impl SerializeValueArray { pub(crate) fn new() -> Self { Self { values: Vec::new() } } @@ -17,28 +17,26 @@ impl SerializeItemArray { } } -impl serde::ser::SerializeSeq for SerializeItemArray { - type Ok = crate::Item; +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::ItemSerializer {})?; - self.values.push(value); + let value = value.serialize(super::ValueSerializer {})?; + self.values.push(crate::Item::Value(value)); Ok(()) } fn end(self) -> Result { - Ok(crate::Item::Value(crate::Value::Array( - crate::Array::with_vec(self.values), - ))) + Ok(crate::Value::Array(crate::Array::with_vec(self.values))) } } -impl serde::ser::SerializeTuple for SerializeItemArray { - type Ok = crate::Item; +impl serde::ser::SerializeTuple for SerializeValueArray { + type Ok = crate::Value; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> @@ -53,8 +51,8 @@ impl serde::ser::SerializeTuple for SerializeItemArray { } } -impl serde::ser::SerializeTupleVariant for SerializeItemArray { - type Ok = crate::Item; +impl serde::ser::SerializeTupleVariant for SerializeValueArray { + type Ok = crate::Value; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> @@ -69,8 +67,8 @@ impl serde::ser::SerializeTupleVariant for SerializeItemArray { } } -impl serde::ser::SerializeTupleStruct for SerializeItemArray { - type Ok = crate::Item; +impl serde::ser::SerializeTupleStruct for SerializeValueArray { + type Ok = crate::Value; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> diff --git a/crates/toml_edit/src/ser/mod.rs b/crates/toml_edit/src/ser/mod.rs index 409a08d3..33ddffee 100644 --- a/crates/toml_edit/src/ser/mod.rs +++ b/crates/toml_edit/src/ser/mod.rs @@ -3,13 +3,12 @@ //! This module contains all the Serde support for serializing Rust structures into TOML. mod array; -mod item; mod key; mod pretty; mod table; +mod value; pub(crate) use array::*; -pub(crate) use item::*; pub(crate) use key::*; pub(crate) use table::*; @@ -145,7 +144,7 @@ where /// 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. +/// "pretty" output. See `ValueSerializer::pretty` for more details. pub fn to_string_pretty(value: &T) -> Result where T: serde::ser::Serialize, @@ -162,11 +161,12 @@ pub fn to_document(value: &T) -> Result where T: serde::ser::Serialize, { - let item = value.serialize(Serializer::new())?; + let value = value.serialize(ValueSerializer::new())?; + let item = crate::Item::Value(value); let root = item .into_table() .map_err(|_| ErrorKind::UnsupportedType(None))?; Ok(root.into()) } -pub use item::ItemSerializer as Serializer; +pub use value::ValueSerializer; diff --git a/crates/toml_edit/src/ser/table.rs b/crates/toml_edit/src/ser/table.rs index 6cd5d523..99a02b8a 100644 --- a/crates/toml_edit/src/ser/table.rs +++ b/crates/toml_edit/src/ser/table.rs @@ -1,11 +1,11 @@ use super::{Error, ErrorKind, KeySerializer}; #[doc(hidden)] -pub struct SerializeItemTable { +pub struct SerializeValueTable { inner: SerializeKeyValuePairs, } -impl SerializeItemTable { +impl SerializeValueTable { pub(crate) fn new() -> Self { Self { inner: SerializeKeyValuePairs::new(), @@ -19,8 +19,8 @@ impl SerializeItemTable { } } -impl serde::ser::SerializeMap for SerializeItemTable { - type Ok = crate::Item; +impl serde::ser::SerializeMap for SerializeValueTable { + type Ok = crate::Value; type Error = Error; fn serialize_key(&mut self, input: &T) -> Result<(), Self::Error> @@ -38,16 +38,14 @@ impl serde::ser::SerializeMap for SerializeItemTable { } fn end(self) -> Result { - self.inner.end().map(|items| { - crate::Item::Value(crate::Value::InlineTable(crate::InlineTable::with_pairs( - items, - ))) - }) + self.inner + .end() + .map(|items| crate::Value::InlineTable(crate::InlineTable::with_pairs(items))) } } -impl serde::ser::SerializeStruct for SerializeItemTable { - type Ok = crate::Item; +impl serde::ser::SerializeStruct for SerializeValueTable { + type Ok = crate::Value; type Error = Error; fn serialize_field( @@ -62,11 +60,9 @@ impl serde::ser::SerializeStruct for SerializeItemTable { } fn end(self) -> Result { - self.inner.end().map(|items| { - crate::Item::Value(crate::Value::InlineTable(crate::InlineTable::with_pairs( - items, - ))) - }) + self.inner + .end() + .map(|items| crate::Value::InlineTable(crate::InlineTable::with_pairs(items))) } } @@ -107,20 +103,21 @@ impl serde::ser::SerializeMap for SerializeKeyValuePairs { where T: serde::ser::Serialize, { - let res = value.serialize(super::ItemSerializer {}); - let item = match res { - Ok(item) => item, + 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.kind != ErrorKind::UnsupportedNone { return Err(e); } - crate::Item::None } - }; - if !item.is_none() { - let key = self.key.take().unwrap(); - let kv = crate::table::TableKeyValue::new(crate::Key::new(&key), item); - self.items.insert(key, kv); } Ok(()) } @@ -142,20 +139,21 @@ impl serde::ser::SerializeStruct for SerializeKeyValuePairs { where T: serde::ser::Serialize, { - let res = value.serialize(super::ItemSerializer {}); - let item = match res { - Ok(item) => item, + 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.kind != ErrorKind::UnsupportedNone { return Err(e); } - crate::Item::None } }; - if !item.is_none() { - let kv = crate::table::TableKeyValue::new(crate::Key::new(key), item); - self.items.insert(crate::InternalString::from(key), kv); - } Ok(()) } diff --git a/crates/toml_edit/src/ser/item.rs b/crates/toml_edit/src/ser/value.rs similarity index 85% rename from crates/toml_edit/src/ser/item.rs rename to crates/toml_edit/src/ser/value.rs index a3f1cada..b6b3cad0 100644 --- a/crates/toml_edit/src/ser/item.rs +++ b/crates/toml_edit/src/ser/value.rs @@ -11,28 +11,28 @@ use super::{Error, ErrorKind}; /// which is passed in when creating the serializer itself. #[derive(Default)] #[non_exhaustive] -pub struct ItemSerializer {} +pub struct ValueSerializer {} -impl ItemSerializer { +impl ValueSerializer { /// Creates a new serializer generate a TOML document. pub fn new() -> Self { Self {} } } -impl serde::ser::Serializer for ItemSerializer { - type Ok = crate::Item; +impl serde::ser::Serializer for ValueSerializer { + type Ok = crate::Value; type Error = Error; - type SerializeSeq = super::SerializeItemArray; - type SerializeTuple = super::SerializeItemArray; - type SerializeTupleStruct = super::SerializeItemArray; - type SerializeTupleVariant = super::SerializeItemArray; - type SerializeMap = super::SerializeItemTable; - type SerializeStruct = super::SerializeItemTable; + type SerializeSeq = super::SerializeValueArray; + type SerializeTuple = super::SerializeValueArray; + type SerializeTupleStruct = super::SerializeValueArray; + type SerializeTupleVariant = super::SerializeValueArray; + type SerializeMap = super::SerializeValueTable; + type SerializeStruct = super::SerializeValueTable; type SerializeStructVariant = serde::ser::Impossible; fn serialize_bool(self, v: bool) -> Result { - Ok(crate::Item::Value(v.into())) + Ok(v.into()) } fn serialize_i8(self, v: i8) -> Result { @@ -48,7 +48,7 @@ impl serde::ser::Serializer for ItemSerializer { } fn serialize_i64(self, v: i64) -> Result { - Ok(crate::Item::Value(v.into())) + Ok(v.into()) } fn serialize_u8(self, v: u8) -> Result { @@ -72,7 +72,7 @@ impl serde::ser::Serializer for ItemSerializer { } fn serialize_f64(self, v: f64) -> Result { - Ok(crate::Item::Value(v.into())) + Ok(v.into()) } fn serialize_char(self, v: char) -> Result { @@ -81,7 +81,7 @@ impl serde::ser::Serializer for ItemSerializer { } fn serialize_str(self, v: &str) -> Result { - Ok(crate::Item::Value(v.into())) + Ok(v.into()) } fn serialize_bytes(self, value: &[u8]) -> Result { @@ -143,8 +143,8 @@ impl serde::ser::Serializer for ItemSerializer { fn serialize_seq(self, len: Option) -> Result { let serializer = match len { - Some(len) => super::SerializeItemArray::with_capacity(len), - None => super::SerializeItemArray::new(), + Some(len) => super::SerializeValueArray::with_capacity(len), + None => super::SerializeValueArray::new(), }; Ok(serializer) } @@ -173,8 +173,8 @@ impl serde::ser::Serializer for ItemSerializer { fn serialize_map(self, len: Option) -> Result { let serializer = match len { - Some(len) => super::SerializeItemTable::with_capacity(len), - None => super::SerializeItemTable::new(), + Some(len) => super::SerializeValueTable::with_capacity(len), + None => super::SerializeValueTable::new(), }; Ok(serializer) }