Crates.io | word_filter_codegen |
lib.rs | word_filter_codegen |
version | 0.7.0 |
source | src |
created_at | 2021-05-28 07:16:25.539671 |
updated_at | 2021-08-01 14:05:47.032156 |
description | Utilities for generating `WordFilter`s at compile-time. |
homepage | |
repository | https://github.com/Anders429/word_filter |
max_upload_size | |
id | 402988 |
size | 53,017 |
Tools for generating Word Filters.
This crate is intended to be used as a build utility for the
word_filter
crate. It is used to generate WordFilter
s
at compile-time using a WordFilterGenerator
.
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. Be sure their versions match.
[dependencies]
word_filter = "0.6.0"
[build-dependencies]
word_filter_codegen = "0.6.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 ***.");
This crate is guaranteed to compile on stable rustc 1.51.0
and up.
This project is licensed under either of
at your option.
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.