Skip to content

Commit

Permalink
Use thiserror for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ystreet committed Mar 5, 2024
1 parent 29103c7 commit 52234a3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ repository = "https://github.com/ystreet/cdp-types"
rust-version = "1.65.0"

[dependencies]
log = "0.4"
cea708-types = "0.3"
log = "0.4"
thiserror = "1"

[dev-dependencies]
once_cell = "1"
Expand Down
24 changes: 9 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@
extern crate log;

/// Various possible errors when parsing data
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ParserError {
/// The length of the data does not match the length in the data
#[error("The length of the data ({actual}) does not match the advertised expected ({expected}) length")]
LengthMismatch {
/// Expected minimum size of the data
expected: usize,
/// The actual length of the data
actual: usize,
},
/// Some magic byte/s do not have the correct value
#[error("Some magic byte/s do not have the correct value")]
WrongMagic,
/// Unrecognied framerate value
#[error("The framerate specified is not known by this implementation")]
UnknownFramerate,
/// Some 'fixed' bits did not have the correct value
#[error("Some fixed bits did not have the correct value")]
InvalidFixedBits,
/// CEA-608 bytes were found after CEA-708 bytes
#[error("CEA-608 compatibility bytes were found after CEA-708 bytes")]
Cea608AfterCea708,
/// Failed to validate the checksum
#[error("The computed checksum value does not match the stored checksum value")]
ChecksumFailed,
/// Sequence count differs between the header and the footer. Usually indicates this packet was
/// spliced together incorrectly.
#[error("The sequence count differs between the header and the footer")]
SequenceCountMismatch,
}

Expand All @@ -51,20 +58,7 @@ impl From<cea708_types::ParserError> for ParserError {
}
}

impl std::fmt::Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.pad(&format!("{self:?}"))
}
}

/// An error enum returned when writing data fails
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriterError {
/// Writing would overflow by how many bytes
WouldOverflow(usize),
/// It is not possible to write to this resource
ReadOnly,
}
pub use cea708_types::WriterError;

static FRAMERATES: [Framerate; 8] = [
Framerate {
Expand Down

0 comments on commit 52234a3

Please sign in to comment.