From 36c00af60f8ff1ebd8d69705c762dddb489d7bd4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 19 Jan 2023 11:49:43 -0600 Subject: [PATCH] feat(serde): Publicly expose toml_edit's ser::Error variants --- crates/toml_edit/src/ser/key.rs | 52 +++++++++++++++---------------- crates/toml_edit/src/ser/mod.rs | 52 +++++++++++-------------------- crates/toml_edit/src/ser/table.rs | 6 ++-- crates/toml_edit/src/ser/value.rs | 12 +++---- 4 files changed, 54 insertions(+), 68 deletions(-) diff --git a/crates/toml_edit/src/ser/key.rs b/crates/toml_edit/src/ser/key.rs index 0265fb2d..d5e381bf 100644 --- a/crates/toml_edit/src/ser/key.rs +++ b/crates/toml_edit/src/ser/key.rs @@ -1,6 +1,6 @@ use crate::InternalString; -use super::{Error, ErrorKind}; +use super::Error; pub(crate) struct KeySerializer; @@ -16,51 +16,51 @@ impl serde::ser::Serializer for KeySerializer { type SerializeStructVariant = serde::ser::Impossible; fn serialize_bool(self, _v: bool) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_i8(self, _v: i8) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_i16(self, _v: i16) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_i32(self, _v: i32) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_i64(self, _v: i64) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_u8(self, _v: u8) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_u16(self, _v: u16) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_u32(self, _v: u32) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_u64(self, _v: u64) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_f32(self, _v: f32) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_f64(self, _v: f64) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_char(self, _v: char) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_str(self, value: &str) -> Result { @@ -68,26 +68,26 @@ impl serde::ser::Serializer for KeySerializer { } fn serialize_bytes(self, _value: &[u8]) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_none(self) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_some(self, _value: &T) -> Result where T: serde::ser::Serialize, { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_unit(self) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_unit_struct(self, _name: &'static str) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_unit_variant( @@ -120,15 +120,15 @@ impl serde::ser::Serializer for KeySerializer { where T: serde::ser::Serialize, { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_seq(self, _len: Option) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_tuple(self, _len: usize) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_tuple_struct( @@ -136,7 +136,7 @@ impl serde::ser::Serializer for KeySerializer { _name: &'static str, _len: usize, ) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_tuple_variant( @@ -146,11 +146,11 @@ impl serde::ser::Serializer for KeySerializer { _variant: &'static str, _len: usize, ) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_map(self, _len: Option) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_struct( @@ -158,7 +158,7 @@ impl serde::ser::Serializer for KeySerializer { _name: &'static str, _len: usize, ) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } fn serialize_struct_variant( @@ -168,6 +168,6 @@ impl serde::ser::Serializer for KeySerializer { _variant: &'static str, _len: usize, ) -> Result { - Err(ErrorKind::KeyNotString.into()) + Err(Error::KeyNotString) } } diff --git a/crates/toml_edit/src/ser/mod.rs b/crates/toml_edit/src/ser/mod.rs index 33ddffee..e72b1d18 100644 --- a/crates/toml_edit/src/ser/mod.rs +++ b/crates/toml_edit/src/ser/mod.rs @@ -16,8 +16,16 @@ use crate::visit_mut::VisitMut; /// Errors that can occur when deserializing a type. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Error { - kind: ErrorKind, +#[non_exhaustive] +pub enum Error { + /// Type could not be serialized to TOML + UnsupportedType(Option<&'static str>), + /// `None` could not be serialized to TOML + UnsupportedNone, + /// Key was not convertable to `String` for serializing to TOML + KeyNotString, + /// Other serialization error + Custom(String), } impl Error { @@ -25,9 +33,7 @@ impl Error { where T: std::fmt::Display, { - Error { - kind: ErrorKind::Custom(msg.to_string()), - } + Error::Custom(msg.to_string()) } } @@ -42,7 +48,13 @@ impl serde::ser::Error for Error { impl std::fmt::Display for Error { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - self.kind.fmt(formatter) + match self { + Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"), + Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"), + Self::UnsupportedNone => "unsupported None value".fmt(formatter), + Self::KeyNotString => "map key was not a string".fmt(formatter), + Self::Custom(s) => s.fmt(formatter), + } } } @@ -60,32 +72,6 @@ impl From for crate::TomlError { impl std::error::Error for Error {} -impl From for Error { - fn from(kind: ErrorKind) -> Self { - Self { kind } - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -enum ErrorKind { - UnsupportedType(Option<&'static str>), - UnsupportedNone, - KeyNotString, - Custom(String), -} - -impl std::fmt::Display for ErrorKind { - fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - ErrorKind::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"), - ErrorKind::UnsupportedType(None) => write!(formatter, "unsupported rust type"), - ErrorKind::UnsupportedNone => "unsupported None value".fmt(formatter), - ErrorKind::KeyNotString => "map key was not a string".fmt(formatter), - ErrorKind::Custom(s) => s.fmt(formatter), - } - } -} - /// Serialize the given data structure as a TOML byte vector. /// /// Serialization can fail if `T`'s implementation of `Serialize` decides to @@ -165,7 +151,7 @@ where let item = crate::Item::Value(value); let root = item .into_table() - .map_err(|_| ErrorKind::UnsupportedType(None))?; + .map_err(|_| Error::UnsupportedType(None))?; Ok(root.into()) } diff --git a/crates/toml_edit/src/ser/table.rs b/crates/toml_edit/src/ser/table.rs index 99a02b8a..81285bc4 100644 --- a/crates/toml_edit/src/ser/table.rs +++ b/crates/toml_edit/src/ser/table.rs @@ -1,4 +1,4 @@ -use super::{Error, ErrorKind, KeySerializer}; +use super::{Error, KeySerializer}; #[doc(hidden)] pub struct SerializeValueTable { @@ -114,7 +114,7 @@ impl serde::ser::SerializeMap for SerializeKeyValuePairs { self.items.insert(key, kv); } Err(e) => { - if e.kind != ErrorKind::UnsupportedNone { + if e != Error::UnsupportedNone { return Err(e); } } @@ -149,7 +149,7 @@ impl serde::ser::SerializeStruct for SerializeKeyValuePairs { self.items.insert(crate::InternalString::from(key), kv); } Err(e) => { - if e.kind != ErrorKind::UnsupportedNone { + if e != Error::UnsupportedNone { return Err(e); } } diff --git a/crates/toml_edit/src/ser/value.rs b/crates/toml_edit/src/ser/value.rs index b6b3cad0..089c93c6 100644 --- a/crates/toml_edit/src/ser/value.rs +++ b/crates/toml_edit/src/ser/value.rs @@ -1,4 +1,4 @@ -use super::{Error, ErrorKind}; +use super::Error; /// Serialization implementation for TOML. /// @@ -90,7 +90,7 @@ impl serde::ser::Serializer for ValueSerializer { } fn serialize_none(self) -> Result { - Err(ErrorKind::UnsupportedNone.into()) + Err(Error::UnsupportedNone) } fn serialize_some(self, value: &T) -> Result @@ -101,11 +101,11 @@ impl serde::ser::Serializer for ValueSerializer { } fn serialize_unit(self) -> Result { - Err(ErrorKind::UnsupportedType(Some("unit")).into()) + Err(Error::UnsupportedType(Some("unit"))) } fn serialize_unit_struct(self, name: &'static str) -> Result { - Err(ErrorKind::UnsupportedType(Some(name)).into()) + Err(Error::UnsupportedType(Some(name))) } fn serialize_unit_variant( @@ -138,7 +138,7 @@ impl serde::ser::Serializer for ValueSerializer { where T: serde::ser::Serialize, { - Err(ErrorKind::UnsupportedType(Some(name)).into()) + Err(Error::UnsupportedType(Some(name))) } fn serialize_seq(self, len: Option) -> Result { @@ -194,6 +194,6 @@ impl serde::ser::Serializer for ValueSerializer { _variant: &'static str, _len: usize, ) -> Result { - Err(ErrorKind::UnsupportedType(Some(name)).into()) + Err(Error::UnsupportedType(Some(name))) } }