Crate nom_parse_macros

Source
Expand description

§nom-parse-trait

This macro generates a ParseFrom implementation for a struct or enum using the provided nom expression(s). The expression should return a tuple for the parsed fields.

The parse_from() macro can be used in 2 separate ways. The first one is using an expression that results in a nom parser. This is generic and can be useful in many cases, since you have the full flexibility of nom functions and combinators. The second one is a very simple one that matches a string verbatim. You do this by starting the expression with the match keyword. This is useful when you have a very simple format that you want to parse.

§nom functions

The expression in the parse_from attribute will be translated to be using valid nom functions. The main function here is to automatically put the namespace before the function name, so you don’t need a ton of use statements in your code. But there are also a couple of special cases:

  • {} or () will be replaced with a nom_parse_trait::ParseFrom::parse call for the corresponding field. This is useful when you are using types that have implemented the ParseFrom trait already.
  • Strings, bytes strings and characters will be translated to match the input verbatim using the nom::bytes::complete::tag function.

§Input types that are supported

The generated ParseFrom implementation is made to be very generic, where it supports any input and error type from nom. This is done with a where clause with many traits that the input should have implemented. All of these are true for the standard &str and &[u8] types.

If you run into a situation where the trait limitations on the input type does not match your use case, please open an issue on the GitHub repository.

§Known limitations

  • When your try to use a custom parser combinator, the nom function parser will try to change all parameters to be nom parsers. This is useful in many cases, but when you need to pass in a normal string for example, it won’t work. In these cases, you can define a separate function to wrap the call. I’m not sure how to fix that right now, but I’m open to suggestions.

  • Since the generated input type is very generic, all functions that you want to use in the nom expression should also be very generic. In the future I might add a way to specify if you want to generate a specific input type, but for now it’s not possible.

Attribute Macros§

parse_from
This macro generates a nom_parse_trait::ParseFrom implementation for a struct or enum using the provided nom expression(s). The expression should return a tuple for the parsed fields.