Skip to content

Commit 357edd1

Browse files
committed
revise: better error messages
1 parent 81d02cf commit 357edd1

File tree

11 files changed

+93
-198
lines changed

11 files changed

+93
-198
lines changed

omics-coordinate/src/position/base.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ mod tests {
139139
#[test]
140140
fn from_number() {
141141
let error: Result<Position<Base>> = 0u32.try_into();
142-
assert_eq!(error.unwrap_err(), Error::IncompatibleValue {
143-
system: Base::NAME,
144-
value: 0,
145-
});
142+
assert_eq!(
143+
error.unwrap_err(),
144+
Error::IncompatibleValue {
145+
system: Base::NAME,
146+
value: 0,
147+
}
148+
);
146149

147150
let position: Position<Base> = 1u32.try_into().unwrap();
148151
assert_eq!(position.get(), 1);

omics-molecule/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ rust-version.workspace = true
1212
[dependencies]
1313
omics-core = { path = "../omics-core", version = "0.1.0" }
1414

15+
thiserror.workspace = true
16+
1517
[lints]
1618
workspace = true

omics-molecule/src/compound/nucleotide/relation.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,26 @@ pub mod substitution;
2020
use omics_core::MISSING_NUCLEOTIDE;
2121
use omics_core::VARIANT_SEPARATOR;
2222
pub use substitution::Substitution;
23+
use thiserror::Error;
2324

2425
use crate::compound::Nucleotide;
2526

2627
/// An error related to a [`Relation`].
27-
#[derive(Debug)]
28+
#[derive(Error, Debug)]
2829
pub enum Error<N: Nucleotide> {
2930
/// Attempted to create a relation with no nucleotides.
31+
#[error("cannot create a relation with no nucleotides")]
3032
Empty,
3133

3234
/// A parse error.
35+
#[error("parse error: {0}")]
3336
ParseError(String),
3437

3538
/// A substitution error.
36-
Substitution(substitution::Error<N>),
39+
#[error(transparent)]
40+
Substitution(#[from] substitution::Error<N>),
3741
}
3842

39-
impl<N: Nucleotide> std::fmt::Display for Error<N> {
40-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41-
match self {
42-
Error::Empty => write!(f, "attempted to create a relation with no nucleotides"),
43-
Error::ParseError(v) => write!(f, "parse error: {v}"),
44-
Error::Substitution(err) => write!(f, "subsitution error: {err}"),
45-
}
46-
}
47-
}
48-
49-
impl<N: Nucleotide> std::error::Error for Error<N> {}
50-
5143
/// A [`Result`](std::result::Result) with an [`Error`].
5244
type Result<T, N> = std::result::Result<T, Error<N>>;
5345

@@ -431,7 +423,7 @@ mod tests {
431423
.parse::<Relation<dna::Nucleotide>>()
432424
.unwrap_err()
433425
.to_string(),
434-
"attempted to create a relation with no nucleotides"
426+
"cannot create a relation with no nucleotides"
435427
);
436428

437429
Ok(())

omics-molecule/src/compound/nucleotide/relation/substitution.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,17 @@ use crate::compound::Nucleotide;
55
mod kind;
66

77
pub use kind::Kind;
8+
use thiserror::Error;
89

910
/// An error related to a [`Substitution`].
10-
#[derive(Debug)]
11+
#[derive(Error, Debug)]
1112
pub enum Error<N: Nucleotide> {
1213
/// Attempted to create a [`Substitution`] with identical reference and
1314
/// alternate nucleotides.
15+
#[error("identical nucleotides in substitution: {0}")]
1416
Identical(N),
1517
}
1618

17-
impl<N: Nucleotide> std::fmt::Display for Error<N> {
18-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19-
match self {
20-
Error::Identical(nucleotide) => {
21-
write!(f, "identical nucleotides for substitution: {nucleotide}")
22-
}
23-
}
24-
}
25-
}
26-
27-
impl<N: Nucleotide> std::error::Error for Error<N> {}
28-
2919
/// A [`Result`](std::result::Result) with an [`Error<N>`].
3020
type Result<T, N> = std::result::Result<T, Error<N>>;
3121

omics-molecule/src/polymer/dna.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,16 @@
33
mod nucleotide;
44

55
pub use nucleotide::Nucleotide;
6+
use thiserror::Error;
67

78
/// An error related to a [`Molecule`].
8-
#[derive(Debug)]
9+
#[derive(Error, Debug)]
910
pub enum Error {
1011
/// An error when processing a [`Nucleotide`].
11-
NucleotideError(nucleotide::Error),
12+
#[error(transparent)]
13+
NucleotideError(#[from] nucleotide::Error),
1214
}
1315

14-
impl std::fmt::Display for Error {
15-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16-
match self {
17-
Error::NucleotideError(err) => write!(f, "nucleotide error: {err}"),
18-
}
19-
}
20-
}
21-
22-
impl std::error::Error for Error {}
23-
2416
/// A molecule representing Deoxyribonucleic Acid, otherwise known as DNA.
2517
#[derive(Debug)]
2618
pub struct Molecule(Vec<Nucleotide>);
@@ -53,12 +45,10 @@ impl Molecule {
5345
/// let m = "ACGT".parse::<Molecule>()?;
5446
/// let nucleotides = m.into_inner();
5547
///
56-
/// assert_eq!(nucleotides, vec![
57-
/// Nucleotide::A,
58-
/// Nucleotide::C,
59-
/// Nucleotide::G,
60-
/// Nucleotide::T,
61-
/// ]);
48+
/// assert_eq!(
49+
/// nucleotides,
50+
/// vec![Nucleotide::A, Nucleotide::C, Nucleotide::G, Nucleotide::T,]
51+
/// );
6252
///
6353
/// # Ok::<(), Box<dyn std::error::Error>>(())
6454
/// ```
@@ -126,6 +116,6 @@ mod tests {
126116
#[test]
127117
fn it_fails_to_parse_a_molecule_from_an_invalid_string() {
128118
let err = "QQQQ".parse::<Molecule>().unwrap_err();
129-
assert_eq!(err.to_string(), "nucleotide error: invalid nucleotide: Q");
119+
assert_eq!(err.to_string(), "invalid nucleotide `Q`");
130120
}
131121
}

omics-molecule/src/polymer/dna/nucleotide.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Nucleotides in DNA.
22
3+
use thiserror::Error;
4+
35
use crate::compound::Kind;
46
use crate::compound::nucleotide::Analogous;
57
use crate::compound::nucleotide::Transcribe;
@@ -17,47 +19,29 @@ use crate::polymer::rna;
1719
// the signatures look identical (they will be converted to string differently).
1820

1921
/// An error when parsing a nucleotide.
20-
#[derive(Debug)]
22+
#[derive(Error, Debug)]
2123
pub enum ParseError {
2224
/// An invalid format was attempted to be parsed.
25+
#[error("invalid nucleotide format `{0}`")]
2326
InvalidFormat(String),
2427

2528
/// An invalid nucleotide was attempted to be parsed.
29+
#[error("invalid nucleotide `{0}`")]
2630
InvalidNucleotide(char),
2731
}
2832

29-
impl std::fmt::Display for ParseError {
30-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31-
match self {
32-
ParseError::InvalidFormat(value) => write!(f, "invalid format: {value}"),
33-
ParseError::InvalidNucleotide(c) => write!(f, "invalid nucleotide: {c}"),
34-
}
35-
}
36-
}
37-
38-
impl std::error::Error for ParseError {}
39-
4033
/// An error related to a [`Nucleotide`].
41-
#[derive(Debug)]
34+
#[derive(Error, Debug)]
4235
pub enum Error {
4336
/// An invalid nucleotide was attempted to be created from a [`char`].
37+
#[error("invalid nucleotide `{0}`")]
4438
InvalidNucleotide(char),
4539

4640
/// A parse error.
47-
ParseError(ParseError),
41+
#[error(transparent)]
42+
ParseError(#[from] ParseError),
4843
}
4944

50-
impl std::fmt::Display for Error {
51-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52-
match self {
53-
Error::InvalidNucleotide(c) => write!(f, "invalid nucleotide: {c}"),
54-
Error::ParseError(err) => write!(f, "parse error: {err}"),
55-
}
56-
}
57-
}
58-
59-
impl std::error::Error for Error {}
60-
6145
/// A nucleotide in an DNA context.
6246
#[derive(Clone, Debug, Eq, PartialEq)]
6347
pub enum Nucleotide {
@@ -193,13 +177,13 @@ mod tests {
193177
assert_eq!(Nucleotide::try_from('T')?, Nucleotide::T);
194178

195179
let err = Nucleotide::try_from('u').unwrap_err();
196-
assert_eq!(err.to_string(), "invalid nucleotide: u");
180+
assert_eq!(err.to_string(), "invalid nucleotide `u`");
197181

198182
let err = Nucleotide::try_from('U').unwrap_err();
199-
assert_eq!(err.to_string(), "invalid nucleotide: U");
183+
assert_eq!(err.to_string(), "invalid nucleotide `U`");
200184

201185
let err = Nucleotide::try_from('q').unwrap_err();
202-
assert_eq!(err.to_string(), "invalid nucleotide: q");
186+
assert_eq!(err.to_string(), "invalid nucleotide `q`");
203187

204188
Ok(())
205189
}
@@ -220,28 +204,28 @@ mod tests {
220204
err,
221205
Error::ParseError(ParseError::InvalidFormat(_))
222206
));
223-
assert_eq!(err.to_string(), "parse error: invalid format: word");
207+
assert_eq!(err.to_string(), "invalid nucleotide format `word`");
224208

225209
let err = "q".parse::<Nucleotide>().unwrap_err();
226210
assert!(matches!(
227211
err,
228212
Error::ParseError(ParseError::InvalidNucleotide(_))
229213
));
230-
assert_eq!(err.to_string(), "parse error: invalid nucleotide: q");
214+
assert_eq!(err.to_string(), "invalid nucleotide `q`");
231215

232216
let err = "u".parse::<Nucleotide>().unwrap_err();
233217
assert!(matches!(
234218
err,
235219
Error::ParseError(ParseError::InvalidNucleotide(_))
236220
));
237-
assert_eq!(err.to_string(), "parse error: invalid nucleotide: u");
221+
assert_eq!(err.to_string(), "invalid nucleotide `u`");
238222

239223
let err = "U".parse::<Nucleotide>().unwrap_err();
240224
assert!(matches!(
241225
err,
242226
Error::ParseError(ParseError::InvalidNucleotide(_))
243227
));
244-
assert_eq!(err.to_string(), "parse error: invalid nucleotide: U");
228+
assert_eq!(err.to_string(), "invalid nucleotide `U`");
245229

246230
Ok(())
247231
}

omics-molecule/src/polymer/rna.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,16 @@
33
mod nucleotide;
44

55
pub use nucleotide::Nucleotide;
6+
use thiserror::Error;
67

78
/// An error related to a [`Molecule`].
8-
#[derive(Debug)]
9+
#[derive(Error, Debug)]
910
pub enum Error {
1011
/// An error when processing a [`Nucleotide`].
11-
NucleotideError(nucleotide::Error),
12+
#[error(transparent)]
13+
NucleotideError(#[from] nucleotide::Error),
1214
}
1315

14-
impl std::fmt::Display for Error {
15-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16-
match self {
17-
Error::NucleotideError(err) => write!(f, "nucleotide error: {err}"),
18-
}
19-
}
20-
}
21-
22-
impl std::error::Error for Error {}
23-
2416
/// A molecule representing Ribonucleic Acid, otherwise known as RNA.
2517
#[derive(Debug)]
2618
pub struct Molecule(Vec<Nucleotide>);
@@ -53,12 +45,10 @@ impl Molecule {
5345
/// let m = "ACGU".parse::<Molecule>()?;
5446
/// let nucleotides = m.into_inner();
5547
///
56-
/// assert_eq!(nucleotides, vec![
57-
/// Nucleotide::A,
58-
/// Nucleotide::C,
59-
/// Nucleotide::G,
60-
/// Nucleotide::U,
61-
/// ]);
48+
/// assert_eq!(
49+
/// nucleotides,
50+
/// vec![Nucleotide::A, Nucleotide::C, Nucleotide::G, Nucleotide::U,]
51+
/// );
6252
///
6353
/// # Ok::<(), Box<dyn std::error::Error>>(())
6454
/// ```
@@ -126,6 +116,6 @@ mod tests {
126116
#[test]
127117
fn it_fails_to_parse_a_molecule_from_an_invalid_string() {
128118
let err = "QQQQ".parse::<Molecule>().unwrap_err();
129-
assert_eq!(err.to_string(), "nucleotide error: invalid nucleotide: Q");
119+
assert_eq!(err.to_string(), "invalid nucleotide `Q`");
130120
}
131121
}

0 commit comments

Comments
 (0)