The generate serialize command automates the process of compiling a regex into a DFA, writing the binary serialization to files, and generating Rust code to deserialize it lazily.
Command Structure:
regex-cli generate serialize <type> <variable_name> <output_dir> <pattern>
Arguments:
<type>:sparse: Sacrifices search performance for smaller DFA size.dense: Faster searches but larger DFAs.dfa: Generates a single DFA (finds end of match).regex: Generates two DFAs (one for end of match, one for start) to support full regex functionality.
<variable_name>: The name of the variable used in the generated Rust source.<output_dir>: The directory where files will be written.<pattern>: The regex pattern(s) to build the DFA for.
Common Flags:
--minimize: Applies a DFA minimization algorithm to shrink the size.--shrink: Uses heuristics to make the NFA smaller, speeding up determinization.--start-kind anchored: Builds a DFA that only supports anchored searches (matches must start at the search offset).--rustfmt: Runs rustfmt on the generated Rust code.--safe: Uses only safe Rust code for deserialization.
Workflow Example:
- Add
regex-automata to your Cargo.toml with features std and dfa-search. - Run the
regex-cli command to generate files. - Include the generated module in your Rust project and use the constant to perform searches.
regex-cli generate serialize sparse dfa \
--minimize \
--shrink \
--start-kind anchored \
--rustfmt \
--safe \
SIMPLE_WORD_FWD \
./src/ \
"\w"