Skip to main content

debian_copyright/
glob.rs

1/// A compiled DEP-5 glob pattern that can efficiently match many paths.
2///
3/// The pattern uses the glob syntax defined by the DEP-5 specification:
4/// `*` matches any sequence of characters, `?` matches a single character,
5/// and backslash escapes `*`, `?` and `\`.
6///
7/// # Examples
8///
9/// ```
10/// let pat = debian_copyright::GlobPattern::new("src/*.rs");
11/// assert!(pat.is_match("src/main.rs"));
12/// assert!(!pat.is_match("lib/main.rs"));
13/// ```
14pub struct GlobPattern(regex::Regex);
15
16impl GlobPattern {
17    /// Compile a DEP-5 glob pattern.
18    pub fn new(pattern: &str) -> Self {
19        Self(glob_to_regex_inner(pattern))
20    }
21
22    /// Check whether a path matches this pattern.
23    pub fn is_match(&self, path: &str) -> bool {
24        self.0.is_match(path)
25    }
26}
27
28/// Convert a glob pattern to a regular expression.
29#[deprecated(since = "0.1.46", note = "use GlobPattern instead")]
30pub fn glob_to_regex(glob: &str) -> regex::Regex {
31    glob_to_regex_inner(glob)
32}
33
34fn glob_to_regex_inner(glob: &str) -> regex::Regex {
35    let mut it = glob.chars();
36    let mut r = "^".to_string();
37
38    while let Some(c) = it.next() {
39        match c {
40            '*' => r.push_str(".*"),
41            '?' => r.push('.'),
42            '\\' => {
43                let c = it.next();
44                match c {
45                    Some('?') | Some('*') | Some('\\') => {
46                        let escaped = regex::escape(&c.unwrap().to_string());
47                        r.push_str(&escaped);
48                    }
49                    Some(x) => {
50                        panic!("invalid escape sequence: \\{}", x);
51                    }
52                    None => {
53                        panic!("invalid escape sequence: \\");
54                    }
55                }
56            }
57            c => {
58                let escaped = regex::escape(&c.to_string());
59                r.push_str(&escaped);
60            }
61        }
62    }
63
64    r.push('$');
65
66    regex::Regex::new(r.as_str()).unwrap()
67}
68
69#[cfg(test)]
70mod tests {
71    #[allow(deprecated)]
72    #[test]
73    fn test_simple() {
74        let r = super::glob_to_regex("*.rs");
75        assert!(r.is_match("foo.rs"));
76        assert!(r.is_match("bar.rs"));
77        assert!(!r.is_match("foo.rs.bak"));
78        assert!(!r.is_match("foo"));
79    }
80
81    #[allow(deprecated)]
82    #[test]
83    fn test_single_char() {
84        let r = super::glob_to_regex("?.rs");
85        assert!(r.is_match("a.rs"));
86        assert!(r.is_match("b.rs"));
87        assert!(!r.is_match("foo.rs"));
88        assert!(!r.is_match("foo"));
89    }
90
91    #[allow(deprecated)]
92    #[test]
93    fn test_escape() {
94        let r = super::glob_to_regex(r"\?.rs");
95        assert!(r.is_match("?.rs"));
96        assert!(!r.is_match("a.rs"));
97        assert!(!r.is_match("b.rs"));
98
99        let r = super::glob_to_regex(r"\*.rs");
100        assert!(r.is_match("*.rs"));
101        assert!(!r.is_match("a.rs"));
102        assert!(!r.is_match("b.rs"));
103
104        let r = super::glob_to_regex(r"\\?.rs");
105        assert!(r.is_match("\\a.rs"));
106        assert!(r.is_match("\\b.rs"));
107        assert!(!r.is_match("a.rs"));
108    }
109
110    #[allow(deprecated)]
111    #[should_panic]
112    #[test]
113    fn test_invalid_escape() {
114        super::glob_to_regex(r"\x.rs");
115    }
116
117    #[allow(deprecated)]
118    #[should_panic]
119    #[test]
120    fn test_invalid_escape2() {
121        super::glob_to_regex(r"\");
122    }
123
124    #[test]
125    fn test_glob_pattern_wildcard() {
126        let pat = super::GlobPattern::new("src/*.rs");
127        assert!(pat.is_match("src/main.rs"));
128        assert!(pat.is_match("src/lib.rs"));
129        assert!(!pat.is_match("lib/main.rs"));
130        assert!(!pat.is_match("src/main.rs.bak"));
131    }
132
133    #[test]
134    fn test_glob_pattern_deep_wildcard() {
135        let pat = super::GlobPattern::new("src/*");
136        assert!(pat.is_match("src/foo"));
137        assert!(pat.is_match("src/foo/bar.rs"));
138        assert!(!pat.is_match("lib/foo"));
139    }
140
141    #[test]
142    fn test_glob_pattern_question_mark() {
143        let pat = super::GlobPattern::new("file?.txt");
144        assert!(pat.is_match("file1.txt"));
145        assert!(pat.is_match("fileA.txt"));
146        assert!(!pat.is_match("file10.txt"));
147        assert!(!pat.is_match("file.txt"));
148    }
149
150    #[test]
151    fn test_glob_pattern_literal() {
152        let pat = super::GlobPattern::new("LICENSE");
153        assert!(pat.is_match("LICENSE"));
154        assert!(!pat.is_match("LICENSE.md"));
155        assert!(!pat.is_match("NOLICENSE"));
156    }
157
158    #[test]
159    fn test_glob_pattern_escaped_star() {
160        let pat = super::GlobPattern::new(r"\*.txt");
161        assert!(pat.is_match("*.txt"));
162        assert!(!pat.is_match("foo.txt"));
163    }
164
165    #[test]
166    fn test_glob_pattern_escaped_question() {
167        let pat = super::GlobPattern::new(r"\?.txt");
168        assert!(pat.is_match("?.txt"));
169        assert!(!pat.is_match("a.txt"));
170    }
171
172    #[test]
173    fn test_glob_pattern_escaped_backslash() {
174        let pat = super::GlobPattern::new(r"\\foo");
175        assert!(pat.is_match(r"\foo"));
176        assert!(!pat.is_match("foo"));
177    }
178
179    #[should_panic]
180    #[test]
181    fn test_glob_pattern_invalid_escape() {
182        super::GlobPattern::new(r"\x");
183    }
184
185    #[should_panic]
186    #[test]
187    fn test_glob_pattern_trailing_backslash() {
188        super::GlobPattern::new(r"\");
189    }
190
191    #[test]
192    fn test_glob_pattern_regex_special_chars() {
193        let pat = super::GlobPattern::new("file(1).txt");
194        assert!(pat.is_match("file(1).txt"));
195        assert!(!pat.is_match("file1.txt"));
196    }
197}