1use std::{fmt::Display, error::Error};
2use crate::{Span, FrozenSpan};
3
4#[derive(Debug, PartialEq)]
5pub struct ParseError {
6 span: FrozenSpan,
7 kind: ParseErrorKind,
9}
10
11impl ParseError {
12 pub fn new<'a>(span: Span<'a>, kind: ParseErrorKind) -> ParseError {
13 ParseError { span: span.frozen(), kind, }
14 }
15}
16
17impl Error for ParseError {}
18
19#[derive(Debug, PartialEq)]
20pub enum ParseErrorKind {
21 Starving { found: usize, required: usize },
22 Unexpected { found: String, expected: String },
23 Neither(Vec<ParseError>),
24 ConditionFailed,
25 Other,
26}
27
28impl Display for ParseErrorKind {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 match &self {
31 ParseErrorKind::Starving { found, required } => write!(f, "Found {} units but expected {}.", found, required),
32 ParseErrorKind::Unexpected { found, expected } => write!(f, "Unexpected: '{}', Expected: '{}'", found, expected),
33 ParseErrorKind::Neither(errors) => write!(f, "Neither parser succeeded: {:?}", errors),
34 ParseErrorKind::ConditionFailed => write!(f, "Condition failed."),
35 ParseErrorKind::Other => write!(f, "Unknown error.")
36 }
37 }
38}
39
40impl Display for ParseError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 write!(f, "{}", self.kind)
44 }
45}
46
47pub type ParseResult<'a, T> = std::result::Result<(Span<'a>, T), ParseError>;