Module word_filter::codegen[][src]

Expand description

Tools for code generation.

This module is intended to be used in the context of a build script to generate code at compile time. To use, add word_filter 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 the word_filter crate to both the Cargo.toml [dependencies] and [build-dependencies] lists.

...
[dependencies]
word_filter = "0.7.0"

[build-dependencies]
word_filter = "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 repetition settings.

Flags defining separator settings.

Code generator for [WordFilter]s, following the builder pattern.

Enums

Visibility of generated code.