Skip to content

Commit 922a4e6

Browse files
committed
refactor: replace thiserror dependency with manual implementation of std::error::Error
1 parent 228094a commit 922a4e6

2 files changed

Lines changed: 86 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ edition = "2021"
1515
num = "0.4"
1616
byteorder = "1"
1717
libflate = "2"
18-
thiserror = "1"

src/codec.rs

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,118 @@ use std::io::Write;
1111
use std::str;
1212

1313
/// Errors which can occur when decoding a term
14-
#[derive(Debug, thiserror::Error)]
14+
#[derive(Debug)]
1515
pub enum DecodeError {
16-
#[error("I/O error")]
17-
Io(#[from] io::Error),
16+
/// I/O error.
17+
Io(io::Error),
1818

19-
#[error("the format version {version} is unsupported")]
19+
/// Unsupported format version.
2020
UnsupportedVersion { version: u8 },
2121

22-
#[error("unknown tag {tag}")]
22+
/// Unknown tag.
2323
UnknownTag { tag: u8 },
2424

25-
#[error("{value} is not a {expected}")]
25+
/// Unexpected type.
2626
UnexpectedType { value: Term, expected: String },
2727

28-
#[error("{value} is out of range {range:?}")]
28+
/// Out of range.
2929
OutOfRange {
3030
value: i32,
3131
range: std::ops::Range<i32>,
3232
},
3333

34-
#[error("tried to convert non-finite float")]
34+
/// Non-finite float.
3535
NonFiniteFloat,
3636
}
3737

38+
impl std::fmt::Display for DecodeError {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
match self {
41+
Self::Io(error) => write!(f, "I/O error: {error}"),
42+
Self::UnsupportedVersion { version } => {
43+
write!(f, "the format version {version} is unsupported")
44+
}
45+
Self::UnknownTag { tag } => write!(f, "unknown tag {tag}"),
46+
Self::UnexpectedType { value, expected } => write!(f, "{value} is not a {expected}"),
47+
Self::OutOfRange { value, range } => write!(f, "{value} is out of range {range:?}"),
48+
Self::NonFiniteFloat => write!(f, "tried to convert non-finite float"),
49+
}
50+
}
51+
}
52+
53+
impl std::error::Error for DecodeError {
54+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55+
if let Self::Io(error) = self {
56+
Some(error)
57+
} else {
58+
None
59+
}
60+
}
61+
}
62+
63+
impl From<std::io::Error> for DecodeError {
64+
fn from(value: std::io::Error) -> Self {
65+
Self::Io(value)
66+
}
67+
}
68+
3869
/// Errors which can occur when encoding a term
39-
#[derive(Debug, thiserror::Error)]
70+
#[derive(Debug)]
4071
pub enum EncodeError {
41-
#[error("I/O error")]
42-
Io(#[from] io::Error),
72+
/// I/O error.
73+
Io(io::Error),
4374

44-
#[error("too long atom name: {} bytes", .0.name.len())]
75+
/// Too long atom name.
4576
TooLongAtomName(Atom),
4677

47-
#[error("too large integer value: {} bytes required to encode", .0.value.to_bytes_le().1.len())]
78+
/// Too large integer value.
4879
TooLargeInteger(BigInteger),
4980

50-
#[error("too large reference ID: {} bytes required to encode", .0.id.len() * 4)]
81+
/// Too large reference ID.
5182
TooLargeReferenceId(Reference),
5283
}
5384

85+
impl std::fmt::Display for EncodeError {
86+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87+
match self {
88+
Self::Io(error) => write!(f, "I/O error: {error}"),
89+
Self::TooLongAtomName(atom) => {
90+
write!(f, "too long atom name: {} bytes", atom.name.len())
91+
}
92+
Self::TooLargeInteger(integer) => {
93+
write!(
94+
f,
95+
"too large integer value: {} bytes required to encode",
96+
integer.value.to_bytes_le().1.len()
97+
)
98+
}
99+
Self::TooLargeReferenceId(reference) => {
100+
write!(
101+
f,
102+
"too large reference ID: {} bytes required to encode",
103+
reference.id.len() * 4
104+
)
105+
}
106+
}
107+
}
108+
}
109+
110+
impl std::error::Error for EncodeError {
111+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
112+
if let Self::Io(error) = self {
113+
Some(error)
114+
} else {
115+
None
116+
}
117+
}
118+
}
119+
120+
impl From<std::io::Error> for EncodeError {
121+
fn from(value: std::io::Error) -> Self {
122+
Self::Io(value)
123+
}
124+
}
125+
54126
pub type DecodeResult = Result<Term, DecodeError>;
55127
pub type EncodeResult = Result<(), EncodeError>;
56128

0 commit comments

Comments
 (0)