WordFilterGenerator

Struct WordFilterGenerator 

Source
pub struct WordFilterGenerator { /* private fields */ }
Expand description

Code generator for WordFilters, following the builder pattern.

Generates code that can be compiled to a WordFilter. Filtered words, ignored exceptions, allowed separators, and character aliases may all be provided through the associated methods.

§Example

use word_filter_codegen::{Visibility, WordFilterGenerator};

let generated_code = WordFilterGenerator::new()
    .visibility(Visibility::Pub)
    .word("foo")
    .exception("foobar")
    .separator(' ')
    .alias('f', 'F')
    .generate("FILTER");

The generated code can then be written to a file in the OUT_DIR. See crate-level documentation for more details.

Implementations§

Source§

impl WordFilterGenerator

Source

pub fn new() -> Self

Creates a new WordFilterGenerator.

This is equivalent to WordFilterGenerator::default().

§Example
use word_filter_codegen::WordFilterGenerator;

let generator = WordFilterGenerator::new();
Source

pub fn word<S>(&mut self, word: S) -> &mut Self
where S: ToString,

Add a single word.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.word("foo");
Source

pub fn words<I, S>(&mut self, words: I) -> &mut Self
where I: IntoIterator<Item = S>, S: ToString,

Add words.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.words(&["foo", "bar"]);
Source

pub fn exception<S>(&mut self, exception: S) -> &mut Self
where S: ToString,

Add a single exception.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.exception("foo");
Source

pub fn exceptions<I, S>(&mut self, exceptions: I) -> &mut Self
where I: IntoIterator<Item = S>, S: ToString,

Add exceptions.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.exceptions(&["foo", "bar"]);
Source

pub fn separator<S>(&mut self, separator: S) -> &mut Self
where S: ToString,

Add a single separator.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.separator("foo");
Source

pub fn separators<I, S>(&mut self, separators: I) -> &mut Self
where I: IntoIterator<Item = S>, S: ToString,

Add separators.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.separators(&["foo", "bar"]);
Source

pub fn alias<S, T>(&mut self, source: S, alias: T) -> &mut Self
where S: ToString, T: ToString,

Add a single alias.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.alias("foo", "bar");
Source

pub fn aliases<'a, I, S, T>(&mut self, aliases: I) -> &mut Self
where I: IntoIterator<Item = &'a (S, T)>, S: ToString + 'a, T: ToString + 'a,

Add aliases.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.aliases(&[("foo", "bar"), ("baz", "qux")]);
Source

pub fn visibility(&mut self, visibility: Visibility) -> &mut Self

Set visibility of the generated code.

§Example
use word_filter_codegen::{Visibility, WordFilterGenerator};

let mut generator = WordFilterGenerator::new();
generator.visibility(Visibility::Pub);
Source

pub fn separator_flags(&mut self, separator_flags: SeparatorFlags) -> &mut Self

Source

pub fn doc<S>(&mut self, doc: S) -> &mut Self
where S: ToString,

Set the doc string of the generated code.

The generated code will be generated with doc as the item-level doc-string.

§Example
use word_filter_codegen::WordFilterGenerator;

let mut generator = WordFilterGenerator::new();
generator.doc("foo");
§Multiple Lines

For doc strings that contain multiple lines, users are advised to use the indoc crate.

use word_filter_codegen::WordFilterGenerator;
use indoc::indoc;

let mut generator = WordFilterGenerator::new();
generator.doc(indoc!(
   "foo

    bar baz quux"
));
Source

pub fn generate(&self, identifier: &str) -> String

Generate code defining a WordFilter with the given words, exceptions, separators, aliases, and visibility.

The generated code is most often written to a file at compile time within a build.rs script. An example build.rs is as follows:

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")
        );
}

Trait Implementations§

Source§

impl Clone for WordFilterGenerator

Source§

fn clone(&self) -> WordFilterGenerator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WordFilterGenerator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WordFilterGenerator

Source§

fn default() -> WordFilterGenerator

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.