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
use crate::{
gen_parser::GenParserRequirement,
Error,
Parser,
};
use either::{
Either,
Left,
Right,
};
use std::fmt::{
Display,
Formatter,
Result as FmtResult,
};
#[derive(Debug)]
pub struct GenParserError<'a, P1, P2>
where
P1: Parser<'a>,
P2: Parser<'a>,
{
from: usize,
requirement: GenParserRequirement<'a, P1, P2>,
cause: Either<P1::Error, P2::Error>,
}
impl<'a, P1, P2> GenParserError<'a, P1, P2>
where
P1: Parser<'a>,
P2: Parser<'a>,
{
pub fn new(
from: usize,
requirement: GenParserRequirement<'a, P1, P2>,
cause: Either<P1::Error, P2::Error>,
) -> Self
{
Self {
from,
requirement,
cause,
}
}
}
impl<'a, P1, P2> Error for GenParserError<'a, P1, P2>
where
P1: Parser<'a>,
P2: Parser<'a>,
{
fn from(&self, f: &mut Formatter) -> FmtResult
{
write!(f, "{}", self.from)
}
fn requirement(&self, f: &mut Formatter) -> FmtResult
{
write!(f, "{}", self.requirement)
}
fn result(&self, f: &mut Formatter) -> FmtResult
{
match &self.cause
{
Left(_) => write!(f, "failed to parse {}", self.requirement.first()),
Right(_) => write!(f, "failed to parse {}", self.requirement.second().unwrap()),
}
}
fn causes(&self, f: &mut Formatter, depth: usize) -> FmtResult
{
match &self.cause
{
Left(err) => err.print(f, depth),
Right(err) => err.print(f, depth),
}
}
}
impl<'a, P1, P2> Display for GenParserError<'a, P1, P2>
where
P1: Parser<'a>,
P2: Parser<'a>,
{
fn fmt(&self, f: &mut Formatter) -> FmtResult
{
self.print(f, 0)
}
}