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
use crate::{
    chr,
    polymorphism::{HasTypeWitness, MakeTypeWitness, TypeEq, TypeWitnessTypeArg},
};

/// A string pattern.
///
/// Types that implement this trait can be used to search into a string.
///
/// This trait can only be implemented in the `konst` crate.
///
pub trait Pattern<'a>: HasTypeWitness<PatternInput<'a, Self>> + Copy + Sized {}

macro_rules! declare_patterns {
    ($((
        $variant:ident, $ty:ty, $normalized:ty, $index:expr,
        |$param:tt| $normalizer:expr
    ),)*) => (
        pub struct PatternInput<'a, P: Pattern<'a>>(PatternInputInner<'a, P>);

        enum PatternInputInner<'a, P: Pattern<'a>> {
            $(
                $variant {te: TypeEq<P, $ty>},
            )*
        }

        impl<'a, Arg> TypeWitnessTypeArg for PatternInput<'a, Arg>
        where
            Arg: Pattern<'a>
        {
            type Arg = Arg;
        }

        $(
            impl<'a> MakeTypeWitness for PatternInput<'a, $ty> {
                const MAKE: Self =
                    PatternInput(PatternInputInner::$variant{
                        te: TypeEq::NEW,
                    });
            }

            impl<'a> Pattern<'a> for $ty {}
        )*

        #[derive(Copy, Clone)]
        pub(crate) enum PatternNorm<'a, P: Pattern<'a>> {
            $(
                $variant{
                    val: $normalized,
                    te: TypeEq<P, $ty>,
                },
            )*
        }

        impl<'a, P: Pattern<'a>> PatternNorm<'a, P> {
            pub(crate) const fn new(pattern: P) -> Self
            where
                P: Pattern<'a>
            {
                match P::WITNESS.0 {
                    $(
                        PatternInputInner::$variant{te} => {
                            let $param = te.to_right(pattern);
                            PatternNorm::$variant{val: $normalizer, te}
                        }
                    )*
                }
            }

            pub(crate) const fn as_str(&self) -> &str {
                match self {
                    PatternNorm::Str{val, te} => te.reachability_hint(*val),
                    PatternNorm::Char{val, te} => te.reachability_hint(val.as_str()),
                }
            }

            pub(crate) const fn as_bytes(&self) -> &[u8] {
                match self {
                    PatternNorm::Str{val, te} => te.reachability_hint(val.as_bytes()),
                    PatternNorm::Char{val, te} => te.reachability_hint(val.as_bytes()),
                }
            }
        }
    )
}

declare_patterns! {
    (Str, &'a str, &'a str, 0, |x| x),
    (Char, char, chr::Utf8Encoded, 1, |char| chr::encode_utf8(char)),
}