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
100
101
102
103
104
105
106
use crate::MAX_STACK_SIZE;
use oxilangtag::LanguageTagParseError;
use oxiri::IriParseError;
use rio_api::parser::{LineBytePosition, ParseError};
use std::char;
use std::error::Error;
use std::fmt;
use std::io;

/// Error that might be returned during parsing.
///
/// It might wrap an IO error or be a parsing error.
#[derive(Debug)]
pub struct TurtleError {
    pub(crate) kind: TurtleErrorKind,
    pub(crate) position: Option<LineBytePosition>,
}

#[derive(Debug)]
pub enum TurtleErrorKind {
    Io(io::Error),
    UnknownPrefix(String),
    PrematureEof,
    UnexpectedByte(u8),
    InvalidUnicodeCodePoint(u32),
    InvalidIri {
        iri: String,
        error: IriParseError,
    },
    InvalidLanguageTag {
        tag: String,
        error: LanguageTagParseError,
    },
    StackOverflow,
}

impl fmt::Display for TurtleError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            TurtleErrorKind::Io(error) => return error.fmt(f),
            TurtleErrorKind::UnknownPrefix(prefix) => write!(f, "unknown prefix '{}'", prefix),
            TurtleErrorKind::PrematureEof => write!(f, "premature end of file"),
            TurtleErrorKind::UnexpectedByte(c) => match char::from_u32(u32::from(*c)) {
                Some(c) => write!(f, "unexpected character '{}'", c.escape_debug()),
                None => write!(f, "unexpected byter {}", c),
            },
            TurtleErrorKind::InvalidUnicodeCodePoint(point) => {
                write!(f, "invalid unicode code point '{}'", point)
            }
            TurtleErrorKind::InvalidIri { iri, error } => {
                write!(f, "error while parsing IRI '{}': {}", iri, error)
            }
            TurtleErrorKind::InvalidLanguageTag { tag, error } => {
                write!(f, "error while parsing language tag '{}': {}", tag, error)
            }
            TurtleErrorKind::StackOverflow => {
                write!(f, "The parser encountered more than {} nested constructions. This number is limited in order to avoid stack overflow OS errors.", MAX_STACK_SIZE)
            }
        }?;
        if let Some(position) = self.position {
            write!(
                f,
                " on line {} at position {}",
                position.line_number(),
                position.byte_number(),
            )?;
        }
        Ok(())
    }
}

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

impl ParseError for TurtleError {
    fn textual_position(&self) -> Option<LineBytePosition> {
        self.position
    }
}

impl From<io::Error> for TurtleError {
    fn from(error: io::Error) -> Self {
        Self {
            kind: TurtleErrorKind::Io(error),
            position: None,
        }
    }
}

impl From<TurtleError> for io::Error {
    fn from(error: TurtleError) -> Self {
        match error.kind {
            TurtleErrorKind::Io(error) => error,
            TurtleErrorKind::PrematureEof => io::Error::new(io::ErrorKind::UnexpectedEof, error),
            _ => io::Error::new(io::ErrorKind::InvalidData, error),
        }
    }
}