Skip to main content

ng_oxigraph/oxrdfio/
error.rs

1use crate::oxrdfxml;
2use crate::oxttl;
3use std::io;
4use std::ops::Range;
5
6/// Error returned during RDF format parsing.
7#[derive(Debug, thiserror::Error)]
8pub enum RdfParseError {
9    /// I/O error during parsing (file not found...).
10    #[error(transparent)]
11    Io(#[from] io::Error),
12    /// An error in the file syntax.
13    #[error(transparent)]
14    Syntax(#[from] RdfSyntaxError),
15}
16
17impl RdfParseError {
18    pub(crate) fn msg(msg: &'static str) -> Self {
19        Self::Syntax(RdfSyntaxError(SyntaxErrorKind::Msg(msg)))
20    }
21}
22
23impl From<oxttl::TurtleSyntaxError> for RdfSyntaxError {
24    #[inline]
25    fn from(error: oxttl::TurtleSyntaxError) -> Self {
26        Self(SyntaxErrorKind::Turtle(error))
27    }
28}
29
30impl From<oxttl::TurtleParseError> for RdfParseError {
31    #[inline]
32    fn from(error: oxttl::TurtleParseError) -> Self {
33        match error {
34            oxttl::TurtleParseError::Syntax(e) => Self::Syntax(e.into()),
35            oxttl::TurtleParseError::Io(e) => Self::Io(e),
36        }
37    }
38}
39
40impl From<oxrdfxml::RdfXmlSyntaxError> for RdfSyntaxError {
41    #[inline]
42    fn from(error: oxrdfxml::RdfXmlSyntaxError) -> Self {
43        Self(SyntaxErrorKind::RdfXml(error))
44    }
45}
46
47impl From<oxrdfxml::RdfXmlParseError> for RdfParseError {
48    #[inline]
49    fn from(error: oxrdfxml::RdfXmlParseError) -> Self {
50        match error {
51            oxrdfxml::RdfXmlParseError::Syntax(e) => Self::Syntax(e.into()),
52            oxrdfxml::RdfXmlParseError::Io(e) => Self::Io(e),
53        }
54    }
55}
56
57impl From<RdfParseError> for io::Error {
58    #[inline]
59    fn from(error: RdfParseError) -> Self {
60        match error {
61            RdfParseError::Io(error) => error,
62            RdfParseError::Syntax(error) => error.into(),
63        }
64    }
65}
66
67/// An error in the syntax of the parsed file.
68#[derive(Debug, thiserror::Error)]
69#[error(transparent)]
70pub struct RdfSyntaxError(#[from] SyntaxErrorKind);
71
72/// An error in the syntax of the parsed file.
73#[derive(Debug, thiserror::Error)]
74enum SyntaxErrorKind {
75    #[error(transparent)]
76    Turtle(#[from] oxttl::TurtleSyntaxError),
77    #[error(transparent)]
78    RdfXml(#[from] oxrdfxml::RdfXmlSyntaxError),
79    #[error("{0}")]
80    Msg(&'static str),
81}
82
83impl RdfSyntaxError {
84    /// The location of the error inside of the file.
85    #[inline]
86    pub fn location(&self) -> Option<Range<TextPosition>> {
87        match &self.0 {
88            SyntaxErrorKind::Turtle(e) => {
89                let location = e.location();
90                Some(
91                    TextPosition {
92                        line: location.start.line,
93                        column: location.start.column,
94                        offset: location.start.offset,
95                    }..TextPosition {
96                        line: location.end.line,
97                        column: location.end.column,
98                        offset: location.end.offset,
99                    },
100                )
101            }
102            SyntaxErrorKind::RdfXml(_) | SyntaxErrorKind::Msg(_) => None,
103        }
104    }
105}
106
107impl From<RdfSyntaxError> for io::Error {
108    #[inline]
109    fn from(error: RdfSyntaxError) -> Self {
110        match error.0 {
111            SyntaxErrorKind::Turtle(error) => error.into(),
112            SyntaxErrorKind::RdfXml(error) => error.into(),
113            SyntaxErrorKind::Msg(msg) => Self::new(io::ErrorKind::InvalidData, msg),
114        }
115    }
116}
117
118/// A position in a text i.e. a `line` number starting from 0, a `column` number starting from 0 (in number of code points) and a global file `offset` starting from 0 (in number of bytes).
119#[derive(Eq, PartialEq, Debug, Clone, Copy)]
120pub struct TextPosition {
121    pub line: u64,
122    pub column: u64,
123    pub offset: u64,
124}