Static Automata: Compile-time DFA generation framework.
This frameworks helps you define validation functions based on deterministic finite automata (DFAs) generated from ABNF grammars.
It works as follows:
- Declare a
moditem annotated with the#[grammar]macro specifying an ABNF grammar (either from a file, or in the doc comments). - Use the
cargo build-automatacommand line interface to generate the declared module file, containing the grammar compiled into a deterministic finite automaton. Alternatively you can call thebuild-automatalibrary directly from abuild.rsscript. - Use the validation functions provided in the generated module directly,
or use the
Validatederive macro to bind them to custom types.
Example
/// Automata module.
///
/// This module file is generated by the command line interface or builder
/// library. It contains an `Iri` type definition for the `IRI` production
/// of the `iri.abnf` that we exported here, with a `validate_bytes` and a
/// `validate_bytes` const function.
///
/// The attribute macro itself doesn't generate anything, but replaces this
/// item with an external module import `mod automata;`.
use ;
/// Derive the `validate_bytes` and `validate_str` methods from the
/// `automata::Iri` automaton.
;
Why not compile the grammars through the attribute macro?
Compiling a grammar requires determinizing a potentially large automaton, which is computationally very expensive.
Command line interface
You can install it with cargo install cargo-build-automata then use it
when you need to re-generate the autamata (e.g. when the associated grammar
changes):
Be careful, this will override the content of the modules annotated with the
#[grammar] attribute macro. If you're not sure which file will be
overriden you can run the cli with the -d/--dry-run flag:
It will compile the grammars, but not write anything.
Build script
The advantage of the command line interface is that it allows you to ship
the automata already compiled with your library/application. However you
might prefer to compile the automata on the user machine, using a build.rs
script. To do that you can use the build-automata library (the cli is
basically a wrapper around this library).
use build_automata;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (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.