Struct word_filter::WordFilter[][src]

pub struct WordFilter<'a, const N: usize> { /* fields omitted */ }
Expand description

A word filter for identifying filtered words within strings.

A WordFilter is constructed from filtered words, exceptions, separators, and aliases. Each of those parameters are defined as follows:

  • filtered words - strings that should be identified and censored by the WordFilter.
  • exceptions - strings that should explicitly not be identified and censored by the WordFilter. Any string that contains filtered word that is contained entirely inside an exception will be ignored.
  • separators - strings that may appear between characters in filtered words and exceptions.
  • aliases - tuples defining alias strings that may replace source strings during matching. These are of the form (<source string>, <alias string>).

WordFilters are constructed at compile-time using the codegen module. See crate-level documentation for further information.

Implementations

Find all filtered words matched by input.

Returns an Iterator over all matched filtered words.

Example

Assuming a compile-time constructed WordFilter FILTER, defined in a build.rs as:

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()
            .word("foo")
            .generate("FILTER")
        );
}

this method is used as follows:

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert_eq!(FILTER.find("this string contains foo").collect::<Vec<_>>(), vec!["foo"]);

Find all raw string slices matched in input.

Returns an iterator over all matched slices in input.

/// # Example

Assuming a compile-time constructed WordFilter FILTER, defined in a build.rs as:

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()
            .word("foo")
            .alias('o', 'a')
            .generate("FILTER")
        );
}

this method is used as follows:

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert_eq!(FILTER.find_raw("this string contains fao").collect::<Vec<_>>(), vec!["fao"]);

Check whether input contains any filtered words.

Returns true if matches are found, and false otherwise.

Example

Assuming a compile-time constructed WordFilter FILTER, defined in a build.rs as:

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()
            .word("foo")
            .generate("FILTER")
        );
}

this method is used as follows:

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert!(FILTER.check("this string contains foo"));

Censor all filtered words within input, replacing their occurances with the output of censor.

Returns a newly-allocated String with all filtered words censored.

Example

Assuming a compile-time constructed WordFilter FILTER, defined in a build.rs as:

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()
            .word("foo")
            .generate("FILTER")
        );
}

this method is used as follows:

use word_filter::censor;

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert!(
    FILTER.censor_with("this string contains foo", censor::replace_graphemes_with!("#")),
    "this string contains ###"
);

Censor all filtered words within input with a default censor of censor::replace_graphemes_with("*").

Returns a newly-allocated String with all filtered words censored.

Example

Assuming a compile-time constructed WordFilter FILTER, defined in a build.rs as:

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()
            .word("foo")
            .generate("FILTER")
        );
}

this method is used as follows:

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert!(FILTER.censor("this string contains foo"), "this string contains ***");

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.