Skip to content

Commit 129090d

Browse files
authored
refactor: manually implement Error (#796)
* drops thiserror as a dependency
1 parent e20a859 commit 129090d

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

Cargo.lock

Lines changed: 0 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ lto = "fat"
2020
codegen-units=1
2121

2222
[dependencies]
23-
thiserror = "1.0"
2423
libc = "0.2.155"
2524
streaming-iterator = "0.1.5"
2625
serde = {version = "1.0.203", features = ["derive"], optional = true}

src/sys/mod.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use thiserror::Error;
2-
31
mod macros;
42

53
#[allow(dead_code)]
@@ -47,52 +45,76 @@ pub use treeseq::TreeSequence;
4745

4846
use traits::TskTeardown;
4947

50-
#[derive(Error, Debug)]
5148
#[non_exhaustive]
49+
#[derive(Debug)]
5250
pub enum TskitError {
5351
/// Returned when conversion attempts fail
54-
#[error("range error: {}", *.0)]
5552
RangeError(String),
5653
/// Used when bad input is encountered.
57-
#[error("we received {} but expected {}",*got, *expected)]
5854
ValueError { got: String, expected: String },
5955
/// Used when array access is out of range.
6056
/// Typically, this is used when accessing
6157
/// arrays allocated on the C side.
62-
#[error("Invalid index")]
6358
IndexError,
6459
/// Raised when samples are requested from
6560
/// [`crate::Tree`] objects, but sample lists are
6661
/// not being updated.
67-
#[error("Not tracking samples in Trees")]
6862
NotTrackingSamples,
6963
/// Wrapper around tskit C API error codes.
70-
#[error("{}", get_tskit_error_message(*code))]
7164
ErrorCode { code: i32 },
7265
/// A redirection of [``crate::metadata::MetadataError``]
73-
#[error("{value:?}")]
7466
MetadataError {
7567
/// The redirected error
76-
#[from]
7768
value: MetadataError,
7869
},
7970
/// General error variant
80-
#[error("{}", *.0)]
8171
LibraryError(String),
8272
}
8373

84-
#[derive(Error, Debug)]
74+
impl std::fmt::Display for TskitError {
75+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76+
match self {
77+
Self::RangeError(msg) => write!(f, "range error: {}", msg),
78+
Self::ValueError { got, expected } => {
79+
write!(f, "we received {} but expected {}", got, expected)
80+
}
81+
Self::IndexError => write!(f, "Invalid index"),
82+
Self::NotTrackingSamples => write!(f, "Not tracking samples in Trees"),
83+
Self::ErrorCode { code } => write!(f, "{}", get_tskit_error_message(*code)),
84+
Self::MetadataError { value } => write!(f, "meta data error: {}", value),
85+
Self::LibraryError(msg) => write!(f, "library error: {msg}"),
86+
}
87+
}
88+
}
89+
90+
impl From<MetadataError> for TskitError {
91+
fn from(value: MetadataError) -> Self {
92+
Self::MetadataError { value }
93+
}
94+
}
95+
96+
impl std::error::Error for TskitError {}
97+
98+
#[derive(Debug)]
8599
#[non_exhaustive]
86100
pub enum MetadataError {
87101
/// Error related to types implementing
88102
/// metadata serialization.
89-
#[error("{}", *value)]
90103
RoundtripError {
91-
#[from]
92104
value: Box<dyn std::error::Error + Send + Sync>,
93105
},
94106
}
95107

108+
impl std::fmt::Display for MetadataError {
109+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110+
match self {
111+
Self::RoundtripError { value } => write!(f, "metadata round trip error: {value:?}"),
112+
}
113+
}
114+
}
115+
116+
impl std::error::Error for MetadataError {}
117+
96118
//#[non_exhaustive]
97119
//#[derive(Error, Debug)]
98120
//pub enum Error {

0 commit comments

Comments
 (0)