Skip to main content

pddl_ish_parser/models/
parser_error.rs

1use crate::parser::error_context::get_error_context;
2
3#[derive(Debug, PartialEq)]
4pub struct ParserError {
5    pub description: String,
6    pub code: String,
7}
8
9impl ParserError {
10    pub fn new(description: String, code: String) -> Self {
11        Self { description, code }
12    }
13}
14
15impl nom::error::ParseError<&str> for ParserError {
16    fn from_error_kind(input: &str, kind: nom::error::ErrorKind) -> Self {
17        ParserError {
18            description: format!("Parsing error: {:?}. Original error: {:?}", kind, input),
19            code: get_error_context(input),
20        }
21    }
22
23    fn append(_: &str, _: nom::error::ErrorKind, other: Self) -> Self {
24        other
25    }
26}
27
28impl std::convert::From<nom::Err<nom::error::Error<&str>>> for ParserError {
29    fn from(err: nom::Err<nom::error::Error<&str>>) -> Self {
30        match err {
31            nom::Err::Error(e) | nom::Err::Failure(e) => ParserError {
32                description: format!("Parsing error: {:?}. Original error: {:?}", e.code, e.input),
33                code: get_error_context(e.input),
34            },
35            nom::Err::Incomplete(_) => ParserError {
36                description: "Parsing error: Incomplete".to_string(),
37                code: "".to_string(),
38            },
39        }
40    }
41}