Skip to content

Commit

Permalink
Create Parser implementation for rest
Browse files Browse the repository at this point in the history
  • Loading branch information
gadunga committed Feb 12, 2025
1 parent a44b52e commit 2b59c45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
/// bits::<_, _, Error<(&[u8], usize)>, _, _>((
/// take(4usize),
/// take(8usize),
/// bytes::<_, _, Error<&[u8]>, _, _>(rest)
/// bytes::<_, _, Error<&[u8]>, _, _>(rest())
/// ))(input)
/// }
///
Expand Down
34 changes: 27 additions & 7 deletions src/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,37 @@ mod tests;
/// Return the remaining input.
///
/// ```rust
/// # use nom::error::ErrorKind;
/// use nom::error;
/// use nom::Parser;
/// use nom::combinator::rest;
/// assert_eq!(rest::<_,(_, ErrorKind)>("abc"), Ok(("", "abc")));
/// assert_eq!(rest::<_,(_, ErrorKind)>(""), Ok(("", "")));
/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete("abc"), Ok(("", "abc")));
/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete(""), Ok(("", "")));
/// ```
#[inline]
pub fn rest<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
pub fn rest<T, E: ParseError<T>>() -> impl Parser<T, Output = T, Error = E>
where
T: Input,
{
Ok(input.take_split(input.input_len()))
Rest {
i: PhantomData,
e: PhantomData,
}
}

/// Parser implementation for [rest]
pub struct Rest<I, E> {
i: PhantomData<I>,
e: PhantomData<E>,
}

impl<I: Input, E: ParseError<I>> Parser<I> for Rest<I, E> {
type Output = I;
type Error = E;

fn process<OM: OutputMode>(&mut self, input: I) -> PResult<OM, I, Self::Output, Self::Error> {
let (i, o) = input.take_split(input.input_len());
Ok((i, OM::Output::bind(|| o)))
}
}

/// Return the length of the remaining input.
Expand Down Expand Up @@ -766,7 +786,7 @@ where
/// fn parser(input: &str) -> IResult<&str, &str> {
/// alt((
/// preceded(one_of("+-"), digit1),
/// rest
/// rest()
/// )).parse(input)
/// }
///
Expand All @@ -789,7 +809,7 @@ where
/// fn parser(input: &str) -> IResult<&str, &str> {
/// alt((
/// preceded(one_of("+-"), cut(digit1)),
/// rest
/// rest()
/// )).parse(input)
/// }
///
Expand Down
5 changes: 3 additions & 2 deletions src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::internal::{Err, IResult, Needed};
#[cfg(feature = "alloc")]
use crate::lib::std::boxed::Box;
use crate::number::complete::u8;
use crate::Parser;

macro_rules! assert_parse(
($left: expr, $right: expr) => {
Expand Down Expand Up @@ -73,14 +74,14 @@ fn end_of_input() {
fn rest_on_slices() {
let input: &[u8] = &b"Hello, world!"[..];
let empty: &[u8] = &b""[..];
assert_parse!(rest(input), Ok((empty, input)));
assert_parse!(rest().parse_complete(input), Ok((empty, input)));
}

#[test]
fn rest_on_strs() {
let input: &str = "Hello, world!";
let empty: &str = "";
assert_parse!(rest(input), Ok((empty, input)));
assert_parse!(rest().parse_complete(input), Ok((empty, input)));
}

#[test]
Expand Down

0 comments on commit 2b59c45

Please sign in to comment.