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
use crate::Pos;
use pest::error::LineColLocation;
use pest::RuleType;
use std::fmt;

/// Parser error
#[derive(Error, Debug, PartialEq)]
pub struct Error {
    pub pos: Pos,
    pub message: String,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl<R: RuleType> From<pest::error::Error<R>> for Error {
    fn from(err: pest::error::Error<R>) -> Self {
        Error {
            pos: {
                let (line, column) = match err.line_col {
                    LineColLocation::Pos((line, column)) => (line, column),
                    LineColLocation::Span((line, column), _) => (line, column),
                };
                Pos { line, column }
            },
            message: err.to_string(),
        }
    }
}

/// Parser result
pub type Result<T> = std::result::Result<T, Error>;