meme_id/schemes/
mod.rs

1pub mod adjective_noun;
2pub mod complex_phrase;
3pub mod phrase;
4pub mod punk;
5pub mod simple_phrase;
6
7use core::{fmt, iter::Peekable};
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum Error<'a> {
11    NotEnoughWords { expected: usize, actual: usize },
12    TrailingWords,
13    Unrecognized { word: &'a str },
14}
15impl fmt::Display for Error<'_> {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Error::NotEnoughWords { expected, actual } => {
19                write!(
20                    f,
21                    "Not enough words. Expected {}, actual {}",
22                    expected, actual
23                )
24            }
25            Error::TrailingWords => {
26                write!(f, "Words left after parsing")
27            }
28            Error::Unrecognized { word } => {
29                write!(f, "Word '{}' unrecognized", word)
30            }
31        }
32    }
33}
34
35fn skip_one_of<'a>(iter: &mut Peekable<impl Iterator<Item = &'a str>>, skip: &[&str]) {
36    iter.next_if(|word| {
37        skip.iter()
38            .any(|skip| str::eq_ignore_ascii_case(word, skip))
39    });
40}
41
42fn string_to_words<'a>(s: &'a str) -> Peekable<impl Iterator<Item = &'a str>> {
43    s.split(|ch: char| !ch.is_ascii_alphabetic())
44        .filter(|s| !s.is_empty() && !s.contains(|ch: char| !ch.is_ascii_alphabetic()))
45        .peekable()
46}
47
48/// Wrapper that changes `Display` behavior of the scheme.
49/// Making it emit all words in one line with hyphen between them.
50/// Without auxiliary words.
51pub struct Hyphenated<T>(pub T);