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
use {
    regex::Regex,
    regex_syntax::escape_into,
    std::{
        convert::Infallible,
        fmt::{Display, Formatter, Result as FmtResult},
        hash::{Hash, Hasher},
        str::FromStr,
    },
};

/// A glob-style pattern matcher used in various Aspen policy elements.
#[derive(Debug, Clone)]
pub enum GlobPattern {
    /// Empty match.
    Empty,

    /// Wildcard match.
    Any,

    /// Exact string match.
    Exact(Box<String>),

    /// StartsWith is a simple prefix match.
    StartsWith(Box<String>),

    /// Regex pattern contains the original Arn glob-like pattern followed by the compiled regex.
    Regex(Box<(String, Regex)>),
}

impl Display for GlobPattern {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            GlobPattern::Empty => Ok(()),
            GlobPattern::Any => write!(f, "*"),
            GlobPattern::Exact(s) => f.write_str(s),
            GlobPattern::StartsWith(s) => write!(f, "{}*", s),
            GlobPattern::Regex(sr) => f.write_str(sr.0.as_str()),
        }
    }
}

impl PartialEq for GlobPattern {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Empty, Self::Empty) => true,
            (Self::Any, Self::Any) => true,
            (Self::Exact(a), Self::Exact(b)) => a == b,
            (Self::StartsWith(a), Self::StartsWith(b)) => a == b,
            (Self::Regex(sr_a), Self::Regex(sr_b)) => sr_a.0 == sr_b.0,
            _ => false,
        }
    }
}

impl Eq for GlobPattern {}

impl Hash for GlobPattern {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        match self {
            Self::Empty => hasher.write_u8(0),
            Self::Any => hasher.write_u8(1),
            Self::Exact(s) => {
                hasher.write_u8(2);
                s.hash(hasher);
            }
            Self::StartsWith(s) => {
                hasher.write_u8(3);
                s.hash(hasher);
            }
            Self::Regex(sr) => {
                hasher.write_u8(4);
                sr.0.hash(hasher);
            }
        }
    }
}

impl<T: AsRef<str>> From<T> for GlobPattern {
    fn from(s: T) -> Self {
        Self::new(s.as_ref())
    }
}

impl FromStr for GlobPattern {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Infallible> {
        Ok(Self::new(s))
    }
}

impl GlobPattern {
    /// Indicate whether the specified string from an [Arn] segment matches this pattern.
    pub fn matches(&self, segment: &str) -> bool {
        match self {
            Self::Empty => segment.is_empty(),
            Self::Any => true,
            Self::Exact(value) => segment == value.as_str(),
            Self::StartsWith(prefix) => segment.starts_with(prefix.as_str()),
            Self::Regex(sr) => sr.1.is_match(segment),
        }
    }

    /// Create a new [ArnSegmentPattern] from a string.
    pub fn new(s: &str) -> Self {
        if s.is_empty() {
            return GlobPattern::Empty;
        }

        if s == "*" {
            return GlobPattern::Any;
        }

        let mut regex_pattern = String::with_capacity(s.len() + 2);
        let mut must_use_regex = false;
        let mut wildcard_seen = false;

        regex_pattern.push('^');

        for c in s.chars() {
            match c {
                '*' => {
                    wildcard_seen = true;
                    regex_pattern.push_str(".*");
                }

                '?' => {
                    must_use_regex = true;
                    regex_pattern.push('.');
                }

                _ => {
                    // Escape any special Regex characters
                    let c_s = c.to_string();
                    escape_into(c_s.as_str(), &mut regex_pattern);

                    if wildcard_seen {
                        must_use_regex = true;
                        wildcard_seen = false;
                    }
                }
            }
        }

        if must_use_regex {
            regex_pattern.push('$');
            Self::Regex(Box::new((
                s.to_string(),
                Regex::new(regex_pattern.as_str()).expect("Regex should always compile"),
            )))
        } else if wildcard_seen {
            // If we saw a wildcard but didn't need to use a regex, then the wildcard was at the end
            Self::StartsWith(Box::new(s[..s.len() - 1].to_string()))
        } else {
            Self::Exact(Box::new(s.to_string()))
        }
    }
}