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
use std::fmt::{self, Display};
use thiserror::Error;

/// Error emitted by all parsers in this module.
#[derive(Error, Debug)]
#[error("failed to parse {kind}:\n{msg}")]
pub struct ParseError {
    kind: ParseErrorKind,
    msg: String,
}

impl ParseError {
    pub(crate) fn new<T>(kind: ParseErrorKind, msg: T) -> ParseError
    where
        T: Display,
    {
        ParseError {
            kind,
            msg: msg.to_string(),
        }
    }
}

/// The kind of `ParseError`.
#[derive(Debug)]
pub enum ParseErrorKind {
    /// Error parsing flat keys.
    FlatKey,
    /// Error parsing gron.
    Gron,
}

impl Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseErrorKind::FlatKey => write!(f, "flat key"),
            ParseErrorKind::Gron => write!(f, "gron"),
        }
    }
}