From dbe9f878c920aaddb1db5c287cb5e30e453dba59 Mon Sep 17 00:00:00 2001 From: Lukas Friman Date: Tue, 22 Jul 2025 15:59:43 +0200 Subject: [PATCH 1/2] feat(core): Infallible error types. --- xmlity/src/types/infallible.rs | 51 ++++++++++++++++++++++++++++++++++ xmlity/src/types/mod.rs | 1 + 2 files changed, 52 insertions(+) create mode 100644 xmlity/src/types/infallible.rs diff --git a/xmlity/src/types/infallible.rs b/xmlity/src/types/infallible.rs new file mode 100644 index 0000000..7d19724 --- /dev/null +++ b/xmlity/src/types/infallible.rs @@ -0,0 +1,51 @@ +use std::convert::Infallible; + +impl crate::de::Error for Infallible { + fn custom(msg: T) -> Self + where + T: std::fmt::Display, + { + panic!("Infallible error: {}", msg); + } + + fn wrong_name(name: &crate::ExpandedName<'_>, expected: &crate::ExpandedName<'_>) -> Self { + panic!("Infallible error: wrong name \"{name}\", expected \"{expected}\""); + } + + fn unexpected_visit(unexpected: crate::de::Unexpected, _expected: &T) -> Self { + panic!("Infallible error: unexpected visit of {unexpected}"); + } + + fn missing_field(field: &str) -> Self { + panic!("Infallible error: missing field {field}"); + } + + fn no_possible_variant(ident: &str) -> Self { + panic!("Infallible error: no possible variant {ident}"); + } + + fn missing_data() -> Self { + panic!("Infallible error: missing data"); + } + + fn unknown_child() -> Self { + panic!("Infallible error: unknown child"); + } + + fn invalid_string() -> Self { + panic!("Infallible error: invalid string"); + } +} + +impl crate::ser::Error for Infallible { + fn custom(msg: T) -> Self + where + T: std::fmt::Display, + { + panic!("Infallible error: {}", msg); + } + + fn unexpected_serialize(unexpected: crate::ser::Unexpected) -> Self { + panic!("Infallible error: unexpected serialize of {unexpected}"); + } +} diff --git a/xmlity/src/types/mod.rs b/xmlity/src/types/mod.rs index 8e4121b..3965ca8 100644 --- a/xmlity/src/types/mod.rs +++ b/xmlity/src/types/mod.rs @@ -3,6 +3,7 @@ //! It also contains some visitors for the types which can be reused, including [`iterator::IteratorVisitor`]. pub mod common; +mod infallible; pub mod iterator; mod primitive; mod smart; From c64ece5aa3f50b08966745fd1ed940c1068436e6 Mon Sep 17 00:00:00 2001 From: Lukas Friman Date: Tue, 22 Jul 2025 16:01:55 +0200 Subject: [PATCH 2/2] `cargo clippy` --- xmlity/src/types/infallible.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmlity/src/types/infallible.rs b/xmlity/src/types/infallible.rs index 7d19724..bf06d13 100644 --- a/xmlity/src/types/infallible.rs +++ b/xmlity/src/types/infallible.rs @@ -5,7 +5,7 @@ impl crate::de::Error for Infallible { where T: std::fmt::Display, { - panic!("Infallible error: {}", msg); + panic!("Infallible error: {msg}"); } fn wrong_name(name: &crate::ExpandedName<'_>, expected: &crate::ExpandedName<'_>) -> Self { @@ -42,7 +42,7 @@ impl crate::ser::Error for Infallible { where T: std::fmt::Display, { - panic!("Infallible error: {}", msg); + panic!("Infallible error: {msg}"); } fn unexpected_serialize(unexpected: crate::ser::Unexpected) -> Self {