Skip to content

Commit e620d2a

Browse files
authored
Merge pull request #50 from KillingSpark/remove_thiserror
Remove thiserror in favor of derive_more
2 parents e15eb1e + 9e9d204 commit e620d2a

18 files changed

+306
-199
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ruzstd"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
authors = ["Moritz Borcherding <[email protected]>"]
55
edition = "2018"
66
license = "MIT"
@@ -11,17 +11,17 @@ exclude = ["decodecorpus_files/*", "dict_tests/*", "fuzz_decodecorpus/*"]
1111
readme = "Readme.md"
1212

1313
[dependencies]
14-
byteorder = { version = "1.4", default-features = false }
14+
byteorder = { version = "1.5", default-features = false }
1515
twox-hash = { version = "1.6", default-features = false }
16-
thiserror = { package = "thiserror-core", version = "1.0.38", default-features = false }
16+
derive_more = { version = "0.99", default-features = false, features = ["display", "from"] }
1717

1818
[dev-dependencies]
1919
criterion = "0.3"
2020
rand = {version = "0.8.5", features = ["small_rng"]}
2121

2222
[features]
2323
default = ["std"]
24-
std = ["thiserror/std"]
24+
std = ["derive_more/error"]
2525

2626
[[bench]]
2727
name = "reversedbitreader_bench"

src/blocks/literals_section.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ pub enum LiteralsSectionType {
1414
Treeless,
1515
}
1616

17-
#[derive(Debug, thiserror::Error)]
17+
#[derive(Debug, derive_more::Display, derive_more::From)]
18+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
1819
#[non_exhaustive]
1920
pub enum LiteralsSectionParseError {
20-
#[error("Illegal literalssectiontype. Is: {got}, must be in: 0, 1, 2, 3")]
21+
#[display(fmt = "Illegal literalssectiontype. Is: {got}, must be in: 0, 1, 2, 3")]
2122
IllegalLiteralSectionType { got: u8 },
22-
#[error(transparent)]
23-
GetBitsError(#[from] GetBitsError),
24-
#[error("Not enough byte to parse the literals section header. Have: {have}, Need: {need}")]
23+
#[display(fmt = "{_0:?}")]
24+
#[from]
25+
GetBitsError(GetBitsError),
26+
#[display(
27+
fmt = "Not enough byte to parse the literals section header. Have: {have}, Need: {need}"
28+
)]
2529
NotEnoughBytes { have: usize, need: u8 },
2630
}
2731

src/blocks/sequence_section.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ impl Default for SequencesHeader {
5555
}
5656
}
5757

58-
#[derive(Debug, thiserror::Error)]
58+
#[derive(Debug, derive_more::Display)]
59+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
5960
#[non_exhaustive]
6061
pub enum SequencesHeaderParseError {
61-
#[error("source must have at least {need_at_least} bytes to parse header; got {got} bytes")]
62+
#[display(
63+
fmt = "source must have at least {need_at_least} bytes to parse header; got {got} bytes"
64+
)]
6265
NotEnoughBytes { need_at_least: u8, got: usize },
6366
}
6467

src/decoding/bit_reader.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ pub struct BitReader<'s> {
33
source: &'s [u8],
44
}
55

6-
#[derive(Debug, thiserror::Error)]
6+
#[derive(Debug, derive_more::Display)]
7+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
78
#[non_exhaustive]
89
pub enum GetBitsError {
9-
#[error("Cant serve this request. The reader is limited to {limit} bits, requested {num_requested_bits} bits")]
10+
#[display(
11+
fmt = "Cant serve this request. The reader is limited to {limit} bits, requested {num_requested_bits} bits"
12+
)]
1013
TooManyBits {
1114
num_requested_bits: usize,
1215
limit: u8,
1316
},
14-
#[error("Can't read {requested} bits, only have {remaining} bits left")]
17+
#[display(fmt = "Can't read {requested} bits, only have {remaining} bits left")]
1518
NotEnoughRemainingBits { requested: usize, remaining: usize },
1619
}
1720

src/decoding/block_decoder.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,72 +25,89 @@ enum DecoderState {
2525
Failed, //TODO put "self.internal_state = DecoderState::Failed;" everywhere an unresolvable error occurs
2626
}
2727

28-
#[derive(Debug, thiserror::Error)]
28+
#[derive(Debug, derive_more::Display, derive_more::From)]
29+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
2930
#[non_exhaustive]
3031
pub enum BlockHeaderReadError {
31-
#[error("Error while reading the block header")]
32-
ReadError(#[from] io::Error),
33-
#[error("Reserved block occured. This is considered corruption by the documentation")]
32+
#[display(fmt = "Error while reading the block header")]
33+
#[from]
34+
ReadError(io::Error),
35+
#[display(fmt = "Reserved block occured. This is considered corruption by the documentation")]
3436
FoundReservedBlock,
35-
#[error("Error getting block type: {0}")]
36-
BlockTypeError(#[from] BlockTypeError),
37-
#[error("Error getting block content size: {0}")]
38-
BlockSizeError(#[from] BlockSizeError),
37+
#[display(fmt = "Error getting block type: {_0}")]
38+
#[from]
39+
BlockTypeError(BlockTypeError),
40+
#[display(fmt = "Error getting block content size: {_0}")]
41+
#[from]
42+
BlockSizeError(BlockSizeError),
3943
}
4044

41-
#[derive(Debug, thiserror::Error)]
45+
#[derive(Debug, derive_more::Display)]
46+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
4247
#[non_exhaustive]
4348
pub enum BlockTypeError {
44-
#[error(
45-
"Invalid Blocktype number. Is: {num} Should be one of: 0, 1, 2, 3 (3 is reserved though"
49+
#[display(
50+
fmt = "Invalid Blocktype number. Is: {num} Should be one of: 0, 1, 2, 3 (3 is reserved though"
4651
)]
4752
InvalidBlocktypeNumber { num: u8 },
4853
}
4954

50-
#[derive(Debug, thiserror::Error)]
55+
#[derive(Debug, derive_more::Display)]
56+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
5157
#[non_exhaustive]
5258
pub enum BlockSizeError {
53-
#[error("Blocksize was bigger than the absolute maximum {ABSOLUTE_MAXIMUM_BLOCK_SIZE} (128kb). Is: {size}")]
59+
#[display(
60+
fmt = "Blocksize was bigger than the absolute maximum {ABSOLUTE_MAXIMUM_BLOCK_SIZE} (128kb). Is: {size}"
61+
)]
5462
BlockSizeTooLarge { size: u32 },
5563
}
5664

57-
#[derive(Debug, thiserror::Error)]
65+
#[derive(Debug, derive_more::Display, derive_more::From)]
66+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
5867
#[non_exhaustive]
5968
pub enum DecompressBlockError {
60-
#[error("Error while reading the block content: {0}")]
61-
BlockContentReadError(#[from] io::Error),
62-
#[error("Malformed section header. Says literals would be this long: {expected_len} but there are only {remaining_bytes} bytes left")]
69+
#[display(fmt = "Error while reading the block content: {_0}")]
70+
#[from]
71+
BlockContentReadError(io::Error),
72+
#[display(
73+
fmt = "Malformed section header. Says literals would be this long: {expected_len} but there are only {remaining_bytes} bytes left"
74+
)]
6375
MalformedSectionHeader {
6476
expected_len: usize,
6577
remaining_bytes: usize,
6678
},
67-
#[error(transparent)]
68-
DecompressLiteralsError(#[from] DecompressLiteralsError),
69-
#[error(transparent)]
70-
LiteralsSectionParseError(#[from] LiteralsSectionParseError),
71-
#[error(transparent)]
72-
SequencesHeaderParseError(#[from] SequencesHeaderParseError),
73-
#[error(transparent)]
74-
DecodeSequenceError(#[from] DecodeSequenceError),
75-
#[error(transparent)]
76-
ExecuteSequencesError(#[from] ExecuteSequencesError),
79+
#[display(fmt = "{_0:?}")]
80+
#[from]
81+
DecompressLiteralsError(DecompressLiteralsError),
82+
#[display(fmt = "{_0:?}")]
83+
#[from]
84+
LiteralsSectionParseError(LiteralsSectionParseError),
85+
#[display(fmt = "{_0:?}")]
86+
#[from]
87+
SequencesHeaderParseError(SequencesHeaderParseError),
88+
#[display(fmt = "{_0:?}")]
89+
#[from]
90+
DecodeSequenceError(DecodeSequenceError),
91+
#[display(fmt = "{_0:?}")]
92+
#[from]
93+
ExecuteSequencesError(ExecuteSequencesError),
7794
}
7895

79-
#[derive(Debug, thiserror::Error)]
96+
#[derive(Debug, derive_more::Display, derive_more::From)]
97+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
8098
#[non_exhaustive]
8199
pub enum DecodeBlockContentError {
82-
#[error("Can't decode next block if failed along the way. Results will be nonsense")]
100+
#[display(fmt = "Can't decode next block if failed along the way. Results will be nonsense")]
83101
DecoderStateIsFailed,
84-
#[error("Cant decode next block body, while expecting to decode the header of the previous block. Results will be nonsense")]
102+
#[display(
103+
fmt = "Cant decode next block body, while expecting to decode the header of the previous block. Results will be nonsense"
104+
)]
85105
ExpectedHeaderOfPreviousBlock,
86-
#[error("Error while reading bytes for {step}: {source}")]
87-
ReadError {
88-
step: BlockType,
89-
#[source]
90-
source: io::Error,
91-
},
92-
#[error(transparent)]
93-
DecompressBlockError(#[from] DecompressBlockError),
106+
#[display(fmt = "Error while reading bytes for {step}: {source}")]
107+
ReadError { step: BlockType, source: io::Error },
108+
#[display(fmt = "{_0:?}")]
109+
#[from]
110+
DecompressBlockError(DecompressBlockError),
94111
}
95112

96113
pub fn new() -> BlockDecoder {

src/decoding/decodebuffer.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ pub struct Decodebuffer {
1515
pub hash: XxHash64,
1616
}
1717

18-
#[derive(Debug, thiserror::Error)]
18+
#[derive(Debug, derive_more::Display)]
19+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
1920
#[non_exhaustive]
2021
pub enum DecodebufferError {
21-
#[error("Need {need} bytes from the dictionary but it is only {got} bytes long")]
22+
#[display(fmt = "Need {need} bytes from the dictionary but it is only {got} bytes long")]
2223
NotEnoughBytesInDictionary { got: usize, need: usize },
23-
#[error("offset: {offset} bigger than buffer: {buf_len}")]
24+
#[display(fmt = "offset: {offset} bigger than buffer: {buf_len}")]
2425
OffsetTooBig { offset: usize, buf_len: usize },
2526
}
2627

src/decoding/dictionary.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ pub struct Dictionary {
1414
pub offset_hist: [u32; 3],
1515
}
1616

17-
#[derive(Debug, thiserror::Error)]
17+
#[derive(Debug, derive_more::Display, derive_more::From)]
18+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
1819
#[non_exhaustive]
1920
pub enum DictionaryDecodeError {
20-
#[error(
21-
"Bad magic_num at start of the dictionary; Got: {got:#04X?}, Expected: {MAGIC_NUM:#04x?}"
21+
#[display(
22+
fmt = "Bad magic_num at start of the dictionary; Got: {got:#04X?}, Expected: {MAGIC_NUM:#04x?}"
2223
)]
2324
BadMagicNum { got: [u8; 4] },
24-
#[error(transparent)]
25-
FSETableError(#[from] FSETableError),
26-
#[error(transparent)]
27-
HuffmanTableError(#[from] HuffmanTableError),
25+
#[display(fmt = "{_0:?}")]
26+
#[from]
27+
FSETableError(FSETableError),
28+
#[display(fmt = "{_0:?}")]
29+
#[from]
30+
HuffmanTableError(HuffmanTableError),
2831
}
2932

3033
pub const MAGIC_NUM: [u8; 4] = [0x37, 0xA4, 0x30, 0xEC];

src/decoding/literals_section_decoder.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,40 @@ use super::scratch::HuffmanScratch;
44
use crate::huff0::{HuffmanDecoder, HuffmanDecoderError, HuffmanTableError};
55
use alloc::vec::Vec;
66

7-
#[derive(Debug, thiserror::Error)]
7+
#[derive(Debug, derive_more::Display, derive_more::From)]
8+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
89
#[non_exhaustive]
910
pub enum DecompressLiteralsError {
10-
#[error(
11-
"compressed size was none even though it must be set to something for compressed literals"
11+
#[display(
12+
fmt = "compressed size was none even though it must be set to something for compressed literals"
1213
)]
1314
MissingCompressedSize,
14-
#[error("num_streams was none even though it must be set to something (1 or 4) for compressed literals")]
15+
#[display(
16+
fmt = "num_streams was none even though it must be set to something (1 or 4) for compressed literals"
17+
)]
1518
MissingNumStreams,
16-
#[error(transparent)]
17-
GetBitsError(#[from] GetBitsError),
18-
#[error(transparent)]
19-
HuffmanTableError(#[from] HuffmanTableError),
20-
#[error(transparent)]
21-
HuffmanDecoderError(#[from] HuffmanDecoderError),
22-
#[error("Tried to reuse huffman table but it was never initialized")]
19+
#[display(fmt = "{_0:?}")]
20+
#[from]
21+
GetBitsError(GetBitsError),
22+
#[display(fmt = "{_0:?}")]
23+
#[from]
24+
HuffmanTableError(HuffmanTableError),
25+
#[display(fmt = "{_0:?}")]
26+
#[from]
27+
HuffmanDecoderError(HuffmanDecoderError),
28+
#[display(fmt = "Tried to reuse huffman table but it was never initialized")]
2329
UninitializedHuffmanTable,
24-
#[error("Need 6 bytes to decode jump header, got {got} bytes")]
30+
#[display(fmt = "Need 6 bytes to decode jump header, got {got} bytes")]
2531
MissingBytesForJumpHeader { got: usize },
26-
#[error("Need at least {needed} bytes to decode literals. Have: {got} bytes")]
32+
#[display(fmt = "Need at least {needed} bytes to decode literals. Have: {got} bytes")]
2733
MissingBytesForLiterals { got: usize, needed: usize },
28-
#[error("Padding at the end of the sequence_section was more than a byte long: {skipped_bits} bits. Probably caused by data corruption")]
34+
#[display(
35+
fmt = "Padding at the end of the sequence_section was more than a byte long: {skipped_bits} bits. Probably caused by data corruption"
36+
)]
2937
ExtraPadding { skipped_bits: i32 },
30-
#[error("Bitstream was read till: {read_til}, should have been: {expected}")]
38+
#[display(fmt = "Bitstream was read till: {read_til}, should have been: {expected}")]
3139
BitstreamReadMismatch { read_til: isize, expected: isize },
32-
#[error("Did not decode enough literals: {decoded}, Should have been: {expected}")]
40+
#[display(fmt = "Did not decode enough literals: {decoded}, Should have been: {expected}")]
3341
DecodedLiteralCountMismatch { decoded: usize, expected: usize },
3442
}
3543

src/decoding/sequence_execution.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use super::{decodebuffer::DecodebufferError, scratch::DecoderScratch};
22

3-
#[derive(Debug, thiserror::Error)]
3+
#[derive(Debug, derive_more::Display, derive_more::From)]
4+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
45
#[non_exhaustive]
56
pub enum ExecuteSequencesError {
6-
#[error(transparent)]
7-
DecodebufferError(#[from] DecodebufferError),
8-
#[error("Sequence wants to copy up to byte {wanted}. Bytes in literalsbuffer: {have}")]
7+
#[display(fmt = "{_0:?}")]
8+
#[from]
9+
DecodebufferError(DecodebufferError),
10+
#[display(fmt = "Sequence wants to copy up to byte {wanted}. Bytes in literalsbuffer: {have}")]
911
NotEnoughBytesForSequence { wanted: usize, have: usize },
10-
#[error("Illegal offset: 0 found")]
12+
#[display(fmt = "Illegal offset: 0 found")]
1113
ZeroOffset,
1214
}
1315

src/decoding/sequence_section_decoder.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@ use super::scratch::FSEScratch;
66
use crate::fse::{FSEDecoder, FSEDecoderError, FSETableError};
77
use alloc::vec::Vec;
88

9-
#[derive(Debug, thiserror::Error)]
9+
#[derive(Debug, derive_more::Display, derive_more::From)]
10+
#[cfg_attr(feature = "std", derive(derive_more::Error))]
1011
#[non_exhaustive]
1112
pub enum DecodeSequenceError {
12-
#[error(transparent)]
13-
GetBitsError(#[from] GetBitsError),
14-
#[error(transparent)]
15-
FSEDecoderError(#[from] FSEDecoderError),
16-
#[error(transparent)]
17-
FSETableError(#[from] FSETableError),
18-
#[error("Padding at the end of the sequence_section was more than a byte long: {skipped_bits} bits. Probably caused by data corruption")]
13+
#[display(fmt = "{_0:?}")]
14+
#[from]
15+
GetBitsError(GetBitsError),
16+
#[display(fmt = "{_0:?}")]
17+
#[from]
18+
FSEDecoderError(FSEDecoderError),
19+
#[display(fmt = "{_0:?}")]
20+
#[from]
21+
FSETableError(FSETableError),
22+
#[display(
23+
fmt = "Padding at the end of the sequence_section was more than a byte long: {skipped_bits} bits. Probably caused by data corruption"
24+
)]
1925
ExtraPadding { skipped_bits: i32 },
20-
#[error("Do not support offsets bigger than 1<<32; got: {offset_code}")]
26+
#[display(fmt = "Do not support offsets bigger than 1<<32; got: {offset_code}")]
2127
UnsupportedOffset { offset_code: u8 },
22-
#[error("Read an offset == 0. That is an illegal value for offsets")]
28+
#[display(fmt = "Read an offset == 0. That is an illegal value for offsets")]
2329
ZeroOffset,
24-
#[error("Bytestream did not contain enough bytes to decode num_sequences")]
30+
#[display(fmt = "Bytestream did not contain enough bytes to decode num_sequences")]
2531
NotEnoughBytesForNumSequences,
26-
#[error("Did not use full bitstream. Bits left: {bits_remaining} ({} bytes)", bits_remaining / 8)]
32+
#[display(fmt = "Did not use full bitstream. Bits left: {bits_remaining} ({} bytes)", bits_remaining / 8)]
2733
ExtraBits { bits_remaining: isize },
28-
#[error("compression modes are none but they must be set to something")]
34+
#[display(fmt = "compression modes are none but they must be set to something")]
2935
MissingCompressionMode,
30-
#[error("Need a byte to read for RLE ll table")]
36+
#[display(fmt = "Need a byte to read for RLE ll table")]
3137
MissingByteForRleLlTable,
32-
#[error("Need a byte to read for RLE of table")]
38+
#[display(fmt = "Need a byte to read for RLE of table")]
3339
MissingByteForRleOfTable,
34-
#[error("Need a byte to read for RLE ml table")]
40+
#[display(fmt = "Need a byte to read for RLE ml table")]
3541
MissingByteForRleMlTable,
3642
}
3743

0 commit comments

Comments
 (0)