Skip to content

Commit d6d2760

Browse files
committed
chore(pc): Move no_context to its own module
1 parent d7f0a38 commit d6d2760

4 files changed

Lines changed: 44 additions & 31 deletions

File tree

rusty_pc/src/ctx.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,6 @@ where
4444
self.0 = Some(ctx.clone());
4545
}
4646
}
47-
48-
pub struct NoContextParser<P, C1, C2> {
49-
parser: P,
50-
_marker: PhantomData<(C1, C2)>,
51-
}
52-
53-
impl<P, C1, C2> NoContextParser<P, C1, C2> {
54-
pub(crate) fn new(parser: P) -> Self {
55-
Self {
56-
parser,
57-
_marker: PhantomData,
58-
}
59-
}
60-
}
61-
62-
impl<I, C1, C2, P> Parser<I, C2> for NoContextParser<P, C1, C2>
63-
where
64-
P: Parser<I, C1>,
65-
I: InputTrait,
66-
{
67-
type Output = P::Output;
68-
type Error = P::Error;
69-
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
70-
self.parser.parse(input)
71-
}
72-
73-
fn set_context(&mut self, _ctx: &C2) {}
74-
}
75-
7647
/// Based on the boolean context, parses using the left or the right parser.
7748
pub struct IifParser<L, R> {
7849
left: L,

rusty_pc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod many_ctx;
1515
pub mod map;
1616
pub mod map_ctx;
1717
pub mod map_err;
18+
pub mod no_context;
1819
mod or;
1920
pub mod or_default;
2021
mod parser;

rusty_pc/src/no_context.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::marker::PhantomData;
2+
3+
use crate::{InputTrait, Parser};
4+
5+
/// Stops propagating the context to the underlying parser.
6+
/// The underlying parser might also have a different context type,
7+
/// so this can be used to resolve context type mismatches,
8+
/// as long as the underlying parser does not use the parent context.
9+
pub struct NoContextParser<P, COut, CIn> {
10+
parser: P,
11+
_marker: PhantomData<(COut, CIn)>,
12+
}
13+
14+
impl<P, COut, CIn> NoContextParser<P, COut, CIn> {
15+
pub(crate) fn new(parser: P) -> Self {
16+
Self {
17+
parser,
18+
_marker: PhantomData,
19+
}
20+
}
21+
}
22+
23+
impl<I, COut, CIn, P> Parser<I, COut> for NoContextParser<P, COut, CIn>
24+
where
25+
P: Parser<I, CIn>,
26+
I: InputTrait,
27+
{
28+
type Output = P::Output;
29+
type Error = P::Error;
30+
31+
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
32+
self.parser.parse(input)
33+
}
34+
35+
fn set_context(&mut self, _ctx: &COut) {}
36+
}

rusty_pc/src/parser.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ThenWithContextParser;
12
use crate::and::{AndParser, Combiner, KeepLeftCombiner, KeepRightCombiner, TupleCombiner};
23
use crate::and_then::AndThenParser;
34
use crate::and_then_err::AndThenErrParser;
@@ -12,10 +13,10 @@ use crate::map_ctx::MapCtxParser;
1213
use crate::map_err::{
1314
ErrorMapper, FatalErrorOverrider, MapErrParser, SoftErrorOverrider, ToFatalErrorMapper
1415
};
16+
use crate::no_context::NoContextParser;
1517
use crate::or_default::OrDefaultParser;
1618
use crate::peek::PeekParser;
1719
use crate::to_option::ToOptionParser;
18-
use crate::{NoContextParser, ThenWithContextParser};
1920

2021
/// A parser uses the given input in order to produce a result.
2122
pub trait Parser<I, C = ()>
@@ -348,7 +349,11 @@ where
348349
// NoContext
349350
// =======================================================================
350351

351-
fn no_context<C2>(self) -> NoContextParser<Self, C, C2>
352+
/// Stops propagating the context to the underlying parser.
353+
/// The underlying parser might also have a different context type,
354+
/// so this can be used to resolve context type mismatches,
355+
/// as long as the underlying parser does not use the parent context.
356+
fn no_context<COut>(self) -> NoContextParser<Self, COut, C>
352357
where
353358
Self: Sized,
354359
{

0 commit comments

Comments
 (0)