From 284ee73ffd347dcaf45e399755fd487925ab3b70 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Tue, 2 Jul 2024 13:24:16 +0200 Subject: [PATCH] Changed parsers to FnMut --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/multipart_file/Cargo.lock | 2 +- src/nom/combinators.rs | 21 ++++++++++++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e668c7c..1801eba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,7 +294,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lib" -version = "1.3.2" +version = "1.3.3" dependencies = [ "axum", "derive", diff --git a/Cargo.toml b/Cargo.toml index ad5f192..17c4811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lib" -version = "1.3.2" +version = "1.3.3" edition = "2021" authors = ["Martin Berg Alstad"] homepage = "emberal.github.io" diff --git a/examples/multipart_file/Cargo.lock b/examples/multipart_file/Cargo.lock index 088fe46..be2eaa3 100644 --- a/examples/multipart_file/Cargo.lock +++ b/examples/multipart_file/Cargo.lock @@ -286,7 +286,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lib" -version = "1.3.2" +version = "1.3.3" dependencies = [ "axum", "thiserror", diff --git a/src/nom/combinators.rs b/src/nom/combinators.rs index 3f61bfd..63fd448 100644 --- a/src/nom/combinators.rs +++ b/src/nom/combinators.rs @@ -17,7 +17,7 @@ use { /// - Returns: A parser that trims leading and trailing whitespace from the input and then runs the value from the inner parser pub fn trim<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R> where - Parser: Fn(&'a str) -> IResult<&'a str, R>, + Parser: FnMut(&'a str) -> IResult<&'a str, R>, { delimited(multispace0, inner, multispace0) } @@ -29,7 +29,7 @@ where /// - Returns: A parser that parses a parenthesized expression pub fn parenthesized<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R> where - Parser: Fn(&'a str) -> IResult<&'a str, R>, + Parser: FnMut(&'a str) -> IResult<&'a str, R>, { delimited(char('('), trim(inner), char(')')) } @@ -49,16 +49,15 @@ where pub fn exhausted<'a, Parser, R>(inner: Parser) -> impl FnMut(&'a str) -> IResult<&'a str, R> where - Parser: Fn(&'a str) -> IResult<&'a str, R>, + Parser: FnMut(&'a str) -> IResult<&'a str, R>, { terminated(inner, eof) } #[cfg(test)] mod tests { - use nom::bytes::streaming::take_while; - use super::*; + use nom::{bytes::complete::take_while, sequence::tuple}; #[test] fn test_trim_both_sides() { @@ -154,4 +153,16 @@ mod tests { let input = "test "; assert!(exhausted(take_where(4, |c: char| c.is_ascii_alphabetic()))(input).is_err()); } + + #[test] + fn test_exhausted_tuple() { + let input = "test"; + let (remaining, result) = exhausted(tuple(( + take_where(3, |c: char| c.is_ascii_alphabetic()), + take_while(|c: char| c.is_ascii_alphabetic()), + )))(input) + .unwrap(); + assert_eq!(remaining, ""); + assert_eq!(result, ("tes", "t")); + } }