# nom-derive
[](./LICENSE-MIT)
[](./LICENSE-APACHE)
[](https://travis-ci.org/chifflier/nom-derive)
## Overview
nom-derive is a custom derive attribute, to derive [nom] parsers automatically from the structure definition.
- [API documentation](https://docs.rs/nom-derive)
*This is only test code for now. Feedback welcome !*
## `#[derive(Nom)]`
This crate exposes a single custom-derive macro `Nom` which
implements `parse` for the struct it is applied to.
The goal of this project is that:
* `derive(Nom)` should be enough for you to derive [nom] parsers for simple
structures easily, without having to write it manually
* it allows overriding any parsing method by your own
* it allows using generated parsing functions along with handwritten parsers and
combining them without efforts
## Usage
Add to your `Cargo.toml` file::
```
nom-derive = "0.2"
nom = "4.2"
```
Add `#[derive(Nom)]` to the structure(s) you want.
See the [tests](https://github.com/rust-bakery/nom-derive/tree/master/tests)
directory for examples.
## Example
This implementation:
```rust
#[macro_use]
extern crate nom_derive;
#[macro_use]
extern crate nom;
use nom::*;
#[derive(Nom)]
struct SimpleStruct {
pub a: u32,
b: u64,
}
```
Allows you to generate the [nom] parser for `SimpleStruct` automatically. The function is called `SimpleStruct::parse`.
The generated code is equivalent to::
```rust
impl SimpleStruct {
fn parse(i: &[u8]) -> IResult<&[u8],SimpleStruct> {
do_parse!(
i,
a: be_u32 >>
b: be_u64 >>
( SimpleStruct{ a,b } )
)
}
}
```
[nom]: https://github.com/geal/nom
## Limitations
* Debugging macros is hard. Debugging macros generated by a custom derive is a
nightmare. The [expand](https://github.com/dtolnay/cargo-expand) cargo
subcommand can help !
## License
Licensed under either of
* Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.