#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct BufferTooShort {
pub need: usize,
pub have: usize,
pub what: &'static str,
}
impl BufferTooShort {
#[must_use]
pub const fn new(need: usize, have: usize, what: &'static str) -> Self {
Self { need, have, what }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct OutputBufferTooSmall {
pub need: usize,
pub have: usize,
pub what: &'static str,
}
impl OutputBufferTooSmall {
#[must_use]
pub const fn new(need: usize, have: usize, what: &'static str) -> Self {
Self { need, have, what }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FieldOverflow {
pub field: &'static str,
pub value: u64,
pub max: u64,
}
impl FieldOverflow {
#[must_use]
pub const fn new(field: &'static str, value: u64, max: u64) -> Self {
Self { field, value, max }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CrcMismatch {
pub what: &'static str,
pub computed: u32,
pub expected: u32,
}
impl CrcMismatch {
#[must_use]
pub const fn new(what: &'static str, computed: u32, expected: u32) -> Self {
Self {
what,
computed,
expected,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InvalidSync {
pub what: &'static str,
pub expected: u32,
pub found: u32,
}
impl InvalidSync {
#[must_use]
pub const fn new(what: &'static str, expected: u32, found: u32) -> Self {
Self {
what,
expected,
found,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ReservedBits {
pub field: &'static str,
pub expected: u64,
pub found: u64,
}
impl ReservedBits {
#[must_use]
pub const fn new(field: &'static str, expected: u64, found: u64) -> Self {
Self {
field,
expected,
found,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InvalidValue {
pub field: &'static str,
pub value: u64,
pub reason: &'static str,
}
impl InvalidValue {
#[must_use]
pub const fn new(field: &'static str, value: u64, reason: &'static str) -> Self {
Self {
field,
value,
reason,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InvalidField {
pub field: &'static str,
pub reason: &'static str,
}
impl InvalidField {
#[must_use]
pub const fn new(field: &'static str, reason: &'static str) -> Self {
Self { field, reason }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct UnsupportedVersion {
pub what: &'static str,
pub found: u32,
}
impl UnsupportedVersion {
#[must_use]
pub const fn new(what: &'static str, found: u32) -> Self {
Self { what, found }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct LengthMismatch {
pub what: &'static str,
pub declared: usize,
pub actual: usize,
}
impl LengthMismatch {
#[must_use]
pub const fn new(what: &'static str, declared: usize, actual: usize) -> Self {
Self {
what,
declared,
actual,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct LimitExceeded {
pub what: &'static str,
pub limit: u64,
pub value: u64,
}
impl LimitExceeded {
#[must_use]
pub const fn new(what: &'static str, limit: u64, value: u64) -> Self {
Self { what, limit, value }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize),
serde(tag = "kind", rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum WireError {
BufferTooShort(BufferTooShort),
OutputBufferTooSmall(OutputBufferTooSmall),
FieldOverflow(FieldOverflow),
CrcMismatch(CrcMismatch),
InvalidSync(InvalidSync),
ReservedBits(ReservedBits),
InvalidValue(InvalidValue),
InvalidField(InvalidField),
UnsupportedVersion(UnsupportedVersion),
LengthMismatch(LengthMismatch),
LimitExceeded(LimitExceeded),
}
impl WireError {
#[must_use]
pub const fn name(&self) -> &'static str {
match self {
WireError::BufferTooShort(_) => "buffer too short",
WireError::OutputBufferTooSmall(_) => "output buffer too small",
WireError::FieldOverflow(_) => "field overflow",
WireError::CrcMismatch(_) => "crc mismatch",
WireError::InvalidSync(_) => "invalid sync",
WireError::ReservedBits(_) => "reserved bits",
WireError::InvalidValue(_) => "invalid value",
WireError::InvalidField(_) => "invalid field",
WireError::UnsupportedVersion(_) => "unsupported version",
WireError::LengthMismatch(_) => "length mismatch",
WireError::LimitExceeded(_) => "limit exceeded",
}
}
}
impl core::fmt::Display for WireError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
WireError::BufferTooShort(e) => e.fmt(f),
WireError::OutputBufferTooSmall(e) => e.fmt(f),
WireError::FieldOverflow(e) => e.fmt(f),
WireError::CrcMismatch(e) => e.fmt(f),
WireError::InvalidSync(e) => e.fmt(f),
WireError::ReservedBits(e) => e.fmt(f),
WireError::InvalidValue(e) => e.fmt(f),
WireError::InvalidField(e) => e.fmt(f),
WireError::UnsupportedVersion(e) => e.fmt(f),
WireError::LengthMismatch(e) => e.fmt(f),
WireError::LimitExceeded(e) => e.fmt(f),
}
}
}
impl core::error::Error for WireError {}
impl From<BufferTooShort> for WireError {
fn from(e: BufferTooShort) -> Self {
WireError::BufferTooShort(e)
}
}
impl From<OutputBufferTooSmall> for WireError {
fn from(e: OutputBufferTooSmall) -> Self {
WireError::OutputBufferTooSmall(e)
}
}
impl From<FieldOverflow> for WireError {
fn from(e: FieldOverflow) -> Self {
WireError::FieldOverflow(e)
}
}
impl From<CrcMismatch> for WireError {
fn from(e: CrcMismatch) -> Self {
WireError::CrcMismatch(e)
}
}
impl From<InvalidSync> for WireError {
fn from(e: InvalidSync) -> Self {
WireError::InvalidSync(e)
}
}
impl From<ReservedBits> for WireError {
fn from(e: ReservedBits) -> Self {
WireError::ReservedBits(e)
}
}
impl From<InvalidValue> for WireError {
fn from(e: InvalidValue) -> Self {
WireError::InvalidValue(e)
}
}
impl From<InvalidField> for WireError {
fn from(e: InvalidField) -> Self {
WireError::InvalidField(e)
}
}
impl From<UnsupportedVersion> for WireError {
fn from(e: UnsupportedVersion) -> Self {
WireError::UnsupportedVersion(e)
}
}
impl From<LengthMismatch> for WireError {
fn from(e: LengthMismatch) -> Self {
WireError::LengthMismatch(e)
}
}
impl From<LimitExceeded> for WireError {
fn from(e: LimitExceeded) -> Self {
WireError::LimitExceeded(e)
}
}
impl core::fmt::Display for LimitExceeded {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} = {} exceeds the configured limit of {}",
self.what, self.value, self.limit
)
}
}
impl core::error::Error for LimitExceeded {}
impl core::fmt::Display for BufferTooShort {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"buffer too short: need {} bytes, have {} (while parsing {})",
self.need, self.have, self.what
)
}
}
impl core::error::Error for BufferTooShort {}
impl core::fmt::Display for OutputBufferTooSmall {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"output buffer too small: need {}, have {} (while serializing {})",
self.need, self.have, self.what
)
}
}
impl core::error::Error for OutputBufferTooSmall {}
impl core::fmt::Display for FieldOverflow {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} = {} exceeds the field maximum {}",
self.field, self.value, self.max
)
}
}
impl core::error::Error for FieldOverflow {}
impl core::fmt::Display for CrcMismatch {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} CRC-32 mismatch: computed {:#010x}, expected {:#010x}",
self.what, self.computed, self.expected
)
}
}
impl core::error::Error for CrcMismatch {}
impl core::fmt::Display for InvalidSync {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"invalid {}: expected {:#x}, got {:#x}",
self.what, self.expected, self.found
)
}
}
impl core::error::Error for InvalidSync {}
impl core::fmt::Display for ReservedBits {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"reserved field {} must be {:#x}, found {:#x}",
self.field, self.expected, self.found
)
}
}
impl core::error::Error for ReservedBits {}
impl core::fmt::Display for InvalidValue {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"field {} value {} invalid: {}",
self.field, self.value, self.reason
)
}
}
impl core::error::Error for InvalidValue {}
impl core::fmt::Display for InvalidField {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "invalid field {}: {}", self.field, self.reason)
}
}
impl core::error::Error for InvalidField {}
impl core::fmt::Display for UnsupportedVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "unsupported {} version {}", self.what, self.found)
}
}
impl core::error::Error for UnsupportedVersion {}
impl core::fmt::Display for LengthMismatch {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"length mismatch for {}: declared {}, actual {}",
self.what, self.declared, self.actual
)
}
}
impl core::error::Error for LengthMismatch {}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use alloc::string::ToString;
#[test]
fn display_messages() {
assert_eq!(
BufferTooShort {
need: 4,
have: 1,
what: "PAT"
}
.to_string(),
"buffer too short: need 4 bytes, have 1 (while parsing PAT)"
);
assert_eq!(
OutputBufferTooSmall {
need: 4,
have: 1,
what: "Frame"
}
.to_string(),
"output buffer too small: need 4, have 1 (while serializing Frame)"
);
assert_eq!(
FieldOverflow {
field: "section_length",
value: 5000,
max: 4095
}
.to_string(),
"section_length = 5000 exceeds the field maximum 4095"
);
assert_eq!(
CrcMismatch {
what: "PSM",
computed: 0xAB,
expected: 0xCD
}
.to_string(),
"PSM CRC-32 mismatch: computed 0x000000ab, expected 0x000000cd"
);
assert_eq!(
InvalidSync {
what: "TS sync byte",
expected: 0x47,
found: 0
}
.to_string(),
"invalid TS sync byte: expected 0x47, got 0x0"
);
assert_eq!(
ReservedBits {
field: "reserved",
expected: 0x7,
found: 0x2
}
.to_string(),
"reserved field reserved must be 0x7, found 0x2"
);
assert_eq!(
InvalidValue {
field: "version",
value: 9,
reason: "out of range"
}
.to_string(),
"field version value 9 invalid: out of range"
);
assert_eq!(
InvalidField {
field: "pts",
reason: "missing"
}
.to_string(),
"invalid field pts: missing"
);
assert_eq!(
UnsupportedVersion {
what: "RTP",
found: 1
}
.to_string(),
"unsupported RTP version 1"
);
assert_eq!(
LengthMismatch {
what: "APDU",
declared: 9,
actual: 7
}
.to_string(),
"length mismatch for APDU: declared 9, actual 7"
);
}
#[test]
fn equality_distinguishes_every_field() {
let a = CrcMismatch {
what: "PSM",
computed: 1,
expected: 2,
};
assert_eq!(a, a);
assert_ne!(a, CrcMismatch { what: "SNDU", ..a });
assert_ne!(a, CrcMismatch { computed: 3, ..a });
assert_ne!(a, CrcMismatch { expected: 3, ..a });
let b = LengthMismatch {
what: "x",
declared: 1,
actual: 2,
};
assert_ne!(b, LengthMismatch { declared: 2, ..b });
assert_ne!(b, LengthMismatch { actual: 1, ..b });
}
#[test]
fn len_helpers_return_the_shared_kinds() {
let e: BufferTooShort = crate::len::need_in(&[], 1, "x").unwrap_err();
assert_eq!(
e,
BufferTooShort {
need: 1,
have: 0,
what: "x"
}
);
let o: OutputBufferTooSmall =
crate::len::need_out(&[], 1, "len_helpers_return_the_shared_kinds").unwrap_err();
assert_eq!(
o,
OutputBufferTooSmall {
need: 1,
have: 0,
what: "len_helpers_return_the_shared_kinds"
}
);
let _: FieldOverflow = crate::len::fit_bits(256, 8, "f").unwrap_err();
}
#[test]
fn limit_exceeded_names_the_quantity_value_and_limit() {
assert_eq!(
LimitExceeded {
what: "moov bytes",
limit: 4,
value: 5
}
.to_string(),
"moov bytes = 5 exceeds the configured limit of 4"
);
}
#[test]
fn every_kind_converts_into_wire_error_and_keeps_its_display() {
let short = BufferTooShort {
need: 2,
have: 1,
what: "x",
};
let cases: [(WireError, &str); 11] = [
(short.into(), "buffer too short"),
(
OutputBufferTooSmall {
need: 2,
have: 1,
what: "every_kind_converts_into_wire_error_and_keeps_its_display",
}
.into(),
"output buffer too small",
),
(
FieldOverflow {
field: "f",
value: 2,
max: 1,
}
.into(),
"field overflow",
),
(
CrcMismatch {
what: "s",
computed: 1,
expected: 2,
}
.into(),
"crc mismatch",
),
(
InvalidSync {
what: "s",
expected: 1,
found: 2,
}
.into(),
"invalid sync",
),
(
ReservedBits {
field: "r",
expected: 1,
found: 0,
}
.into(),
"reserved bits",
),
(
InvalidValue {
field: "v",
value: 1,
reason: "r",
}
.into(),
"invalid value",
),
(
InvalidField {
field: "f",
reason: "r",
}
.into(),
"invalid field",
),
(
UnsupportedVersion {
what: "w",
found: 9,
}
.into(),
"unsupported version",
),
(
LengthMismatch {
what: "w",
declared: 1,
actual: 2,
}
.into(),
"length mismatch",
),
(
LimitExceeded {
what: "w",
limit: 1,
value: 2,
}
.into(),
"limit exceeded",
),
];
for (error, name) in cases {
assert_eq!(error.name(), name);
assert!(!error.to_string().is_empty());
}
assert_eq!(
WireError::from(short).to_string(),
short.to_string(),
"Display delegates to the kind"
);
}
#[test]
fn kinds_are_core_errors() {
fn is_error<E: core::error::Error + Copy + Send + Sync + 'static>() {}
is_error::<BufferTooShort>();
is_error::<CrcMismatch>();
is_error::<InvalidSync>();
is_error::<ReservedBits>();
is_error::<InvalidValue>();
is_error::<InvalidField>();
is_error::<UnsupportedVersion>();
is_error::<LengthMismatch>();
is_error::<LimitExceeded>();
is_error::<WireError>();
}
}