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

Use thiserror for errors #10

Merged
merged 1 commit into from
Mar 5, 2024
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
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.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repository = "https://github.com/ystreet/cea708-types"
rust-version = "1.63.0"

[dependencies]
once_cell = "1"
muldiv = "1"
log = "0.4"
env_logger = "0.10"
log = "0.4"
muldiv = "1"
once_cell = "1"
thiserror = "1"
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,32 @@ extern crate log;
pub mod tables;

/// Various possible errors when parsing data
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ParserError {
/// Length of data does not match length advertised
#[error("The length of the data ({actual}) does not match the advertised expected ({expected}) length")]
LengthMismatch {
/// The expected size
expected: usize,
/// The actual size
actual: usize,
},
/// CEA-608 comaptibility bytes encountered after CEA-708
#[error("CEA-608 compatibility bytes were found after CEA-708 bytes at position {byte_pos}")]
Cea608AfterCea708 {
/// Position of the offending bytes
byte_pos: usize,
},
}

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)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum WriterError {
/// Writing would overflow by how many bytes
#[error("Writing the data would overflow by {0} bytes")]
WouldOverflow(usize),
/// It is not possible to write to this resource
#[error("The resource is not writable")]
ReadOnly,
}

Expand Down
3 changes: 2 additions & 1 deletion src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

//! Module for the various [Code] tables available

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum CodeError {
/// Length of data does not match length advertised
#[error("The length of the data ({actual}) does not match the advertised expected ({expected}) length")]
LengthMismatch {
/// The expected size
expected: usize,
Expand Down
Loading