unicount_lib/
lib.rs

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
#[doc = include_str!("../README.md")]
use {clap::ValueEnum, unicode_segmentation::UnicodeSegmentation};

fn split(s: &str) -> Vec<String> {
    UnicodeSegmentation::graphemes(s, true)
        .map(|x| x.to_string())
        .collect()
}

pub struct Counter {
    current: usize,
    alphabet: Vec<String>,
}

impl Counter {
    pub fn new(alphabet: &str, start: usize) -> Counter {
        Counter {
            current: start,
            alphabet: split(alphabet),
        }
    }

    fn get(&self, x: usize) -> String {
        let length = self.alphabet.len();
        let d = x / length;
        let r = x % length;
        let t = self.alphabet[r].clone();
        if d == 0 {
            t
        } else {
            format!("{}{t}", self.get(d - 1))
        }
    }
}

impl std::iter::Iterator for Counter {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let r = self.to_string();
        self.current += 1;
        Some(r)
    }
}

impl std::fmt::Display for Counter {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.get(self.current))
    }
}

#[derive(Clone, ValueEnum)]
pub enum Kind {
    EnglishUpper,
    EnglishLower,
}

impl Kind {
    pub fn counter(&self, start: usize) -> Counter {
        match self {
            Kind::EnglishUpper => Counter::new("ABCDEFGHIJKLMNOPQRSTUVWXYZ", start),
            Kind::EnglishLower => Counter::new("abcdefghijklmnopqrstuvwxyz", start),
        }
    }
}