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