Skip to main content

ionic/mzml/utilities/
parse_error.rs

1use std::fmt::{Display, Formatter, Result};
2
3#[derive(Debug)]
4pub enum ParseError {
5    Xml(quick_xml::Error),
6    Base64(base64::DecodeError),
7    Decompress(String),
8    UnexpectedEof { context: String, byte_offset: u64 },
9}
10
11impl Display for ParseError {
12    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
13        match self {
14            Self::Xml(e) => write!(f, "XML error: {e}"),
15            Self::Base64(e) => write!(f, "base64 decode error: {e}"),
16            Self::Decompress(s) => write!(f, "decompression error: {s}"),
17            Self::UnexpectedEof {
18                context,
19                byte_offset,
20            } => write!(
21                f,
22                "unexpected end of file inside <{context}> (byte {byte_offset})"
23            ),
24        }
25    }
26}
27
28impl From<quick_xml::Error> for ParseError {
29    fn from(e: quick_xml::Error) -> Self {
30        Self::Xml(e)
31    }
32}
33impl From<base64::DecodeError> for ParseError {
34    fn from(e: base64::DecodeError) -> Self {
35        Self::Base64(e)
36    }
37}
38
39impl std::error::Error for ParseError {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        match self {
42            Self::Xml(e) => Some(e),
43            Self::Base64(e) => Some(e),
44            Self::Decompress(_) | Self::UnexpectedEof { .. } => None,
45        }
46    }
47}