Crate word_filter_codegen[−][src]
Expand description
Code generation for the word_filter
crate.
This crate is intended to be used alongside the word_filter
crate to generate code at compile
time. To use, add word_filter_codegen
to the [build-dependency]
list and generate the code
in a build.rs
file. Then include!
the file in lib.rs
.
Example
For example, a simple WordFilter
can be generated by the following.
First, add both the word_filter
and word_filter_codegen
crates to the Cargo.toml
[dependencies]
and [build-dependencies]
lists respectively.
... [dependencies] word_filter = "0.7.0" [build-dependencies] word_filter_codegen = "0.7.0" ...
Next, generate the WordFilter
in the build.rs
file.
use std::{ env, fs::File, io::{BufWriter, Write}, path::Path, }; use word_filter_codegen::{Visibility, WordFilterGenerator}; fn main() { let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs"); let mut file = BufWriter::new(File::create(&path).unwrap()); writeln!( &mut file, "{}", WordFilterGenerator::new() .visibility(Visibility::Pub) .word("foo") .generate("FILTER") ); }
And finally, include the generated code in the lib.rs
file.
ⓘ
include!(concat!(env!("OUT_DIR"), "/codegen.rs")); assert!(FILTER.censor("Should censor foo."), "Should censor ***.");
Structs
Flags defining separator settings.
Code generator for WordFilter
s, following the builder pattern.
Enums
Visibility of generated code.