ng_oxigraph/oxttl/toolkit/
error.rs1use std::ops::Range;
2use std::{fmt, io};
3
4#[derive(Eq, PartialEq, Debug, Clone, Copy)]
6pub struct TextPosition {
7 pub line: u64,
8 pub column: u64,
9 pub offset: u64,
10}
11
12#[derive(Debug, thiserror::Error)]
16pub struct TurtleSyntaxError {
17 pub(super) location: Range<TextPosition>,
18 pub(super) message: String,
19}
20
21impl TurtleSyntaxError {
22 #[inline]
24 pub fn location(&self) -> Range<TextPosition> {
25 self.location.clone()
26 }
27
28 #[inline]
30 pub fn message(&self) -> &str {
31 &self.message
32 }
33}
34
35impl fmt::Display for TurtleSyntaxError {
36 #[inline]
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 if self.location.start.offset + 1 >= self.location.end.offset {
39 write!(
40 f,
41 "Parser error at line {} column {}: {}",
42 self.location.start.line + 1,
43 self.location.start.column + 1,
44 self.message
45 )
46 } else if self.location.start.line == self.location.end.line {
47 write!(
48 f,
49 "Parser error between at line {} between columns {} and column {}: {}",
50 self.location.start.line + 1,
51 self.location.start.column + 1,
52 self.location.end.column + 1,
53 self.message
54 )
55 } else {
56 write!(
57 f,
58 "Parser error between line {} column {} and line {} column {}: {}",
59 self.location.start.line + 1,
60 self.location.start.column + 1,
61 self.location.end.line + 1,
62 self.location.end.column + 1,
63 self.message
64 )
65 }
66 }
67}
68
69impl From<TurtleSyntaxError> for io::Error {
70 #[inline]
71 fn from(error: TurtleSyntaxError) -> Self {
72 Self::new(io::ErrorKind::InvalidData, error)
73 }
74}
75
76#[derive(Debug, thiserror::Error)]
80pub enum TurtleParseError {
81 #[error(transparent)]
83 Io(#[from] io::Error),
84 #[error(transparent)]
86 Syntax(#[from] TurtleSyntaxError),
87}
88
89impl From<TurtleParseError> for io::Error {
90 #[inline]
91 fn from(error: TurtleParseError) -> Self {
92 match error {
93 TurtleParseError::Syntax(e) => e.into(),
94 TurtleParseError::Io(e) => e,
95 }
96 }
97}