rust-guardian 0.1.1

Dynamic code quality enforcement preventing incomplete or placeholder code
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Path filtering using .gitignore-style patterns
//!
//! Architectural Principle: Service Layer - PathFilter orchestrates complex path matching logic
//! - Encapsulates the rules for include/exclude pattern evaluation
//! - Provides clean interface for determining whether a path should be analyzed
//! - Handles .guardianignore file discovery and parsing

use crate::domain::violations::{GuardianError, GuardianResult};
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// Manages path filtering using .gitignore-style patterns
#[derive(Debug, Clone)]
pub struct PathFilter {
    /// Include/exclude patterns
    patterns: Vec<FilterPattern>,
    /// Whether to process .guardianignore files
    process_ignore_files: bool,
    /// Name of ignore files to process
    ignore_filename: String,
}

/// A single path filter pattern
#[derive(Debug, Clone)]
struct FilterPattern {
    /// The glob pattern
    pattern: glob::Pattern,
    /// Whether this is an include pattern (starts with !)
    is_include: bool,
    /// Original pattern string for debugging
    original: String,
}

impl PathFilter {
    /// Create a new path filter with the given patterns
    pub fn new(patterns: Vec<String>, ignore_filename: Option<String>) -> GuardianResult<Self> {
        let mut filter_patterns = Vec::new();

        for pattern_str in patterns {
            let (is_include, pattern_str) = if let Some(stripped) = pattern_str.strip_prefix('!') {
                (true, stripped.to_string())
            } else {
                (false, pattern_str)
            };

            let pattern = glob::Pattern::new(&pattern_str).map_err(|e| {
                GuardianError::pattern(format!("Invalid pattern '{pattern_str}': {e}"))
            })?;

            filter_patterns.push(FilterPattern {
                pattern,
                is_include,
                original: pattern_str,
            });
        }

        Ok(Self {
            patterns: filter_patterns,
            process_ignore_files: ignore_filename.is_some(),
            ignore_filename: ignore_filename.unwrap_or_else(|| ".guardianignore".to_string()),
        })
    }

    /// Create a default path filter with sensible exclusions
    pub fn with_defaults() -> GuardianResult<Self> {
        Self::new(
            vec![
                // Exclude common build/cache directories
                "target/**".to_string(),
                "**/node_modules/**".to_string(),
                "**/.git/**".to_string(),
                "**/*.generated.*".to_string(),
                "**/dist/**".to_string(),
                "**/build/**".to_string(),
            ],
            Some(".guardianignore".to_string()),
        )
    }

    /// Check if a file should be analyzed based on all patterns and ignore files
    pub fn should_analyze<P: AsRef<Path>>(&self, path: P) -> GuardianResult<bool> {
        let path = path.as_ref();
        let _path_str = path.to_string_lossy();

        // Start with default: include all files
        let mut should_include = true;

        // Apply patterns in order (like .gitignore)
        for pattern in &self.patterns {
            let matches = self.pattern_matches_path(pattern, path);

            if matches {
                should_include = pattern.is_include;
            }
        }

        // If excluded by configured patterns, return false
        if !should_include {
            return Ok(false);
        }

        // Check .guardianignore files if enabled
        if self.process_ignore_files {
            let ignored_by_files = self.is_ignored_by_files(path)?;
            if ignored_by_files {
                return Ok(false);
            }
        }

        Ok(true)
    }

    /// Check if path is ignored by .guardianignore files
    fn is_ignored_by_files<P: AsRef<Path>>(&self, path: P) -> GuardianResult<bool> {
        let path = path.as_ref();
        let mut current_dir = path.parent();
        let mut is_ignored = false;

        // Walk up the directory tree looking for .guardianignore files
        while let Some(dir) = current_dir {
            let ignore_file = dir.join(&self.ignore_filename);

            if ignore_file.exists() {
                let patterns = self.load_ignore_file(&ignore_file)?;

                // Check if any pattern in this file matches
                for pattern in patterns {
                    // Make path relative to the ignore file's directory
                    if let Ok(relative_path) = path.strip_prefix(dir) {
                        let matches = self.pattern_matches_path(&pattern, relative_path);

                        if matches {
                            is_ignored = !pattern.is_include;
                        }
                    }
                }
            }

            current_dir = dir.parent();
        }

        Ok(is_ignored)
    }

    /// Load patterns from a .guardianignore file
    fn load_ignore_file<P: AsRef<Path>>(&self, path: P) -> GuardianResult<Vec<FilterPattern>> {
        let content = fs::read_to_string(&path).map_err(|e| {
            GuardianError::config(format!(
                "Failed to read ignore file '{}': {}",
                path.as_ref().display(),
                e
            ))
        })?;

        let mut patterns = Vec::new();

        for line in content.lines() {
            let line = line.trim();

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            let (is_include, pattern_str) = if let Some(stripped) = line.strip_prefix('!') {
                (true, stripped.to_string())
            } else {
                (false, line.to_string())
            };

            match glob::Pattern::new(&pattern_str) {
                Ok(pattern) => {
                    patterns.push(FilterPattern {
                        pattern,
                        is_include,
                        original: pattern_str,
                    });
                }
                Err(e) => {
                    // Log warning but don't fail - just skip invalid patterns
                    tracing::warn!(
                        "Invalid pattern '{}' in {}: {}",
                        pattern_str,
                        path.as_ref().display(),
                        e
                    );
                }
            }
        }

        Ok(patterns)
    }

    /// Get all files that should be analyzed in a directory tree
    pub fn find_files<P: AsRef<Path>>(&self, root: P) -> GuardianResult<Vec<PathBuf>> {
        let root = root.as_ref();
        let mut files = Vec::new();

        // OPTIMIZATION: Use filter_entry to skip massive directories BEFORE entering them
        let walker = WalkDir::new(root)
            .follow_links(false)
            .into_iter()
            .filter_entry(|e| {
                let name = e.file_name().to_string_lossy();

                // SKIP common massive directories to prevent IO floods
                if name == ".git"
                    || name == "target"
                    || name == "node_modules"
                    || name == ".venv"
                    || name == "venv"
                    || name == ".idea"
                    || name == ".vscode"
                {
                    return false;
                }
                true
            });

        for entry in walker.filter_map(|e| e.ok()) {
            let path = entry.path();

            // Only process files, not directories
            if path.is_file() && self.should_analyze(path)? {
                files.push(path.to_path_buf());
            }
        }

        Ok(files)
    }

    /// Filter a list of paths to only those that should be analyzed
    pub fn filter_paths<P: AsRef<Path>>(&self, paths: &[P]) -> GuardianResult<Vec<PathBuf>> {
        let mut filtered = Vec::new();

        for path in paths {
            if self.should_analyze(path)? {
                filtered.push(path.as_ref().to_path_buf());
            }
        }

        Ok(filtered)
    }

    /// Add a pattern to the filter
    pub fn add_pattern(&mut self, pattern: String) -> GuardianResult<()> {
        let (is_include, pattern_str) = if let Some(stripped) = pattern.strip_prefix('!') {
            (true, stripped.to_string())
        } else {
            (false, pattern)
        };

        let glob_pattern = glob::Pattern::new(&pattern_str)
            .map_err(|e| GuardianError::pattern(format!("Invalid pattern '{pattern_str}': {e}")))?;

        self.patterns.push(FilterPattern {
            pattern: glob_pattern,
            is_include,
            original: pattern_str,
        });

        Ok(())
    }

    /// Get debug information about patterns and their matches
    pub fn debug_patterns<P: AsRef<Path>>(&self, path: P) -> Vec<String> {
        let path = path.as_ref();
        let mut debug_info = Vec::new();

        for (i, pattern) in self.patterns.iter().enumerate() {
            let matches = self.pattern_matches_path(pattern, path);
            let prefix = if pattern.is_include { "!" } else { "" };

            debug_info.push(format!(
                "Pattern {}: {}{} -> {}",
                i,
                prefix,
                pattern.original,
                if matches { "MATCH" } else { "no match" }
            ));
        }

        debug_info
    }

    /// Check if a pattern matches a path using .gitignore-style rules
    fn pattern_matches_path(&self, pattern: &FilterPattern, path: &Path) -> bool {
        let path_str = path.to_string_lossy();

        // Handle different pattern types
        if pattern.original.ends_with('/') {
            // Directory pattern - only match directories
            if !path.is_dir() {
                return false;
            }
            // Remove trailing slash and match
            let dir_pattern = pattern.original.trim_end_matches('/');
            return glob::Pattern::new(dir_pattern)
                .map(|p| p.matches(&path_str))
                .unwrap_or(false);
        }

        if pattern.original.starts_with('/') {
            // Absolute pattern from root - remove leading slash and match from beginning
            let absolute_pattern = pattern
                .original
                .strip_prefix('/')
                .unwrap_or(&pattern.original);
            return glob::Pattern::new(absolute_pattern)
                .map(|p| p.matches(&path_str))
                .unwrap_or(false);
        }

        if pattern.original.contains('/') {
            // Pattern contains slash - match full path
            return pattern.pattern.matches(&path_str);
        } else {
            // No slash - match filename only
            if let Some(filename) = path.file_name() {
                return pattern.pattern.matches(&filename.to_string_lossy());
            }
        }

        false
    }
}

/// Architecture-compliant validation functions for integration testing
#[cfg(test)]
#[allow(dead_code)]
pub mod validation {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Validate basic pattern matching functionality - designed for integration testing
    pub fn validate_basic_pattern_matching() -> GuardianResult<()> {
        let filter = PathFilter::new(
            vec![
                "target/**".to_string(), // Exclude target directory
                "*.md".to_string(),      // Exclude markdown files
            ],
            None,
        )?;

        if !filter.should_analyze(Path::new("src/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Basic pattern validation failed - should analyze src files",
            ));
        }

        if filter.should_analyze(Path::new("target/debug/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Basic pattern validation failed - should exclude target files",
            ));
        }

        if filter.should_analyze(Path::new("README.md"))? {
            return Err(GuardianError::pattern(
                "Basic pattern validation failed - should exclude markdown files",
            ));
        }

        Ok(())
    }

    /// Validate include override functionality - designed for integration testing
    pub fn validate_include_override() -> GuardianResult<()> {
        let filter = PathFilter::new(
            vec![
                "target/**".to_string(),          // Exclude target
                "!target/special/**".to_string(), // But include target/special
            ],
            None,
        )?;

        if filter.should_analyze(Path::new("target/debug/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Include override validation failed - should exclude target/debug",
            ));
        }

        if !filter.should_analyze(Path::new("target/special/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Include override validation failed - should include target/special",
            ));
        }

        Ok(())
    }

    /// Validate pattern order functionality - designed for integration testing
    pub fn validate_pattern_order() -> GuardianResult<()> {
        let filter = PathFilter::new(
            vec![
                "tests/**".to_string(),            // Exclude tests
                "!tests/important.rs".to_string(), // But include important test
                "!*.rs".to_string(),               // And include all .rs files (overrides excludes)
            ],
            None,
        )?;

        if !filter.should_analyze(Path::new("src/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Pattern order validation failed - should analyze src files",
            ));
        }

        if !filter.should_analyze(Path::new("tests/unit.rs"))? {
            return Err(GuardianError::pattern(
                "Pattern order validation failed - should analyze test files with overrides",
            ));
        }

        if !filter.should_analyze(Path::new("tests/important.rs"))? {
            return Err(GuardianError::pattern(
                "Pattern order validation failed - should analyze important test files",
            ));
        }

        Ok(())
    }

    /// Validate guardianignore file functionality - designed for integration testing
    pub fn validate_guardianignore_file() -> GuardianResult<()> {
        let temp_dir = TempDir::new()
            .map_err(|e| GuardianError::config(format!("Failed to create temp dir: {}", e)))?;
        let root = temp_dir.path();

        // Create directory structure
        fs::create_dir_all(root.join("src"))?;
        fs::create_dir_all(root.join("tests"))?;

        // Create .guardianignore file
        fs::write(
            root.join(".guardianignore"),
            "*.tmp\ntests/**\n!tests/important.rs\n",
        )?;

        // Create test files
        fs::write(root.join("src/lib.rs"), "")?;
        fs::write(root.join("temp.tmp"), "")?;
        fs::write(root.join("tests/unit.rs"), "")?;
        fs::write(root.join("tests/important.rs"), "")?;

        let filter = PathFilter::new(vec![], Some(".guardianignore".to_string()))?;

        if !filter.should_analyze(root.join("src/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Guardianignore validation failed - should analyze src files",
            ));
        }

        if filter.should_analyze(root.join("temp.tmp"))? {
            return Err(GuardianError::pattern(
                "Guardianignore validation failed - should exclude tmp files",
            ));
        }

        if filter.should_analyze(root.join("tests/unit.rs"))? {
            return Err(GuardianError::pattern(
                "Guardianignore validation failed - should exclude test files",
            ));
        }

        if !filter.should_analyze(root.join("tests/important.rs"))? {
            return Err(GuardianError::pattern(
                "Guardianignore validation failed - should include important files",
            ));
        }

        Ok(())
    }

    /// Validate invalid pattern handling - designed for integration testing
    pub fn validate_invalid_pattern_handling() -> GuardianResult<()> {
        let result = PathFilter::new(vec!["[invalid".to_string()], None);
        if result.is_ok() {
            return Err(GuardianError::pattern(
                "Invalid pattern validation failed - should reject invalid patterns",
            ));
        }

        Ok(())
    }

    /// Validate default filter functionality - designed for integration testing
    pub fn validate_default_filter() -> GuardianResult<()> {
        let filter = PathFilter::with_defaults()?;

        if filter.should_analyze(Path::new("target/debug/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Default filter validation failed - should exclude target directory",
            ));
        }

        if !filter.should_analyze(Path::new("src/lib.rs"))? {
            return Err(GuardianError::pattern(
                "Default filter validation failed - should include source files",
            ));
        }

        Ok(())
    }
}