-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.rs
More file actions
220 lines (197 loc) · 6.15 KB
/
Copy pathpattern.rs
File metadata and controls
220 lines (197 loc) · 6.15 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::MatchSettings;
use crate::compile::compile_component;
use crate::error::Error;
use crate::spec::{DirType, FSEntry, FSPattern, FileOrDirType, FileType};
use fspec_placeholder::parse_component;
pub(crate) fn parse_pattern_str(
raw: &str,
line: usize,
settings: &MatchSettings,
) -> Result<FSPattern, Error> {
let s0 = raw.trim();
if s0.is_empty() {
return Err(parse_err(line, 1, "empty pattern"));
}
// Anchored vs unanchored.
// Support both '/' and './' as anchored prefixes
let (anchored, mut s, base_col) = if let Some(rest) = s0.strip_prefix("./") {
(true, rest, 3) // we consumed './'
} else if let Some(rest) = s0.strip_prefix('/') {
(true, rest, 2) // we consumed '/'
} else {
(false, s0, 1)
};
// Directory vs file is determined by trailing slash.
let ends_with_slash = s.ends_with('/');
if ends_with_slash {
// Strip exactly one trailing slash; anything else will be handled by empty-segment checks.
s = &s[..s.len() - 1];
}
if s.is_empty() {
return Err(parse_err(
line,
base_col,
"pattern must not be just '/' (no path components)",
));
}
// Split into segments. We disallow empty segments like `a//b`.
let parts: Vec<&str> = s.split('/').collect();
for (i, part) in parts.iter().enumerate() {
if part.is_empty() {
// Column: approximate start of this empty segment.
// (Good enough; you can refine later if you want exact columns.)
let col = base_col + parts[..i].iter().map(|p| p.len() + 1).sum::<usize>();
return Err(parse_err(line, col, "empty path segment (// not allowed)"));
}
}
let last_idx = parts.len() - 1;
let mut entries = Vec::with_capacity(parts.len());
for (i, part) in parts.iter().enumerate() {
let is_last = i == last_idx;
if !is_last {
entries.push(FSEntry::Dir(parse_dir(part)?));
continue;
}
// Final component depends on trailing slash.
if ends_with_slash {
entries.push(FSEntry::Dir(parse_dir(part)?));
} else if settings.allow_file_or_dir_leaf {
entries.push(FSEntry::Either(parse_file_or_dir(part)?));
} else {
entries.push(FSEntry::File(parse_file(part)?));
}
}
Ok(if anchored {
FSPattern::Anchored(entries)
} else {
FSPattern::Unanchored(entries)
})
}
fn parse_dir(s: &str) -> Result<DirType, Error> {
match s {
"*" => Ok(DirType::Star),
"**" => Ok(DirType::DoubleStar),
_ => {
let component = parse_component(s)?;
let compiled = compile_component(&component)?;
Ok(DirType::Component(compiled))
}
}
}
fn parse_file(s: &str) -> Result<FileType, Error> {
match s {
"*" => Ok(FileType::Star),
_ => {
let component = parse_component(s)?;
let compiled = compile_component(&component)?;
Ok(FileType::Component(compiled))
}
}
}
fn parse_file_or_dir(s: &str) -> Result<FileOrDirType, Error> {
match s {
"*" => Ok(FileOrDirType::Star),
_ => {
let component = parse_component(s)?;
let compiled = compile_component(&component)?;
Ok(FileOrDirType::Component(compiled))
}
}
}
fn parse_err(line: usize, col: usize, msg: impl Into<String>) -> Error {
Error::Parse {
line,
col,
msg: msg.into(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::spec::FSPattern::*;
#[test]
fn unanchored_dir_then_entry() {
let p = parse_pattern_str("assets/*/*.png", 1, &MatchSettings::default()).unwrap();
match p {
Unanchored(entries) => {
assert_eq!(entries.len(), 3);
// Just verify structure, not exact equality since we can't compare CompiledComponent
}
_ => panic!("expected Unanchored"),
}
}
#[test]
fn trailing_slash_makes_last_component_dir() {
let p = parse_pattern_str("assets/*/", 1, &MatchSettings::default()).unwrap();
match p {
Unanchored(entries) => {
assert_eq!(entries.len(), 2);
}
_ => panic!("expected Unanchored"),
}
}
#[test]
fn anchored_pattern() {
let p = parse_pattern_str("/assets/**/x", 1, &MatchSettings::default()).unwrap();
match p {
Anchored(entries) => {
assert_eq!(entries.len(), 3);
}
_ => panic!("expected Anchored"),
}
}
#[test]
fn anchored_pattern_with_dot_slash() {
let p = parse_pattern_str("./assets/**/x", 1, &MatchSettings::default()).unwrap();
match p {
Anchored(entries) => {
assert_eq!(entries.len(), 3);
}
_ => panic!("expected Anchored"),
}
}
#[test]
fn anchored_dir_with_dot_slash() {
let p = parse_pattern_str("./bin/", 1, &MatchSettings::default()).unwrap();
match p {
Anchored(entries) => {
assert_eq!(entries.len(), 1);
}
_ => panic!("expected Anchored"),
}
}
#[test]
fn rejects_double_slash() {
assert!(parse_pattern_str("a//b", 1, &MatchSettings::default()).is_err());
}
#[test]
fn spaces_in_dir_literal() {
let p = parse_pattern_str(
"/assets/this dir has spaces /x",
1,
&MatchSettings::default(),
)
.unwrap();
match p {
Anchored(entries) => {
assert_eq!(entries.len(), 3);
}
_ => panic!("expected Anchored"),
}
}
#[test]
fn spaces_in_file_literal() {
let p = parse_pattern_str(
"/assets/approved/My mom named this file.png",
1,
&MatchSettings::default(),
)
.unwrap();
match p {
Anchored(entries) => {
assert_eq!(entries.len(), 3);
}
_ => panic!("expected Anchored"),
}
}
}