Skip to content

Commit ff94030

Browse files
committed
Created dedicated ToFatalParser
1 parent d704115 commit ff94030

4 files changed

Lines changed: 53 additions & 23 deletions

File tree

rusty_pc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod supplier;
2727
mod surround;
2828
pub mod text;
2929
mod then_with;
30+
pub mod to_fatal;
3031
pub mod to_option;
3132
mod token;
3233
mod top_level;

rusty_pc/src/map_err.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ where
4242
fn map(&self, err: E) -> E;
4343
}
4444

45-
/// Converts a soft error into a fatal equivalent.
46-
pub struct ToFatalErrorMapper;
47-
48-
impl<E> ErrorMapper<E> for ToFatalErrorMapper
49-
where
50-
E: ParserErrorTrait,
51-
{
52-
fn map(&self, err: E) -> E {
53-
err.to_fatal()
54-
}
55-
}
56-
5745
/// Overrides a soft error with the given value.
5846
pub struct SoftErrorOverrider<E>(E);
5947

rusty_pc/src/parser.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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::{
14-
ErrorMapper, FatalErrorOverrider, MapErrParser, SoftErrorOverrider, ToFatalErrorMapper
15-
};
13+
use crate::map_err::{ErrorMapper, FatalErrorOverrider, MapErrParser, SoftErrorOverrider};
1614
use crate::no_context::NoContextParser;
1715
use crate::or_default::OrDefaultParser;
1816
use crate::peek::PeekParser;
17+
use crate::to_fatal::ToFatalParser;
1918
use crate::to_option::ToOptionParser;
2019

2120
/// A parser uses the given input in order to produce a result.
@@ -337,14 +336,6 @@ where
337336
self.map_err(FatalErrorOverrider::new(err))
338337
}
339338

340-
/// If this parser returns a soft error, it will be converted to a fatal error.
341-
fn to_fatal(self) -> MapErrParser<Self, ToFatalErrorMapper>
342-
where
343-
Self: Sized,
344-
{
345-
self.map_err(ToFatalErrorMapper)
346-
}
347-
348339
// =======================================================================
349340
// NoContext
350341
// =======================================================================
@@ -413,6 +404,18 @@ where
413404
ThenWithContextParser::new(self, other, combiner)
414405
}
415406

407+
// =======================================================================
408+
// ToFatal
409+
// =======================================================================
410+
411+
/// If this parser returns a soft error, it will be converted to a fatal error.
412+
fn to_fatal(self) -> ToFatalParser<Self>
413+
where
414+
Self: Sized,
415+
{
416+
ToFatalParser::new(self)
417+
}
418+
416419
// =======================================================================
417420
// ToOption
418421
// =======================================================================

rusty_pc/src/to_fatal.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
2+
use crate::{InputTrait, Parser, ParserErrorTrait};
3+
4+
pub struct ToFatalParser<P> {
5+
parser: P,
6+
}
7+
8+
impl<P> ToFatalParser<P> {
9+
pub(crate) fn new(parser: P) -> Self {
10+
Self { parser }
11+
}
12+
}
13+
14+
impl<I, C, P> MapDecorator<I, C> for ToFatalParser<P>
15+
where
16+
I: InputTrait,
17+
P: Parser<I, C>,
18+
{
19+
type OriginalOutput = P::Output;
20+
type Output = P::Output;
21+
type Error = P::Error;
22+
23+
fn decorated(
24+
&mut self,
25+
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
26+
&mut self.parser
27+
}
28+
29+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
30+
Ok(ok)
31+
}
32+
33+
fn map_soft_error(&self, err: Self::Error) -> Result<Self::Output, Self::Error> {
34+
Err(err.to_fatal())
35+
}
36+
}
37+
38+
impl<P> MapDecoratorMarker for ToFatalParser<P> {}

0 commit comments

Comments
 (0)