Skip to content

Commit

Permalink
sl-std: derive Error for bitreader::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwuelker committed Aug 10, 2024
1 parent d403fc2 commit 39979e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/sl-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ criterion = { workspace = true }

[dependencies]
serialize = { workspace = true, optional = true }
error-derive = { workspace = true }

[[bench]]
name = "date_from_days_since_unix"
Expand Down
19 changes: 7 additions & 12 deletions crates/sl-std/src/bitreader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use error_derive::Error;

/// Wraps a byte buffer to allow reading individual bits
#[derive(Debug)]
Expand All @@ -9,10 +9,15 @@ pub struct BitReader<'a> {
}

// this enum might grow once we add streaming (ie the reader wraps a Read instance)
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Error)]
pub enum Error {
#[msg = "unexpected end of file"]
UnexpectedEOF,

#[msg = "attempting to read too many bits at once"]
TooLargeRead,

#[msg = "attempting to read bytes from an unaligned position"]
UnalignedRead,
}

Expand Down Expand Up @@ -116,16 +121,6 @@ impl<'a> BitReader<'a> {
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLargeRead => "attempting to read too many bits at once".fmt(f),
Self::UnalignedRead => "attempting to read bytes from an unaligned position".fmt(f),
Self::UnexpectedEOF => "unexpected end of file".fmt(f),
}
}
}

#[cfg(test)]
mod tests {
use super::BitReader;
Expand Down

0 comments on commit 39979e7

Please sign in to comment.