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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Syntax validation for navigation guides
use crate::errors::{Result, SyntaxError};
use crate::types::{FilesystemItem, NavigationGuide, NavigationGuideLine};
use std::collections::HashSet;
/// Validator for navigation guide syntax
pub struct Validator;
impl Validator {
/// Create a new validator instance
pub fn new() -> Self {
Self
}
/// Validate the syntax of a navigation guide
pub fn validate_syntax(&self, guide: &NavigationGuide) -> Result<()> {
// Check for empty guide
if guide.items.is_empty() {
return Err(SyntaxError::EmptyGuideBlock.into());
}
// Validate each item
for item in &guide.items {
self.validate_item(item)?;
}
// Validate indentation consistency
self.validate_indentation(&guide.items)?;
Ok(())
}
/// Validate a single navigation guide item
fn validate_item(&self, item: &NavigationGuideLine) -> Result<()> {
match &item.item {
FilesystemItem::Placeholder { .. } => {
// Placeholders don't need path validation
// They will have additional validation in validate_placeholders
}
_ => {
// Validate path characters for non-placeholder items
self.validate_path_characters(item)?;
}
}
match &item.item {
FilesystemItem::Directory { path, children, .. } => {
// Directory paths should not contain the trailing slash in our internal representation
// (it's stripped during parsing, but this is a double-check)
if path.ends_with('/') {
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.clone(),
}
.into());
}
// Validate children recursively
for child in children {
self.validate_item(child)?;
}
// Check placeholder-specific rules for children
self.validate_placeholder_rules(children)?;
}
FilesystemItem::File { path, .. } | FilesystemItem::Symlink { path, .. } => {
// Files and symlinks should not end with slash
if path.ends_with('/') {
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.clone(),
}
.into());
}
}
FilesystemItem::Placeholder { .. } => {
// Placeholder-specific validation is done at the parent level
}
}
Ok(())
}
/// Validate path characters
fn validate_path_characters(&self, item: &NavigationGuideLine) -> Result<()> {
let path = item.path();
// Check for empty path
if path.is_empty() {
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.to_string(),
}
.into());
}
// Check for invalid characters
// Allow alphanumeric, dash, underscore, dot, and forward slash
for ch in path.chars() {
if !ch.is_alphanumeric()
&& !matches!(
ch,
'-' | '_'
| '.'
| '/'
| ' '
| '('
| ')'
| '['
| ']'
| '{'
| '}'
| '@'
| '+'
| '~'
| ','
)
{
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.to_string(),
}
.into());
}
}
// Check for double slashes
if path.contains("//") {
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.to_string(),
}
.into());
}
// Check for paths starting or ending with slash (should have been handled in parsing)
if path.starts_with('/') || path.ends_with('/') {
return Err(SyntaxError::InvalidPathFormat {
line: item.line_number,
path: path.to_string(),
}
.into());
}
Ok(())
}
/// Validate indentation consistency across items
fn validate_indentation(&self, items: &[NavigationGuideLine]) -> Result<()> {
if items.is_empty() {
return Ok(());
}
// Collect all unique indent levels
let mut indent_levels: HashSet<usize> = HashSet::new();
self.collect_indent_levels(items, &mut indent_levels);
// Check that all indentation levels are consistent
// First, find the base indentation unit (smallest non-zero indent)
let base_indent = indent_levels
.iter()
.filter(|&&level| level > 0)
.min()
.copied();
if let Some(base) = base_indent {
// All indent levels should be multiples of the base
for &level in &indent_levels {
if level > 0 && level % base != 0 {
// Find the first item with this indent level to report the error
if let Some(item) = self.find_item_with_indent(items, level) {
return Err(SyntaxError::InconsistentIndentation {
line: item.line_number,
expected: ((level / base) + 1) * base,
found: level,
}
.into());
}
}
}
}
// Validate proper nesting (no skipping levels)
self.validate_nesting(items)?;
// Validate placeholder rules at root level
self.validate_placeholder_rules(items)?;
Ok(())
}
/// Validate placeholder-specific rules
fn validate_placeholder_rules(&self, items: &[NavigationGuideLine]) -> Result<()> {
// Check that placeholders are not adjacent
for i in 0..items.len() {
if items[i].is_placeholder() {
// Check if next item is also a placeholder
if i + 1 < items.len() && items[i + 1].is_placeholder() {
return Err(SyntaxError::AdjacentPlaceholders {
line: items[i + 1].line_number,
}
.into());
}
// Placeholders cannot have children (this should be enforced by parser)
if items[i].children().is_some() && !items[i].children().unwrap().is_empty() {
return Err(SyntaxError::PlaceholderWithChildren {
line: items[i].line_number,
}
.into());
}
}
}
Ok(())
}
/// Collect all indent levels from items and their children
fn collect_indent_levels(&self, items: &[NavigationGuideLine], levels: &mut HashSet<usize>) {
for item in items {
levels.insert(item.indent_level);
if let Some(children) = item.children() {
self.collect_indent_levels(children, levels);
}
}
}
/// Find the first item with the given indent level
fn find_item_with_indent<'a>(
&self,
items: &'a [NavigationGuideLine],
target_level: usize,
) -> Option<&'a NavigationGuideLine> {
for item in items {
if item.indent_level == target_level {
return Some(item);
}
if let Some(children) = item.children() {
if let Some(found) = self.find_item_with_indent(children, target_level) {
return Some(found);
}
}
}
None
}
/// Validate that indentation levels don't skip (e.g., 0 -> 2 without 1)
fn validate_nesting(&self, items: &[NavigationGuideLine]) -> Result<()> {
for item in items {
if let Some(children) = item.children() {
for child in children {
// Children should be exactly one level deeper than parent
if child.indent_level != item.indent_level + 1 {
return Err(SyntaxError::InvalidIndentationLevel {
line: child.line_number,
}
.into());
}
// Recursively check children
self.validate_nesting(children)?;
}
}
}
Ok(())
}
}
impl Default for Validator {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_empty_guide() {
let guide = NavigationGuide::new();
let validator = Validator::new();
let result = validator.validate_syntax(&guide);
assert!(matches!(
result,
Err(crate::errors::AppError::Syntax(
SyntaxError::EmptyGuideBlock
))
));
}
#[test]
fn test_validate_invalid_path_characters() {
let mut guide = NavigationGuide::new();
guide.items.push(NavigationGuideLine {
line_number: 1,
indent_level: 0,
item: FilesystemItem::File {
path: "file|with|pipes.txt".to_string(),
comment: None,
},
});
let validator = Validator::new();
let result = validator.validate_syntax(&guide);
assert!(matches!(
result,
Err(crate::errors::AppError::Syntax(
SyntaxError::InvalidPathFormat { .. }
))
));
}
#[test]
fn test_validate_double_slashes() {
let mut guide = NavigationGuide::new();
guide.items.push(NavigationGuideLine {
line_number: 1,
indent_level: 0,
item: FilesystemItem::File {
path: "path//with//double//slashes.txt".to_string(),
comment: None,
},
});
let validator = Validator::new();
let result = validator.validate_syntax(&guide);
assert!(matches!(
result,
Err(crate::errors::AppError::Syntax(
SyntaxError::InvalidPathFormat { .. }
))
));
}
#[test]
fn test_validate_adjacent_placeholders() {
let mut guide = NavigationGuide::new();
guide.items.push(NavigationGuideLine {
line_number: 1,
indent_level: 0,
item: FilesystemItem::Directory {
path: "src".to_string(),
comment: None,
children: vec![
NavigationGuideLine {
line_number: 2,
indent_level: 1,
item: FilesystemItem::Placeholder {
comment: Some("first placeholder".to_string()),
},
},
NavigationGuideLine {
line_number: 3,
indent_level: 1,
item: FilesystemItem::Placeholder {
comment: Some("second placeholder".to_string()),
},
},
],
},
});
let validator = Validator::new();
let result = validator.validate_syntax(&guide);
assert!(matches!(
result,
Err(crate::errors::AppError::Syntax(
SyntaxError::AdjacentPlaceholders { line: 3 }
))
));
}
#[test]
fn test_validate_non_adjacent_placeholders() {
let mut guide = NavigationGuide::new();
guide.items.push(NavigationGuideLine {
line_number: 1,
indent_level: 0,
item: FilesystemItem::Directory {
path: "src".to_string(),
comment: None,
children: vec![
NavigationGuideLine {
line_number: 2,
indent_level: 1,
item: FilesystemItem::Placeholder {
comment: Some("first placeholder".to_string()),
},
},
NavigationGuideLine {
line_number: 3,
indent_level: 1,
item: FilesystemItem::File {
path: "main.rs".to_string(),
comment: None,
},
},
NavigationGuideLine {
line_number: 4,
indent_level: 1,
item: FilesystemItem::Placeholder {
comment: Some("second placeholder".to_string()),
},
},
],
},
});
let validator = Validator::new();
let result = validator.validate_syntax(&guide);
assert!(result.is_ok());
}
}