Skip to content

Commit

Permalink
remove derive_more (#60)
Browse files Browse the repository at this point in the history
* Initial go at things

* Finish off Frame

* Finish off removing all of derive_more

* Fix clippy complaint

* Actually fix CI!
  • Loading branch information
xd009642 authored May 13, 2024
1 parent 88f7a41 commit 5265c12
Show file tree
Hide file tree
Showing 14 changed files with 1,015 additions and 244 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ categories = ["compression"]
[dependencies]
byteorder = { version = "1.5", default-features = false }
twox-hash = { version = "1.6", default-features = false, optional = true }
derive_more = { version = "0.99", default-features = false, features = ["display", "from"] }

[dev-dependencies]
criterion = "0.5"
Expand All @@ -24,7 +23,7 @@ rand = { version = "0.8.5", features = ["small_rng"] }
[features]
default = ["hash", "std"]
hash = ["dep:twox-hash"]
std = ["derive_more/error"]
std = []

[[bench]]
name = "reversedbitreader_bench"
Expand Down
46 changes: 38 additions & 8 deletions src/blocks/literals_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,51 @@ pub enum LiteralsSectionType {
Treeless,
}

#[derive(Debug, derive_more::Display, derive_more::From)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[derive(Debug)]
#[non_exhaustive]
pub enum LiteralsSectionParseError {
#[display(fmt = "Illegal literalssectiontype. Is: {got}, must be in: 0, 1, 2, 3")]
IllegalLiteralSectionType { got: u8 },
#[display(fmt = "{_0:?}")]
#[from]
GetBitsError(GetBitsError),
#[display(
fmt = "Not enough byte to parse the literals section header. Have: {have}, Need: {need}"
)]
NotEnoughBytes { have: usize, need: u8 },
}

#[cfg(feature = "std")]
impl std::error::Error for LiteralsSectionParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
LiteralsSectionParseError::GetBitsError(source) => Some(source),
_ => None,
}
}
}
impl core::fmt::Display for LiteralsSectionParseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
LiteralsSectionParseError::IllegalLiteralSectionType { got } => {
write!(
f,
"Illegal literalssectiontype. Is: {}, must be in: 0, 1, 2, 3",
got
)
}
LiteralsSectionParseError::GetBitsError(e) => write!(f, "{:?}", e),
LiteralsSectionParseError::NotEnoughBytes { have, need } => {
write!(
f,
"Not enough byte to parse the literals section header. Have: {}, Need: {}",
have, need,
)
}
}
}
}

impl From<GetBitsError> for LiteralsSectionParseError {
fn from(val: GetBitsError) -> Self {
Self::GetBitsError(val)
}
}

impl core::fmt::Display for LiteralsSectionType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Expand Down
23 changes: 18 additions & 5 deletions src/blocks/sequence_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,29 @@ impl Default for SequencesHeader {
}
}

#[derive(Debug, derive_more::Display)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[derive(Debug)]
#[non_exhaustive]
pub enum SequencesHeaderParseError {
#[display(
fmt = "source must have at least {need_at_least} bytes to parse header; got {got} bytes"
)]
NotEnoughBytes { need_at_least: u8, got: usize },
}

#[cfg(feature = "std")]
impl std::error::Error for SequencesHeaderParseError {}

impl core::fmt::Display for SequencesHeaderParseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
SequencesHeaderParseError::NotEnoughBytes { need_at_least, got } => {
write!(
f,
"source must have at least {} bytes to parse header; got {} bytes",
need_at_least, got,
)
}
}
}
}

impl SequencesHeader {
pub fn new() -> SequencesHeader {
SequencesHeader {
Expand Down
42 changes: 35 additions & 7 deletions src/decoding/bit_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,47 @@ pub struct BitReader<'s> {
source: &'s [u8],
}

#[derive(Debug, derive_more::Display)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[derive(Debug)]
#[non_exhaustive]
pub enum GetBitsError {
#[display(
fmt = "Cant serve this request. The reader is limited to {limit} bits, requested {num_requested_bits} bits"
)]
TooManyBits {
num_requested_bits: usize,
limit: u8,
},
#[display(fmt = "Can't read {requested} bits, only have {remaining} bits left")]
NotEnoughRemainingBits { requested: usize, remaining: usize },
NotEnoughRemainingBits {
requested: usize,
remaining: usize,
},
}

#[cfg(feature = "std")]
impl std::error::Error for GetBitsError {}

impl core::fmt::Display for GetBitsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
GetBitsError::TooManyBits {
num_requested_bits,
limit,
} => {
write!(
f,
"Cant serve this request. The reader is limited to {} bits, requested {} bits",
limit, num_requested_bits,
)
}
GetBitsError::NotEnoughRemainingBits {
requested,
remaining,
} => {
write!(
f,
"Can\'t read {} bits, only have {} bits left",
requested, remaining,
)
}
}
}
}

impl<'s> BitReader<'s> {
Expand Down
Loading

0 comments on commit 5265c12

Please sign in to comment.