const_format/__str_methods/
pattern.rs

1use super::AsciiByte;
2
3pub(crate) struct PatternCtor<T>(pub(crate) T);
4
5impl PatternCtor<u8> {
6    pub(crate) const fn conv(self) -> Pattern {
7        Pattern::AsciiByte(AsciiByte::new(self.0))
8    }
9}
10
11impl PatternCtor<&'static str> {
12    pub(crate) const fn conv(self) -> Pattern {
13        if let [b @ 0..=127] = *self.0.as_bytes() {
14            Pattern::AsciiByte(AsciiByte::new(b))
15        } else {
16            Pattern::Str(self.0)
17        }
18    }
19}
20
21impl PatternCtor<char> {
22    pub(crate) const fn conv(self) -> Pattern {
23        let code = self.0 as u32;
24        if let c @ 0..=127 = code {
25            Pattern::AsciiByte(AsciiByte::new(c as u8))
26        } else {
27            Pattern::Char(crate::char_encoding::char_to_display(self.0))
28        }
29    }
30}
31
32#[derive(Copy, Clone)]
33pub(crate) enum Pattern {
34    AsciiByte(AsciiByte),
35    Str(&'static str),
36    Char(crate::char_encoding::FmtChar),
37}
38
39pub(crate) enum PatternNorm<'a> {
40    AsciiByte(AsciiByte),
41    Str(&'a [u8]),
42}
43
44impl Pattern {
45    pub(crate) const fn normalize(&self) -> PatternNorm<'_> {
46        match self {
47            Pattern::AsciiByte(ab) => PatternNorm::AsciiByte(*ab),
48            Pattern::Str(str) => PatternNorm::Str(str.as_bytes()),
49            Pattern::Char(char) => PatternNorm::Str(char.as_bytes()),
50        }
51    }
52}