Skip to main content

clayers_xml/
error.rs

1/// Errors from XML processing operations.
2#[derive(Debug, thiserror::Error)]
3pub enum Error {
4    #[error("XML parse error: {0}")]
5    XmlParse(String),
6
7    #[error("XML serialization error: {0}")]
8    XmlSerialize(String),
9
10    #[error("C14N error: {0}")]
11    Canonicalization(String),
12
13    #[error("invalid hash format: {0}")]
14    InvalidHashFormat(String),
15
16    #[error("XPath query error: {0}")]
17    Query(String),
18
19    #[error("XSLT error: {0}")]
20    Xslt(String),
21
22    #[error("I/O error: {0}")]
23    Io(#[from] std::io::Error),
24}
25
26impl From<xot::Error> for Error {
27    fn from(e: xot::Error) -> Self {
28        Self::XmlParse(e.to_string())
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn error_display_xml_parse() {
38        let e = Error::XmlParse("unexpected EOF".into());
39        assert!(e.to_string().contains("XML parse error"));
40        assert!(e.to_string().contains("unexpected EOF"));
41    }
42
43    #[test]
44    fn error_display_c14n() {
45        let e = Error::Canonicalization("bad input".into());
46        assert!(e.to_string().contains("C14N error"));
47    }
48
49    #[test]
50    fn error_display_hash_format() {
51        let e = Error::InvalidHashFormat("not hex".into());
52        assert!(e.to_string().contains("invalid hash format"));
53    }
54}