pub struct Maybe<T: Pattern>(pub T);Expand description
A pattern that matches 0 or 1 occurrences of the inner pattern T, making it optional.
Because both 0 and 1 occurrences are valid matches, Maybe never fails:
Pattern::immediate_match and Pattern::trailing_match always return Ok, falling
back to an empty match at the relevant end of the input if T doesn’t match there. Likewise,
Pattern::first_match / Pattern::first_match_ex always match at the very start of the
input.
More conveniently created via Pattern::maybe
§Example
use {
shrimple_parser::{
Pattern,
parser::one,
pattern::Maybe,
},
core::convert::Infallible,
};
// Matches an optional '-' sign at the start of the input.
assert_eq!(
one::<_, Infallible>('-'.maybe())("-123"),
Ok(("123", "-")),
);
// No '-' present - still matches, with an empty matched fragment.
assert_eq!(
one::<_, Infallible>('-'.maybe())("123"),
Ok(("123", "")),
);Tuple Fields§
§0: TTrait Implementations§
impl<T: Copy + Pattern> Copy for Maybe<T>
Source§impl<T: Pattern> Pattern for Maybe<T>
impl<T: Pattern> Pattern for Maybe<T>
Source§fn immediate_matches<I: Input>(&self, input: I) -> (I, I)
fn immediate_matches<I: Input>(&self, input: I) -> (I, I)
Repeating “0 or 1 T” is the same as “0 or more T”, so this delegates directly to
T’s implementation rather than looping on Pattern::immediate_match, which would
never terminate since Maybe::immediate_match never fails.
Source§fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)
fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)
See Maybe::immediate_matches for why this delegates straight to T instead of
looping on Pattern::trailing_match.
Source§fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>
fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>
Source§fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))
fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))
Pattern::immediate_matches, but also counts the number of matches. Read moreSource§fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>
fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>
Pattern::immediate_match, but matches at the end of input.
The return values are (the input before the match, the match) Read moreSource§fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>
fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>
Source§fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>
fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>
Pattern::first_match, but the match is excluded from the rest of the input. Read moreSource§fn by_ref(&self) -> Ref<'_, Self>
fn by_ref(&self) -> Ref<'_, Self>
Source§fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other>where
Self: Sized,
fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other>where
Self: Sized,
self and another pattern into a pattern that matches both of them in a sequence,
with self before other Read moreSource§fn not_escaped_by<Prefix: Pattern>(
self,
prefix: Prefix,
) -> NotEscaped<Prefix, Self>where
Self: Sized,
fn not_escaped_by<Prefix: Pattern>(
self,
prefix: Prefix,
) -> NotEscaped<Prefix, Self>where
Self: Sized,
self only if it’s not escaped (immediately preceded)
by the provided pattern. Read moreSource§fn not_enclosed_by<Enclosure: Pattern>(
self,
enc: Enclosure,
) -> NotEnclosed<Enclosure, Self>where
Self: Sized,
fn not_enclosed_by<Enclosure: Pattern>(
self,
enc: Enclosure,
) -> NotEnclosed<Enclosure, Self>where
Self: Sized,
self only if it’s not enclosed (preceded & superceded) by
the provided pattern. Read moreAuto Trait Implementations§
impl<T> Freeze for Maybe<T>where
T: Freeze,
impl<T> RefUnwindSafe for Maybe<T>where
T: RefUnwindSafe,
impl<T> Send for Maybe<T>where
T: Send,
impl<T> Sync for Maybe<T>where
T: Sync,
impl<T> Unpin for Maybe<T>where
T: Unpin,
impl<T> UnsafeUnpin for Maybe<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Maybe<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more