Skip to content

Commit

Permalink
Merge pull request #469 from epage/error
Browse files Browse the repository at this point in the history
feat(serde): Publicly expose toml_edit's ser::Error variants
  • Loading branch information
epage authored Jan 19, 2023
2 parents b774716 + 36c00af commit 6fe3305
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 68 deletions.
52 changes: 26 additions & 26 deletions crates/toml_edit/src/ser/key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::InternalString;

use super::{Error, ErrorKind};
use super::Error;

pub(crate) struct KeySerializer;

Expand All @@ -16,78 +16,78 @@ impl serde::ser::Serializer for KeySerializer {
type SerializeStructVariant = serde::ser::Impossible<InternalString, Error>;

fn serialize_bool(self, _v: bool) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_i8(self, _v: i8) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_i16(self, _v: i16) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_i32(self, _v: i32) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_i64(self, _v: i64) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_u8(self, _v: u8) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_u16(self, _v: u16) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_u32(self, _v: u32) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_u64(self, _v: u64) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_f32(self, _v: f32) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_f64(self, _v: f64) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_char(self, _v: char) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_str(self, value: &str) -> Result<InternalString, Self::Error> {
Ok(InternalString::from(value))
}

fn serialize_bytes(self, _value: &[u8]) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_none(self) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<InternalString, Self::Error>
where
T: serde::ser::Serialize,
{
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_unit(self) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_unit_struct(self, _name: &'static str) -> Result<InternalString, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_unit_variant(
Expand Down Expand Up @@ -120,23 +120,23 @@ impl serde::ser::Serializer for KeySerializer {
where
T: serde::ser::Serialize,
{
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_tuple_variant(
Expand All @@ -146,19 +146,19 @@ impl serde::ser::Serializer for KeySerializer {
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}

fn serialize_struct_variant(
Expand All @@ -168,6 +168,6 @@ impl serde::ser::Serializer for KeySerializer {
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ErrorKind::KeyNotString.into())
Err(Error::KeyNotString)
}
}
52 changes: 19 additions & 33 deletions crates/toml_edit/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ 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 {
pub(crate) fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Error {
kind: ErrorKind::Custom(msg.to_string()),
}
Error::Custom(msg.to_string())
}
}

Expand All @@ -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),
}
}
}

Expand All @@ -60,32 +72,6 @@ impl From<Error> for crate::TomlError {

impl std::error::Error for Error {}

impl From<ErrorKind> 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
Expand Down Expand Up @@ -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())
}

Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/ser/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Error, ErrorKind, KeySerializer};
use super::{Error, KeySerializer};

#[doc(hidden)]
pub struct SerializeValueTable {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/toml_edit/src/ser/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Error, ErrorKind};
use super::Error;

/// Serialization implementation for TOML.
///
Expand Down Expand Up @@ -90,7 +90,7 @@ impl serde::ser::Serializer for ValueSerializer {
}

fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Err(ErrorKind::UnsupportedNone.into())
Err(Error::UnsupportedNone)
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
Expand All @@ -101,11 +101,11 @@ impl serde::ser::Serializer for ValueSerializer {
}

fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(ErrorKind::UnsupportedType(Some("unit")).into())
Err(Error::UnsupportedType(Some("unit")))
}

fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(ErrorKind::UnsupportedType(Some(name)).into())
Err(Error::UnsupportedType(Some(name)))
}

fn serialize_unit_variant(
Expand Down Expand Up @@ -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<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Expand Down Expand Up @@ -194,6 +194,6 @@ impl serde::ser::Serializer for ValueSerializer {
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ErrorKind::UnsupportedType(Some(name)).into())
Err(Error::UnsupportedType(Some(name)))
}
}

0 comments on commit 6fe3305

Please sign in to comment.