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
use std::fmt;

use nom::error::{ContextError, ErrorKind, FromExternalError, ParseError};
use nom::InputLength;

/// [nom::branch::alt] return the last error of the branch by default
///
/// With a custom error type it is possible to have
/// [nom::branch::alt] return the error of the parser
/// that went the farthest in the input data.
///
/// There is little difference between [ParseSQLError] and [nom::error::VerboseError]
#[derive(Clone, Debug, PartialEq)]
pub struct ParseSQLError<I>
where
    I: InputLength,
{
    pub errors: Vec<(I, ParseSQLErrorKind)>,
}

#[derive(Clone, Debug, PartialEq)]
/// Error context for `ParseSQLError`
pub enum ParseSQLErrorKind {
    /// Static string added by the `context` function
    Context(&'static str),
    /// Indicates which character was expected by the `char` function
    Char(char),
    /// Error kind given by various nom parsers
    Nom(ErrorKind),
}

impl<I> ParseError<I> for ParseSQLError<I>
where
    I: InputLength,
{
    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
        ParseSQLError {
            errors: vec![(input, ParseSQLErrorKind::Nom(kind))],
        }
    }

    fn append(input: I, kind: ErrorKind, mut other: Self) -> Self {
        other.errors.push((input, ParseSQLErrorKind::Nom(kind)));
        other
    }

    fn from_char(input: I, c: char) -> Self {
        ParseSQLError {
            errors: vec![(input, ParseSQLErrorKind::Char(c))],
        }
    }

    fn or(self, other: Self) -> Self {
        if self.errors[0].0.input_len() >= other.errors[0].0.input_len() {
            other
        } else {
            self
        }
    }
}

impl<I: nom::InputLength> ContextError<I> for ParseSQLError<I> {
    fn add_context(input: I, ctx: &'static str, mut other: Self) -> Self {
        other.errors.push((input, ParseSQLErrorKind::Context(ctx)));
        other
    }
}

impl<I: InputLength, E> FromExternalError<I, E> for ParseSQLError<I> {
    /// Create a new error from an input position and an external error
    fn from_external_error(input: I, kind: ErrorKind, _e: E) -> Self {
        Self::from_error_kind(input, kind)
    }
}

impl<I: fmt::Display + InputLength> fmt::Display for ParseSQLError<I> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Parse error:")?;
        for (input, error) in &self.errors {
            match error {
                ParseSQLErrorKind::Nom(e) => writeln!(f, "{:?} at: {}", e, input)?,
                ParseSQLErrorKind::Char(c) => writeln!(f, "expected '{}' at: {}", c, input)?,
                ParseSQLErrorKind::Context(s) => writeln!(f, "in section '{}', at: {}", s, input)?,
            }
        }

        Ok(())
    }
}

impl<I: fmt::Debug + fmt::Display + InputLength> std::error::Error for ParseSQLError<I> {}