Adjust FnMut param in dbg_dmp since take/map are now FnMut#1845
Open
sebosp wants to merge 2 commits intorust-bakery:mainfrom
Open
Adjust FnMut param in dbg_dmp since take/map are now FnMut#1845sebosp wants to merge 2 commits intorust-bakery:mainfrom
sebosp wants to merge 2 commits intorust-bakery:mainfrom
Conversation
Signed-off-by: Seb Ospina <kraige@gmail.com>
|
I have been mucking around with this. I think a better implementation would be to implement it as a proper #[cfg(feature = "std")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "std")))]
pub fn dbg_dmp<'a, P, I, O, E>(parser: P, context: &'static str) -> DbgDmp<P>
where
E: std::fmt::Debug,
P: Parser<I, Output = O, Error = E>,
{
DbgDmp { parser, context }
}
/// dbg_dmp implementation
#[cfg(feature = "std")]
pub struct DbgDmp<P> {
parser: P,
context: &'static str,
}
#[cfg(feature = "std")]
impl<F, I> Parser<I> for DbgDmp<F>
where
I: HexDisplay + Clone,
F: Parser<I>,
<F as Parser<I>>::Error: std::fmt::Debug,
{
type Output = <F as Parser<I>>::Output;
type Error = <F as Parser<I>>::Error;
fn process<OM: OutputMode>(&mut self, input: I) -> PResult<OM, I, Self::Output, Self::Error> {
match self
.parser
.process::<OutputM<OM::Output, Emit, OM::Incomplete>>(input.clone())
{
Err(e) => {
eprintln!("{} Error: {:?} at:\n{}", self.context, e, input.to_hex(8));
match e {
Err::Error(e) => Err(Err::Error(OM::Error::bind(|| e))),
Err::Failure(e) => Err(Err::Failure(e)),
Err::Incomplete(e) => Err(Err::Incomplete(e)),
}
}
Ok(v) => Ok(v),
}
}
}(Edit: a slightly more concise code). It does look overly complex and I do not particularly like it. I adjusted the doctest to accommodate it and it passes. use nom::{Parser, IResult, error::dbg_dmp, bytes::tag};
fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
dbg_dmp(tag("abcd"), "tag").parse_complete(i)
}
let a = &b"efghijkl"[..];
// Will print the following message:
// tag Error: Error(Error { input: [101, 102, 103, 104, 105, 106, 107, 108], code: Tag }) at:
// 00000000 65 66 67 68 69 6a 6b 6c efghijkl
f(a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.