use alloc::borrow::Cow;
use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SerialError {
UnexpectedEof {
needed: usize,
remaining: usize,
},
InvalidLength {
declared: u64,
remaining: usize,
},
VarintOverflow,
IntegerOutOfRange,
InvalidBool {
byte: u8,
},
InvalidUtf8,
InvalidTag {
kind: &'static str,
tag: u8,
},
UnknownVariant {
kind: &'static str,
index: u64,
},
TrailingBytes {
remaining: usize,
},
#[cfg(feature = "std")]
Io {
kind: std::io::ErrorKind,
message: alloc::string::String,
},
DepthLimitExceeded {
max_depth: usize,
},
NonCanonical {
reason: &'static str,
},
InvalidVersion {
version: u64,
},
InvalidValue {
kind: &'static str,
},
InvalidConfig {
reason: &'static str,
},
Custom {
message: Cow<'static, str>,
},
}
impl fmt::Display for SerialError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEof { needed, remaining } => write!(
f,
"unexpected end of input: needed {needed} more byte(s), {remaining} remaining"
),
Self::InvalidLength {
declared,
remaining,
} => write!(
f,
"length prefix exceeds remaining buffer: declared {declared}, remaining {remaining}"
),
Self::VarintOverflow => {
f.write_str("varint exceeds the maximum byte count for its target width")
}
Self::IntegerOutOfRange => {
f.write_str("decoded integer does not fit in the requested width")
}
Self::InvalidBool { byte } => write!(f, "invalid boolean byte: 0x{byte:02x}"),
Self::InvalidUtf8 => f.write_str("length-prefixed bytes were not valid UTF-8"),
Self::InvalidTag { kind, tag } => write!(f, "invalid {kind} tag: 0x{tag:02x}"),
Self::UnknownVariant { kind, index } => {
write!(f, "unknown {kind} variant index: {index}")
}
Self::TrailingBytes { remaining } => {
write!(
f,
"trailing input after strict decode: {remaining} byte(s) unread"
)
}
Self::DepthLimitExceeded { max_depth } => {
write!(
f,
"nesting depth exceeds the configured limit of {max_depth}"
)
}
Self::NonCanonical { reason } => {
write!(
f,
"non-canonical encoding rejected in canonical mode: {reason}"
)
}
Self::InvalidVersion { version } => {
write!(
f,
"invalid schema version {version} (version 0 is reserved)"
)
}
Self::InvalidValue { kind } => {
write!(f, "decoded value is not a valid {kind}")
}
Self::InvalidConfig { reason } => write!(f, "invalid decoder configuration: {reason}"),
Self::Custom { message } => f.write_str(message),
#[cfg(feature = "std")]
Self::Io { kind, message } => write!(f, "I/O error ({kind:?}): {message}"),
}
}
}
impl SerialError {
#[must_use]
pub fn custom(message: impl Into<Cow<'static, str>>) -> Self {
Self::Custom {
message: message.into(),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for SerialError {}
#[cfg(all(feature = "serde", not(feature = "std")))]
impl serde::ser::StdError for SerialError {}
#[cfg(feature = "serde")]
impl serde::ser::Error for SerialError {
fn custom<T: fmt::Display>(msg: T) -> Self {
use alloc::string::ToString;
Self::custom(msg.to_string())
}
}
#[cfg(feature = "serde")]
impl serde::de::Error for SerialError {
fn custom<T: fmt::Display>(msg: T) -> Self {
use alloc::string::ToString;
Self::custom(msg.to_string())
}
}
pub type Result<T> = core::result::Result<T, SerialError>;
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
use alloc::string::ToString;
#[test]
fn display_unexpected_eof_reports_counts() {
let err = SerialError::UnexpectedEof {
needed: 4,
remaining: 1,
};
let msg = err.to_string();
assert!(msg.contains("needed 4"));
assert!(msg.contains("1 remaining"));
}
#[test]
fn display_invalid_length_reports_declared_and_remaining() {
let err = SerialError::InvalidLength {
declared: 1 << 20,
remaining: 16,
};
let msg = err.to_string();
assert!(msg.contains("1048576"));
assert!(msg.contains("16"));
}
#[test]
fn display_invalid_bool_is_hex_with_zero_pad() {
let err = SerialError::InvalidBool { byte: 0x2a };
assert_eq!(err.to_string(), "invalid boolean byte: 0x2a");
}
#[test]
fn display_invalid_tag_carries_kind_and_byte() {
let err = SerialError::InvalidTag {
kind: "Option",
tag: 0x7f,
};
assert!(err.to_string().contains("Option"));
assert!(err.to_string().contains("0x7f"));
}
#[test]
fn equality_distinguishes_variants() {
let a = SerialError::VarintOverflow;
let b = SerialError::VarintOverflow;
let c = SerialError::IntegerOutOfRange;
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn clone_preserves_variant() {
let err = SerialError::TrailingBytes { remaining: 8 };
let cloned = err.clone();
assert_eq!(err, cloned);
}
#[test]
fn test_display_of_variants_added_in_1_1() {
let cases = [
(
SerialError::DepthLimitExceeded { max_depth: 7 },
"nesting depth exceeds the configured limit of 7",
),
(
SerialError::NonCanonical { reason: "varint" },
"non-canonical encoding rejected in canonical mode: varint",
),
(
SerialError::InvalidVersion { version: 0 },
"invalid schema version 0 (version 0 is reserved)",
),
(
SerialError::InvalidValue { kind: "char" },
"decoded value is not a valid char",
),
(
SerialError::InvalidConfig { reason: "x" },
"invalid decoder configuration: x",
),
(SerialError::custom("boom"), "boom"),
(
SerialError::UnknownVariant {
kind: "E",
index: 9,
},
"unknown E variant index: 9",
),
];
for (err, expected) in cases {
assert_eq!(err.to_string(), expected);
}
}
#[test]
fn test_custom_accepts_static_and_owned_messages() {
let a = SerialError::custom("static");
let b = SerialError::custom(alloc::string::String::from("static"));
assert_eq!(a, b);
assert!(matches!(
a,
SerialError::Custom {
message: Cow::Borrowed("static")
}
));
}
#[test]
fn debug_format_does_not_panic() {
let _ = format!("{:?}", SerialError::InvalidUtf8);
}
}