Skip to content

Commit

Permalink
Revert "remove derive_more (KillingSpark#60)"
Browse files Browse the repository at this point in the history
This reverts commit 5265c12.
  • Loading branch information
JelteF committed Jun 15, 2024
1 parent 101df3e commit 8f807fc
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 1,014 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 @@ -23,7 +24,7 @@ rand = { version = "0.8.5", features = ["small_rng"] }
[features]
default = ["hash", "std"]
hash = ["dep:twox-hash"]
std = []
std = ["derive_more/error"]

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

#[derive(Debug)]
#[derive(Debug, derive_more::Display, derive_more::From)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[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: 5 additions & 18 deletions src/blocks/sequence_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,16 @@ impl Default for SequencesHeader {
}
}

#[derive(Debug)]
#[derive(Debug, derive_more::Display)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[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 {
/// Create a new [SequencesHeader].
pub fn new() -> SequencesHeader {
Expand Down
42 changes: 7 additions & 35 deletions src/decoding/bit_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,19 @@ pub struct BitReader<'s> {
source: &'s [u8],
}

#[derive(Debug)]
#[derive(Debug, derive_more::Display)]
#[cfg_attr(feature = "std", derive(derive_more::Error))]
#[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,
},
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,
)
}
}
}
#[display(fmt = "Can't read {requested} bits, only have {remaining} bits left")]
NotEnoughRemainingBits { requested: usize, remaining: usize },
}

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

0 comments on commit 8f807fc

Please sign in to comment.