Thanks to visit codestin.com
Credit goes to docs.rs

Skip to main content

markab_parser/stringify_parser/
error.rs

1use crate::{
2	stringify_parser::StringifyParserRequirement,
3	Error,
4	Parser,
5};
6use std::fmt::{
7	Display,
8	Formatter,
9	Result as FmtResult,
10};
11
12#[derive(Debug)]
13pub struct StringifyParserError<'a, P>
14where
15	P: Parser<'a>,
16{
17	from: usize,
18	requirement: StringifyParserRequirement<'a, P>,
19	err: P::Error,
20}
21
22impl<'a, P> StringifyParserError<'a, P>
23where
24	P: Parser<'a>,
25{
26	pub fn new(from: usize, requirement: StringifyParserRequirement<'a, P>, err: P::Error) -> Self
27	{
28		Self {
29			from,
30			requirement,
31			err,
32		}
33	}
34}
35
36impl<'a, P> Error for StringifyParserError<'a, P>
37where
38	P: Parser<'a>,
39{
40	fn from(&self, f: &mut Formatter) -> FmtResult
41	{
42		write!(f, "{}", self.from)
43	}
44
45	fn requirement(&self, f: &mut Formatter) -> FmtResult
46	{
47		write!(f, "{}", self.requirement)
48	}
49
50	fn result(&self, f: &mut Formatter) -> FmtResult
51	{
52		write!(f, "failed to parse")
53	}
54
55	fn causes(&self, f: &mut Formatter, depth: usize) -> FmtResult
56	{
57		self.err.print(f, depth)
58	}
59
60	fn print(&self, f: &mut Formatter, depth: usize) -> FmtResult
61	{
62		self.causes(f, depth)
63	}
64}
65
66impl<'a, P> Display for StringifyParserError<'a, P>
67where
68	P: Parser<'a>,
69{
70	fn fmt(&self, f: &mut Formatter) -> FmtResult
71	{
72		self.print(f, 0)
73	}
74}