Skip to main content

ng_oxigraph/sparesults/
error.rs

1use crate::oxrdf::TermParseError;
2use std::io;
3use std::ops::Range;
4use std::sync::Arc;
5
6/// Error returned during SPARQL result formats format parsing.
7#[derive(Debug, thiserror::Error)]
8pub enum QueryResultsParseError {
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] QueryResultsSyntaxError),
15}
16
17impl From<QueryResultsParseError> for io::Error {
18    #[inline]
19    fn from(error: QueryResultsParseError) -> Self {
20        match error {
21            QueryResultsParseError::Io(error) => error,
22            QueryResultsParseError::Syntax(error) => error.into(),
23        }
24    }
25}
26
27impl From<json_event_parser::ParseError> for QueryResultsParseError {
28    fn from(error: json_event_parser::ParseError) -> Self {
29        match error {
30            json_event_parser::ParseError::Syntax(error) => {
31                QueryResultsSyntaxError::from(error).into()
32            }
33            json_event_parser::ParseError::Io(error) => error.into(),
34        }
35    }
36}
37
38impl From<quick_xml::Error> for QueryResultsParseError {
39    #[inline]
40    fn from(error: quick_xml::Error) -> Self {
41        match error {
42            quick_xml::Error::Io(error) => {
43                Self::Io(Arc::try_unwrap(error).unwrap_or_else(|e| io::Error::new(e.kind(), e)))
44            }
45            _ => Self::Syntax(QueryResultsSyntaxError(SyntaxErrorKind::Xml(error))),
46        }
47    }
48}
49
50impl From<quick_xml::escape::EscapeError> for QueryResultsParseError {
51    #[inline]
52    fn from(error: quick_xml::escape::EscapeError) -> Self {
53        quick_xml::Error::from(error).into()
54    }
55}
56/// An error in the syntax of the parsed file.
57#[derive(Debug, thiserror::Error)]
58#[error(transparent)]
59pub struct QueryResultsSyntaxError(#[from] pub(crate) SyntaxErrorKind);
60
61#[derive(Debug, thiserror::Error)]
62pub(crate) enum SyntaxErrorKind {
63    #[error(transparent)]
64    Json(#[from] json_event_parser::SyntaxError),
65    #[error(transparent)]
66    Xml(#[from] quick_xml::Error),
67    #[error("Error {error} on '{term}' in line {}", location.start.line + 1)]
68    Term {
69        #[source]
70        error: TermParseError,
71        term: String,
72        location: Range<TextPosition>,
73    },
74    #[error("{msg}")]
75    Msg {
76        msg: String,
77        location: Option<Range<TextPosition>>,
78    },
79}
80
81impl QueryResultsSyntaxError {
82    /// Builds an error from a printable error message.
83    #[inline]
84    pub(crate) fn msg(msg: impl Into<String>) -> Self {
85        Self(SyntaxErrorKind::Msg {
86            msg: msg.into(),
87            location: None,
88        })
89    }
90
91    /// Builds an error from a printable error message and a location
92    #[inline]
93    pub(crate) fn located_message(msg: impl Into<String>, location: Range<TextPosition>) -> Self {
94        Self(SyntaxErrorKind::Msg {
95            msg: msg.into(),
96            location: Some(location),
97        })
98    }
99
100    /// The location of the error inside of the file.
101    #[inline]
102    pub fn location(&self) -> Option<Range<TextPosition>> {
103        match &self.0 {
104            SyntaxErrorKind::Json(e) => {
105                let location = e.location();
106                Some(
107                    TextPosition {
108                        line: location.start.line,
109                        column: location.start.column,
110                        offset: location.start.offset,
111                    }..TextPosition {
112                        line: location.end.line,
113                        column: location.end.column,
114                        offset: location.end.offset,
115                    },
116                )
117            }
118            SyntaxErrorKind::Term { location, .. } => Some(location.clone()),
119            SyntaxErrorKind::Msg { location, .. } => location.clone(),
120            SyntaxErrorKind::Xml(_) => None,
121        }
122    }
123}
124
125impl From<QueryResultsSyntaxError> for io::Error {
126    #[inline]
127    fn from(error: QueryResultsSyntaxError) -> Self {
128        match error.0 {
129            SyntaxErrorKind::Json(error) => Self::new(io::ErrorKind::InvalidData, error),
130            SyntaxErrorKind::Xml(error) => match error {
131                quick_xml::Error::Io(error) => {
132                    Arc::try_unwrap(error).unwrap_or_else(|e| Self::new(e.kind(), e))
133                }
134                quick_xml::Error::UnexpectedEof(error) => {
135                    Self::new(io::ErrorKind::UnexpectedEof, error)
136                }
137                _ => Self::new(io::ErrorKind::InvalidData, error),
138            },
139            SyntaxErrorKind::Term { .. } => Self::new(io::ErrorKind::InvalidData, error),
140            SyntaxErrorKind::Msg { msg, .. } => Self::new(io::ErrorKind::InvalidData, msg),
141        }
142    }
143}
144
145impl From<json_event_parser::SyntaxError> for QueryResultsSyntaxError {
146    fn from(error: json_event_parser::SyntaxError) -> Self {
147        Self(SyntaxErrorKind::Json(error))
148    }
149}
150
151/// 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).
152#[derive(Eq, PartialEq, Debug, Clone, Copy)]
153pub struct TextPosition {
154    pub line: u64,
155    pub column: u64,
156    pub offset: u64,
157}