1use std::{
2 error::Error,
3 fmt::{Display, Formatter},
4};
5
6#[non_exhaustive]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum IonError {
9 Msg(String),
10 BadDtype { dtype: u8, kind: &'static str },
11 UnsupportedPacking(u8),
12 UnsupportedFormatVersion(u16),
13 UnsupportedCodec(u8),
14 MissingSpectrumBounds,
15 BadSpectrumBoundsChecksum,
16 MalformedSpectrumBounds(String),
17 MissingChromatogramBounds,
18 BadChromatogramBoundsChecksum,
19 MalformedChromatogramBounds(String),
20 OutOfRange { index: usize, count: u64 },
21}
22
23pub type IonResult<T> = Result<T, IonError>;
24
25impl IonError {
26 pub fn contains(&self, text: &str) -> bool {
27 self.to_string().contains(text)
28 }
29}
30
31impl Display for IonError {
32 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33 match self {
34 Self::Msg(text) => f.write_str(text),
35 Self::BadDtype { dtype, kind } => {
36 write!(f, "unsupported dtype {dtype} for {kind}")
37 }
38 Self::UnsupportedPacking(b) => write!(f, "unsupported packing id: {b}"),
39 Self::UnsupportedFormatVersion(v) => write!(f, "unsupported format version: {v}"),
40 Self::UnsupportedCodec(c) => write!(f, "unsupported compression codec: {c}"),
41 Self::MissingSpectrumBounds => f.write_str(
42 "spectrum has no window bounds (A0); re-encode the file with the current Ionic",
43 ),
44 Self::BadSpectrumBoundsChecksum => f.write_str("spectrum bounds (A0) checksum failed"),
45 Self::MalformedSpectrumBounds(reason) => {
46 write!(f, "spectrum bounds (A0) malformed: {reason}")
47 }
48 Self::MissingChromatogramBounds => f.write_str(
49 "chromatogram has no window bounds (A0); re-encode the file with the current Ionic",
50 ),
51 Self::BadChromatogramBoundsChecksum => {
52 f.write_str("chromatogram bounds (A0) checksum failed")
53 }
54 Self::MalformedChromatogramBounds(reason) => {
55 write!(f, "chromatogram bounds (A0) malformed: {reason}")
56 }
57 Self::OutOfRange { index, count } => {
58 write!(f, "index {index} out of range (count is {count})")
59 }
60 }
61 }
62}
63
64impl Error for IonError {}
65
66impl From<String> for IonError {
67 fn from(text: String) -> Self {
68 Self::Msg(text)
69 }
70}
71
72impl From<&str> for IonError {
73 fn from(text: &str) -> Self {
74 Self::Msg(text.to_owned())
75 }
76}
77
78impl From<std::io::Error> for IonError {
79 fn from(err: std::io::Error) -> Self {
80 Self::Msg(err.to_string())
81 }
82}
83
84impl From<crate::mzml::ParseError> for IonError {
85 fn from(source: crate::mzml::ParseError) -> Self {
86 Self::Msg(source.to_string())
87 }
88}
89
90impl From<crate::mzml::BinToMzmlError> for IonError {
91 fn from(source: crate::mzml::BinToMzmlError) -> Self {
92 Self::Msg(source.to_string())
93 }
94}