spindle_lib/
error.rs

1use std::collections::HashSet;
2use std::fmt;
3
4/// The type of error that can occur when parsing a grammar.
5#[derive(Debug, PartialEq)]
6pub struct Error(pub(crate) ErrorRepr);
7
8impl std::error::Error for Error {
9    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
10        match &self.0 {
11            ErrorRepr::Regex(e) => Some(e),
12            ErrorRepr::Grammar(e) => Some(e),
13            _ => None,
14        }
15    }
16}
17
18#[derive(Debug, PartialEq)]
19pub(crate) enum ErrorRepr {
20    Regex(crate::regex::Error),
21    Grammar(peg::error::ParseError<peg::str::LineCol>),
22    UnkownVar(String),
23    DuplicateVars(HashSet<String>),
24    Reserved(Vec<String>),
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match &self.0 {
30            ErrorRepr::Regex(e) => e.fmt(f),
31            ErrorRepr::Grammar(e) => e.fmt(f),
32            ErrorRepr::UnkownVar(e) => write!(f, "Unkown variable reference: {}", e),
33            ErrorRepr::DuplicateVars(e) => write!(f, "Duplicate variable definitions: {:?}", e),
34            ErrorRepr::Reserved(e) => write!(f, "Reserved variable name: {:?}", e),
35        }
36    }
37}