1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use oxilangtag::LanguageTagParseError;
use oxiri::IriParseError;
use rio_api::parser::{LineBytePosition, ParseError};
use std::error::Error;
use std::{fmt, io};

/// Error that might be returned during parsing.
///
/// It might wrap an IO error or be a parsing error.
#[derive(Debug)]
pub struct RdfXmlError {
    pub(crate) kind: RdfXmlErrorKind,
}

#[derive(Debug)]
pub(crate) enum RdfXmlErrorKind {
    Xml(quick_xml::Error),
    InvalidIri {
        iri: String,
        error: IriParseError,
    },
    InvalidLanguageTag {
        tag: String,
        error: LanguageTagParseError,
    },
    Other(String),
}

impl RdfXmlError {
    pub(crate) fn msg(msg: impl Into<String>) -> Self {
        Self {
            kind: RdfXmlErrorKind::Other(msg.into()),
        }
    }
}

impl fmt::Display for RdfXmlError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            RdfXmlErrorKind::Xml(error) => error.fmt(f),
            RdfXmlErrorKind::InvalidIri { iri, error } => {
                write!(f, "error while parsing IRI '{}': {}", iri, error)
            }
            RdfXmlErrorKind::InvalidLanguageTag { tag, error } => {
                write!(f, "error while parsing language tag '{}': {}", tag, error)
            }
            RdfXmlErrorKind::Other(message) => write!(f, "{}", message),
        }
    }
}

impl Error for RdfXmlError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            RdfXmlErrorKind::Xml(error) => Some(error),
            RdfXmlErrorKind::InvalidIri { error, .. } => Some(error),
            RdfXmlErrorKind::InvalidLanguageTag { error, .. } => Some(error),
            _ => None,
        }
    }
}

impl ParseError for RdfXmlError {
    fn textual_position(&self) -> Option<LineBytePosition> {
        None
    }
}

impl From<quick_xml::Error> for RdfXmlError {
    fn from(error: quick_xml::Error) -> Self {
        Self {
            kind: RdfXmlErrorKind::Xml(error),
        }
    }
}

impl From<io::Error> for RdfXmlError {
    fn from(error: io::Error) -> Self {
        Self {
            kind: RdfXmlErrorKind::Xml(quick_xml::Error::Io(error)),
        }
    }
}

impl From<RdfXmlError> for io::Error {
    fn from(error: RdfXmlError) -> Self {
        match error.kind {
            RdfXmlErrorKind::Xml(error) => match error {
                quick_xml::Error::Io(error) => error,
                quick_xml::Error::UnexpectedEof(error) => {
                    io::Error::new(io::ErrorKind::UnexpectedEof, error)
                }
                error => io::Error::new(io::ErrorKind::InvalidData, error),
            },
            RdfXmlErrorKind::Other(error) => io::Error::new(io::ErrorKind::InvalidData, error),
            _ => io::Error::new(io::ErrorKind::InvalidData, error),
        }
    }
}