Skip to content

Commit c8b48da

Browse files
authored
Merge pull request #7 from sile/remove-thiserror
Replace `thiserror` crate with manual error trait implementations
2 parents a63257e + 81517bc commit c8b48da

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ license = "MIT"
1414
rand = "0.8"
1515
rand_distr = "0.4"
1616
statrs = "0.15"
17-
thiserror = "1"
1817

1918
[dev-dependencies]
2019
anyhow = "1"

‎src/lib.rs‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,36 @@ struct Trial {
296296
}
297297

298298
/// Possible errors during [`TpeOptimizerBuilder::build`].
299-
#[derive(Debug, Clone, thiserror::Error)]
299+
#[derive(Debug, Clone)]
300300
pub enum BuildError {
301-
#[error("the value of `gamma` must be in the range from 0.0 to 1.0")]
302301
/// The value of `gamma` must be in the range from `0.0` to `1.0`.
303302
GammaOutOfRange,
304303

305-
#[error("the value of `candidates` must be a positive integer")]
306304
/// The value of `candidates` must be a positive integer.
307305
ZeroCandidates,
308306
}
309307

308+
impl std::fmt::Display for BuildError {
309+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
310+
match self {
311+
BuildError::GammaOutOfRange => {
312+
write!(
313+
f,
314+
"the value of `gamma` must be in the range from 0.0 to 1.0"
315+
)
316+
}
317+
BuildError::ZeroCandidates => {
318+
write!(f, "the value of `candidates` must be a positive integer")
319+
}
320+
}
321+
}
322+
}
323+
324+
impl std::error::Error for BuildError {}
325+
310326
/// Possible errors during [`TpeOptimizer::tell`].
311-
#[derive(Debug, Clone, thiserror::Error)]
327+
#[derive(Debug, Clone)]
312328
pub enum TellError {
313-
#[error("the parameter value {param} is out of the range {range}")]
314329
/// The parameter value is out of the range.
315330
ParamOutOfRange {
316331
/// Actual parameter value.
@@ -319,11 +334,29 @@ pub enum TellError {
319334
range: Range,
320335
},
321336

322-
#[error("NaN value is not allowed")]
323337
/// NaN value is not allowed.
324338
NanValue,
325339
}
326340

341+
impl std::fmt::Display for TellError {
342+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
343+
match self {
344+
TellError::ParamOutOfRange { param, range } => {
345+
write!(
346+
f,
347+
"the parameter value {} is out of the range {}",
348+
param, range
349+
)
350+
}
351+
TellError::NanValue => {
352+
write!(f, "NaN value is not allowed")
353+
}
354+
}
355+
}
356+
}
357+
358+
impl std::error::Error for TellError {}
359+
327360
#[cfg(test)]
328361
mod tests {
329362
use super::*;

‎src/range.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,22 @@ impl Range {
4949
}
5050

5151
/// Possible errors during [`Range`] construction.
52-
#[derive(Debug, Clone, thiserror::Error)]
52+
#[derive(Debug, Clone)]
5353
pub enum RangeError {
54-
#[error("not a finite range")]
5554
/// Not a finite range.
5655
NonFiniteRange,
5756

58-
#[error("an empty range")]
5957
/// An empty range.
6058
EmptyRange,
6159
}
60+
61+
impl std::fmt::Display for RangeError {
62+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63+
match self {
64+
RangeError::NonFiniteRange => write!(f, "not a finite range"),
65+
RangeError::EmptyRange => write!(f, "an empty range"),
66+
}
67+
}
68+
}
69+
70+
impl std::error::Error for RangeError {}

0 commit comments

Comments
 (0)