Anders429 / word_filter

A Word Filter for filtering text.
Apache License 2.0
1 stars 0 forks source link

Replace Type enum with bitflags #67

Closed Anders429 closed 3 years ago

Anders429 commented 3 years ago

The Type enum can be replaced with a bitflag struct (using the bitflags crate). This will remove the need for some values, such as SeparatorReturn, which is strictly a combination of the Separator and Return variants. Additionally, some other State attributes could be added to a bitflags struct, simplifying things quite a bit.

I think the struct definition would look something like this:

bitflags! {
    struct Type: u8 {
        const WORD = 0b0000_0001;
        const EXCEPTION = 0b0000_0010;
        const SEPARATOR = 0b0000_0100;
        const RETURN = 0b0000_1000;
        const INTO_REPETITION = 0b0001_0000;
        const TAKE_REPETITION = 0b0010_0000;
        const INTO_SEPARATOR = 0b0100_0000;
    }
}

The actual str currently stored in the Word variant will need to be stored as an attribute within the State instead. Without having the store the str directly, we could even combine the WORD and EXCEPTION flags into a single MATCH flag and use the presence of the str to decide whether it is a word or exception match. This isn't strictly necessary, since we have enough bits to have the two options be distinct, and having the two states be distinct will make matching easier.