From df2c1ece94286f7515fc5e1f71b3dc5ca8242fb7 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 24 Feb 2025 17:17:17 +0100 Subject: [PATCH] Add `Parser` implementation for `nom::combinator::value` --- src/combinator/mod.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index fe6655b9..14ae0796 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -564,7 +564,30 @@ pub fn value, F>( where F: Parser, { - parser.map(move |_| val.clone()) + Value { val, parser } +} + +/// Parser implementation for [value] +pub struct Value { + parser: F, + val: O1, +} + +impl Parser for Value +where + F: Parser, + O1: Clone, + E: ParseError, +{ + type Output = O1; + type Error = E; + fn process( + &mut self, + input: I, + ) -> PResult { + self.parser.process::>(input) + .map(|(i, _)| ( i, OM::Output::bind(|| self.val.clone()) )) + } } /// Succeeds if the child parser returns an error.