Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ser): Report the name of the unsupported type #464

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/toml_edit/src/ser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl serde::ser::Serializer for ItemSerializer {
}

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

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

fn serialize_unit_variant(
Expand All @@ -130,15 +130,15 @@ impl serde::ser::Serializer for ItemSerializer {

fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: serde::ser::Serialize,
{
Err(ErrorKind::UnsupportedType.into())
Err(ErrorKind::UnsupportedType(Some(name)).into())
}

fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Expand Down Expand Up @@ -189,11 +189,11 @@ impl serde::ser::Serializer for ItemSerializer {

fn serialize_struct_variant(
self,
_name: &'static str,
name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ErrorKind::UnsupportedType.into())
Err(ErrorKind::UnsupportedType(Some(name)).into())
}
}
9 changes: 6 additions & 3 deletions crates/toml_edit/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl From<ErrorKind> for Error {

#[derive(Debug, Clone, PartialEq, Eq)]
enum ErrorKind {
UnsupportedType,
UnsupportedType(Option<&'static str>),
UnsupportedNone,
KeyNotString,
Custom(String),
Expand All @@ -78,7 +78,8 @@ enum ErrorKind {
impl std::fmt::Display for ErrorKind {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ErrorKind::UnsupportedType => "unsupported Rust type".fmt(formatter),
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),
Expand Down Expand Up @@ -162,7 +163,9 @@ where
T: serde::ser::Serialize,
{
let item = to_item(value)?;
let root = item.into_table().map_err(|_| ErrorKind::UnsupportedType)?;
let root = item
.into_table()
.map_err(|_| ErrorKind::UnsupportedType(None))?;
Ok(root.into())
}

Expand Down
19 changes: 19 additions & 0 deletions crates/toml_edit/tests/testsuite/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,22 @@ fn float_max() {
Table(map! { a_b: Float(f64::MAX) }),
}
}

#[test]
fn unsupported_root_type() {
let native = "value";
let err = toml_edit::ser::to_string_pretty(&native).unwrap_err();
snapbox::assert_eq("unsupported rust type", err.to_string());
}

#[test]
fn unsupported_nested_type() {
#[derive(Debug, Serialize, Deserialize)]
struct Foo {
unused: (),
}

let native = Foo { unused: () };
let err = toml_edit::ser::to_string_pretty(&native).unwrap_err();
snapbox::assert_eq("unsupported unit type", err.to_string());
}