Skip to content

Commit 0b02594

Browse files
committed
Created dedicated parser for MapFatalErr
1 parent 504ed2e commit 0b02594

4 files changed

Lines changed: 48 additions & 83 deletions

File tree

rusty_pc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod many_ctx;
1616
pub mod map;
1717
pub mod map_ctx;
1818
pub mod map_decorator;
19-
pub mod map_err;
19+
pub mod map_fatal_err;
2020
pub mod map_soft_err;
2121
pub mod no_context;
2222
mod or;

rusty_pc/src/map_err.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

rusty_pc/src/map_fatal_err.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::{InputTrait, Parser, ParserErrorTrait};
2+
3+
/// A parser that maps the error of the decorated parser
4+
/// using the given mapper.
5+
pub struct MapFatalErrParser<P, E> {
6+
parser: P,
7+
err: E,
8+
}
9+
10+
impl<P, E> MapFatalErrParser<P, E> {
11+
pub(crate) fn new(parser: P, err: E) -> Self {
12+
Self { parser, err }
13+
}
14+
}
15+
16+
impl<I, C, P, E> Parser<I, C> for MapFatalErrParser<P, E>
17+
where
18+
I: InputTrait,
19+
P: Parser<I, C, Error = E>,
20+
E: ParserErrorTrait,
21+
{
22+
type Output = P::Output;
23+
type Error = P::Error;
24+
25+
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
26+
match self.parser.parse(input) {
27+
Ok(value) => Ok(value),
28+
Err(_) => Err(self.err.clone()),
29+
}
30+
}
31+
32+
fn set_context(&mut self, ctx: &C) {
33+
self.parser.set_context(ctx)
34+
}
35+
}

rusty_pc/src/parser.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::flatten::FlattenParser;
1010
use crate::many::{ManyCombiner, ManyParser, VecManyCombiner};
1111
use crate::map::{MapParser, MapToUnitParser};
1212
use crate::map_ctx::MapCtxParser;
13-
use crate::map_err::{ErrorMapper, FatalErrorOverrider, MapErrParser};
13+
use crate::map_fatal_err::MapFatalErrParser;
1414
use crate::map_soft_err::MapSoftErrParser;
1515
use crate::no_context::NoContextParser;
1616
use crate::or_default::OrDefaultParser;
@@ -290,21 +290,25 @@ where
290290
}
291291

292292
// =======================================================================
293-
// MapSoftErr
293+
// MapFatalErr
294294
// =======================================================================
295295

296-
/// Maps the error of this parser.
296+
/// Maps the fatal error of this parser.
297297
/// If the parser is successful, the value is returned as-is.
298-
/// If the parser returns an error, the given mapper is used to map the error
299-
/// to a new error.
300-
fn map_err<F>(self, mapper: F) -> MapErrParser<Self, F>
298+
/// If the parser returns a soft error, the error is returned as-is.
299+
/// If the parser returns a fatal error, it is replaced by the given error.
300+
fn map_fatal_err(self, err: Self::Error) -> MapFatalErrParser<Self, Self::Error>
301301
where
302302
Self: Sized,
303-
F: ErrorMapper<Self::Error>,
304303
{
305-
MapErrParser::new(self, mapper)
304+
assert!(err.is_fatal());
305+
MapFatalErrParser::new(self, err)
306306
}
307307

308+
// =======================================================================
309+
// MapSoftErr
310+
// =======================================================================
311+
308312
/// If this parser returns a soft error, the soft error will be replaced by
309313
/// the given error (which might be soft or fatal).
310314
fn with_soft_err(self, err: Self::Error) -> MapSoftErrParser<Self, Self::Error>
@@ -324,19 +328,6 @@ where
324328
self.with_soft_err(err)
325329
}
326330

327-
/// If this parser returns a fatal error, the fatal error will be replaced by the given error
328-
/// (which must be fatal too).
329-
fn with_fatal_err(
330-
self,
331-
err: Self::Error,
332-
) -> MapErrParser<Self, FatalErrorOverrider<Self::Error>>
333-
where
334-
Self: Sized,
335-
{
336-
assert!(err.is_fatal());
337-
self.map_err(FatalErrorOverrider::new(err))
338-
}
339-
340331
// =======================================================================
341332
// NoContext
342333
// =======================================================================

0 commit comments

Comments
 (0)