1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Code generation for the [`word_filter`](https://docs.rs/word_filter) crate.
//!
//! This crate is intended to be used alongside the `word_filter` crate to generate code at compile
//! time. To use, add `word_filter_codegen` 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 both the `word_filter` and `word_filter_codegen` crates to the `Cargo.toml`
//! `[dependencies]` and `[build-dependencies]` lists respectively.
//!
//! ``` toml
//! ...
//! [dependencies]
//! word_filter = "0.7.0"
//!
//! [build-dependencies]
//! word_filter_codegen = "0.7.0"
//! ...
//! ```
//!
//! Next, generate the [`WordFilter`] in the `build.rs` file.
//!
//! ``` no_run
//! 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.
//!
//! ``` ignore
//! include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
//!
//! assert!(FILTER.censor("Should censor foo."), "Should censor ***.");
//! ```
//!
//! [`WordFilter`]: word_filter::WordFilter

#![no_std]

extern crate alloc;

mod pda;
mod state;
mod r#type;

use alloc::{
    borrow::ToOwned,
    collections::VecDeque,
    format,
    string::{String, ToString},
    vec::Vec,
};
use bitflags::bitflags;
use pda::Pda;
use str_overlap::Overlap;

/// Visibility of generated code.
///
/// Can be provided to [`WordFilterGenerator`] to define the visibility of the resulting code.
///
/// # Example
/// The following code example generates a [`WordFilter`] that is visible to the rest of the crate:
///
/// ```
/// use word_filter_codegen::{Visibility, WordFilterGenerator};
///
/// let generated_code = WordFilterGenerator::new()
///     .visibility(Visibility::PubCrate)
///     .word("foo")
///     .generate("FILTER");
/// ```
///
/// [`WordFilter`]: word_filter::WordFilter
#[derive(Clone, Debug)]
pub enum Visibility {
    Private,
    Pub,
    PubCrate,
    PubIn(String),
}

impl Default for Visibility {
    fn default() -> Self {
        Self::Private
    }
}

impl ToString for Visibility {
    fn to_string(&self) -> String {
        match self {
            Visibility::Private => String::new(),
            Visibility::Pub => "pub".to_owned(),
            Visibility::PubCrate => "pub(crate)".to_owned(),
            Visibility::PubIn(path) => format!("pub(in {})", path),
        }
    }
}

bitflags! {
    /// Flags defining separator settings.
    ///
    /// These flags can be passed to a `WordFilterGenerator` to define when separators should be
    /// allowed during matching.
    ///
    /// # Examples
    /// To set separator flags within a `WordFilterGenerator`, simply provide the desired flags
    /// with the `separator_flags` method:
    ///
    /// ```
    /// use word_filter_codegen::{SeparatorFlags, WordFilterGenerator};
    ///
    /// let mut generator = WordFilterGenerator::new();
    ///
    /// generator.separator_flags(SeparatorFlags::BETWEEN_WORDS);
    /// ```
    ///
    /// As these settings are bitflags, they can be combined by `or`ing them together. For example,
    /// to set separators to be allowed between words and exceptions, combine the flags as follows:
    ///
    /// ```
    /// use word_filter_codegen::{SeparatorFlags, WordFilterGenerator};
    ///
    /// let mut generator = WordFilterGenerator::new();
    ///
    /// generator.separator_flags(SeparatorFlags::BETWEEN_WORDS | SeparatorFlags::BETWEEN_EXCEPTIONS);
    /// ```
    ///
    /// Note that a `WordFilter` will default to having `BETWEEN_WORDS` and `BETWEEN_EXCEPTIONS`.
    /// set.
    pub struct SeparatorFlags: u8 {
        /// Allow separators when matching words.
        const BETWEEN_WORDS = 0x0000_0001;
        /// Allow separators when matching exceptions.
        const BETWEEN_EXCEPTIONS = 0x0000_0010;
    }
}

impl Default for SeparatorFlags {
    fn default() -> Self {
        Self::all()
    }
}

/// Code generator for [`WordFilter`]s, 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.
///
/// [`WordFilter`]: word_filter::WordFilter
#[derive(Clone, Debug, Default)]
pub struct WordFilterGenerator {
    words: Vec<String>,
    exceptions: Vec<String>,
    separators: Vec<String>,
    aliases: Vec<(String, String)>,
    visibility: Visibility,
    separator_flags: SeparatorFlags,
    doc: String,
}

impl WordFilterGenerator {
    /// Creates a new WordFilterGenerator.
    ///
    /// This is equivalent to `WordFilterGenerator::default()`.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let generator = WordFilterGenerator::new();
    /// ```
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a single word.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.word("foo");
    /// ```
    #[inline]
    pub fn word<S>(&mut self, word: S) -> &mut Self
    where
        S: ToString,
    {
        self.words.push(word.to_string());
        self
    }

    /// Add words.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.words(&["foo", "bar"]);
    /// ```
    #[inline]
    pub fn words<I, S>(&mut self, words: I) -> &mut Self
    where
        I: IntoIterator<Item = S>,
        S: ToString,
    {
        self.words.extend(words.into_iter().map(|s| s.to_string()));
        self
    }

    /// Add a single exception.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.exception("foo");
    /// ```
    #[inline]
    pub fn exception<S>(&mut self, exception: S) -> &mut Self
    where
        S: ToString,
    {
        self.exceptions.push(exception.to_string());
        self
    }

    /// Add exceptions.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.exceptions(&["foo", "bar"]);
    /// ```
    #[inline]
    pub fn exceptions<I, S>(&mut self, exceptions: I) -> &mut Self
    where
        I: IntoIterator<Item = S>,
        S: ToString,
    {
        self.exceptions
            .extend(exceptions.into_iter().map(|s| s.to_string()));
        self
    }

    /// Add a single separator.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.separator("foo");
    /// ```
    #[inline]
    pub fn separator<S>(&mut self, separator: S) -> &mut Self
    where
        S: ToString,
    {
        self.separators.push(separator.to_string());
        self
    }

    /// Add separators.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.separators(&["foo", "bar"]);
    /// ```
    #[inline]
    pub fn separators<I, S>(&mut self, separators: I) -> &mut Self
    where
        I: IntoIterator<Item = S>,
        S: ToString,
    {
        self.separators
            .extend(separators.into_iter().map(|s| s.to_string()));
        self
    }

    /// Add a single alias.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.alias("foo", "bar");
    /// ```
    #[inline]
    pub fn alias<S, T>(&mut self, source: S, alias: T) -> &mut Self
    where
        S: ToString,
        T: ToString,
    {
        self.aliases.push((source.to_string(), alias.to_string()));
        self
    }

    /// Add aliases.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.aliases(&[("foo", "bar"), ("baz", "qux")]);
    /// ```
    #[inline]
    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,
    {
        self.aliases.extend(
            aliases
                .into_iter()
                .map(|(s, t)| (s.to_string(), t.to_string())),
        );
        self
    }

    /// Set visibility of the generated code.
    ///
    /// # Example
    /// ```
    /// use word_filter_codegen::{Visibility, WordFilterGenerator};
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.visibility(Visibility::Pub);
    /// ```
    #[inline]
    pub fn visibility(&mut self, visibility: Visibility) -> &mut Self {
        self.visibility = visibility;
        self
    }

    #[inline]
    pub fn separator_flags(&mut self, separator_flags: SeparatorFlags) -> &mut Self {
        self.separator_flags = separator_flags;
        self
    }

    /// 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`](https://crates.io/crates/indoc) crate.
    ///
    /// ```
    /// use word_filter_codegen::WordFilterGenerator;
    /// use indoc::indoc;
    ///
    /// let mut generator = WordFilterGenerator::new();
    /// generator.doc(indoc!(
    ///    "foo
    ///
    ///     bar baz quux"
    /// ));
    /// ```
    #[inline]
    pub fn doc<S>(&mut self, doc: S) -> &mut Self
    where
        S: ToString,
    {
        self.doc = doc.to_string();
        self
    }

    /// 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:
    ///
    /// ``` no_run
    /// 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")
    ///         );
    /// }
    /// ```
    ///
    /// [`WordFilter`]: word_filter::WordFilter
    pub fn generate(&self, identifier: &str) -> String {
        let mut pda = Pda::new();

        for word in &self.words {
            pda.add_word(
                word,
                self.separator_flags.contains(SeparatorFlags::BETWEEN_WORDS),
            );
        }
        for exception in &self.exceptions {
            pda.add_exception(
                exception,
                self.separator_flags
                    .contains(SeparatorFlags::BETWEEN_EXCEPTIONS),
            );
        }
        for separator in &self.separators {
            pda.add_separator(separator);
        }

        let mut aliases = self.aliases.clone();
        // Find merged aliases.
        // First, find all aliases that can possibly be combined by a value.
        let mut queue = VecDeque::new();
        for (value, alias) in &self.aliases {
            for (merge_value, _) in &self.aliases {
                let overlap_value = alias.overlap_end(merge_value);
                if overlap_value.is_empty() || overlap_value == *merge_value {
                    continue;
                }
                queue.push_back((
                    value.clone(),
                    unsafe {
                        // SAFETY: `overlap_value` will always be the prefix of `merge_value`.
                        // Therefore, this will never be out of bounds and it will always uphold
                        // `str` invariants.
                        merge_value.get_unchecked(overlap_value.len()..).to_owned()
                    },
                    alias.clone(),
                ));
            }
        }
        // Now, find aliases that complete the combination.
        while let Some((value, target_value, alias)) = queue.pop_front() {
            for (new_value, new_alias) in &self.aliases {
                if target_value == *new_alias || new_alias.starts_with(&target_value) {
                    aliases.push((value.clone() + new_value, alias.clone() + new_alias));
                } else if target_value.starts_with(new_alias) {
                    // If the combination isn't complete, push it to the queue and try again.
                    queue.push_back((
                        value.clone() + new_value,
                        unsafe {
                            // SAFETY: Since `new_alias` is the prefix of `target_value`, this will
                            // never be out of bounds and will always uphold `str` invariants.
                            target_value.get_unchecked(new_alias.len()..).to_owned()
                        },
                        alias.clone() + new_alias,
                    ));
                }
            }
        }

        pda.apply_aliases(
            &aliases,
            self.separator_flags.contains(SeparatorFlags::BETWEEN_WORDS),
            pda::WORD_INDEX,
        );
        pda.apply_aliases(
            &aliases,
            self.separator_flags
                .contains(SeparatorFlags::BETWEEN_EXCEPTIONS),
            pda::EXCEPTION_INDEX,
        );

        pda.minimize();

        format!(
            "#[doc = \"{}\"]\n{} static {}: {} = {};",
            self.doc,
            self.visibility.to_string(),
            identifier,
            pda.to_type(),
            pda.to_definition(identifier)
        )
    }
}