rio_xml/
error.rs

1use oxilangtag::LanguageTagParseError;
2use oxiri::IriParseError;
3use rio_api::parser::{LineBytePosition, ParseError};
4use std::error::Error;
5use std::sync::Arc;
6use std::{fmt, io};
7
8/// Error that might be returned during parsing.
9///
10/// It might wrap an IO error or be a parsing error.
11#[derive(Debug)]
12pub struct RdfXmlError {
13    pub(crate) kind: RdfXmlErrorKind,
14}
15
16#[derive(Debug)]
17pub(crate) enum RdfXmlErrorKind {
18    Xml(quick_xml::Error),
19    XmlAttribute(quick_xml::events::attributes::AttrError),
20    InvalidIri {
21        iri: String,
22        error: IriParseError,
23    },
24    InvalidLanguageTag {
25        tag: String,
26        error: LanguageTagParseError,
27    },
28    Other(String),
29}
30
31impl RdfXmlError {
32    pub(crate) fn msg(msg: impl Into<String>) -> Self {
33        Self {
34            kind: RdfXmlErrorKind::Other(msg.into()),
35        }
36    }
37}
38
39impl fmt::Display for RdfXmlError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match &self.kind {
42            RdfXmlErrorKind::Xml(error) => error.fmt(f),
43            RdfXmlErrorKind::XmlAttribute(error) => error.fmt(f),
44            RdfXmlErrorKind::InvalidIri { iri, error } => {
45                write!(f, "error while parsing IRI '{}': {}", iri, error)
46            }
47            RdfXmlErrorKind::InvalidLanguageTag { tag, error } => {
48                write!(f, "error while parsing language tag '{}': {}", tag, error)
49            }
50            RdfXmlErrorKind::Other(message) => write!(f, "{}", message),
51        }
52    }
53}
54
55impl Error for RdfXmlError {
56    fn source(&self) -> Option<&(dyn Error + 'static)> {
57        match &self.kind {
58            RdfXmlErrorKind::Xml(error) => Some(error),
59            RdfXmlErrorKind::XmlAttribute(error) => Some(error),
60            RdfXmlErrorKind::InvalidIri { error, .. } => Some(error),
61            RdfXmlErrorKind::InvalidLanguageTag { error, .. } => Some(error),
62            RdfXmlErrorKind::Other(_) => None,
63        }
64    }
65}
66
67impl ParseError for RdfXmlError {
68    fn textual_position(&self) -> Option<LineBytePosition> {
69        None
70    }
71}
72
73impl From<quick_xml::Error> for RdfXmlError {
74    fn from(error: quick_xml::Error) -> Self {
75        Self {
76            kind: RdfXmlErrorKind::Xml(error),
77        }
78    }
79}
80
81impl From<quick_xml::events::attributes::AttrError> for RdfXmlError {
82    fn from(error: quick_xml::events::attributes::AttrError) -> Self {
83        Self {
84            kind: RdfXmlErrorKind::XmlAttribute(error),
85        }
86    }
87}
88
89impl From<io::Error> for RdfXmlError {
90    fn from(error: io::Error) -> Self {
91        Self {
92            kind: RdfXmlErrorKind::Xml(quick_xml::Error::Io(Arc::new(error))),
93        }
94    }
95}
96
97impl From<RdfXmlError> for io::Error {
98    fn from(error: RdfXmlError) -> Self {
99        match error.kind {
100            RdfXmlErrorKind::Xml(error) => match error {
101                quick_xml::Error::Io(error) => io::Error::new(error.kind(), error),
102                error => io::Error::new(io::ErrorKind::InvalidData, error),
103            },
104            RdfXmlErrorKind::Other(error) => io::Error::new(io::ErrorKind::InvalidData, error),
105            _ => io::Error::new(io::ErrorKind::InvalidData, error),
106        }
107    }
108}