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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# Rust Guardian

[![Crates.io](https://img.shields.io/crates/v/rust-guardian.svg)](https://crates.io/crates/rust-guardian)
[![Documentation](https://docs.rs/rust-guardian/badge.svg)](https://docs.rs/rust-guardian)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Dynamic code quality enforcement that prevents incomplete or placeholder code from reaching production.**

Rust Guardian is a self-contained, production-ready crate for comprehensive code quality analysis. It provides both a CLI tool and library API for detecting placeholder code, enforcing architectural boundaries, and maintaining code quality standards across any Rust project.

## Features

- **🔍 Pattern-Based Analysis**: Detect TODO comments, unimplemented macros, and placeholder code
- **🏗️ Architecture Enforcement**: Validate architectural principles and bounded context integrity  
- **⚡ High Performance**: Parallel processing with intelligent caching
- **🤖 Automation Integration**: Async API for CI/CD and automated workflows
- **📊 Multiple Output Formats**: Human, JSON, JUnit, SARIF, GitHub Actions
- **⚙️ Flexible Configuration**: YAML-based pattern customization
- **🔄 Watch Mode**: Real-time validation during development

## Quick Start

### Installation

```bash
# Install from crates.io
cargo install rust-guardian

# Or add to your project
cargo add rust-guardian
```

### CLI Usage

```bash
# Check entire project
rust-guardian check

# Check specific paths
rust-guardian check src/ lib.rs

# Output formats
rust-guardian check --format json              # JSON for tooling
rust-guardian check --format agent             # Agent-friendly: [line:path] violation
rust-guardian check --format junit             # JUnit XML for CI/CD  
rust-guardian check --format sarif             # SARIF for security tools
rust-guardian check --format github            # GitHub Actions format

# Filter by severity
rust-guardian check --severity error           # Only errors
rust-guardian check --severity warning         # Warnings and errors
rust-guardian check --severity info            # All violations

# Performance and caching
rust-guardian check --cache                    # Enable caching
rust-guardian check --cache-file /tmp/cache    # Custom cache location
rust-guardian check --no-parallel              # Disable parallel processing
rust-guardian check --max-violations 50        # Limit output

# File filtering
rust-guardian check --exclude "**/*.tmp"       # Additional exclude patterns
rust-guardian check --exclude "legacy/" --exclude "vendor/"
rust-guardian check --guardianignore .custom   # Custom ignore file
rust-guardian check --no-ignore                # Ignore all .guardianignore files

# Configuration and debugging
rust-guardian check -c custom.yaml             # Custom config file
rust-guardian check --verbose                  # Enable debug logging
rust-guardian check --no-color                 # Disable colors
rust-guardian check --fail-fast                # Stop on first error

# Watch mode for development
rust-guardian watch src/                       # Watch directory for changes
rust-guardian watch --debounce 500             # Custom debounce ms

# Configuration management
rust-guardian validate-config                  # Validate guardian.yaml
rust-guardian validate-config custom.yaml     # Validate custom config

# Rule management
rust-guardian rules                            # List all rules
rust-guardian rules --enabled-only            # Only show enabled rules
rust-guardian rules --category placeholders   # Filter by category
rust-guardian explain todo_comments           # Explain specific rule

# Cache management
rust-guardian cache stats                     # Show cache statistics
rust-guardian cache clear                     # Clear cache
```

### Library Usage

```rust
use rust_guardian::{GuardianValidator, ValidationOptions};
use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let guardian = GuardianValidator::new()?;
    
    let paths = vec![PathBuf::from("src")];
    let report = guardian.validate_async(paths).await?;
    
    if report.has_errors() {
        println!("❌ Found {} violations", report.violations.len());
        for violation in &report.violations {
            println!("  {}:{}: {}", 
                violation.file_path.display(),
                violation.line_number.unwrap_or(0),
                violation.message
            );
        }
        return Err("Code quality violations found".into());
    }
    
    println!("✅ All checks passed!");
    Ok(())
}
```

## Configuration

Create `guardian.yaml` in your project root:

```yaml
version: "1.0"

paths:
  patterns:
    # Exclude patterns (like .gitignore)
    - "target/"           # Ignore target directory
    - "**/*.md"          # Ignore all markdown files
    - "*.generated.rs"   # Ignore generated files
    - "!README.md"       # But include README.md
    - "**/tests/**"      # Ignore test directories
    
    # Include patterns (with !) - override previous excludes
    - "!src/core/**/*.rs"      # Always check core modules
    - "!src/api/**/*.rs"       # Always check API modules
    
  # Optional: Support .guardianignore file
  ignore_file: ".guardianignore"  # Like .gitignore but for guardian

patterns:
  placeholders:
    severity: error
    enabled: true
    rules:
      - id: todo_comments
        type: regex
        pattern: '\b(TODO|FIXME|HACK|XXX|BUG|REFACTOR)\b'
        message: "Placeholder comment detected: {match}"
        
      - id: unimplemented_macros
        type: ast
        pattern: |
          macro_call:
            - unimplemented
            - todo
            - unreachable  
            - panic
        message: "Unfinished macro {macro_name}! found"
        exclude_if:
          - attribute: "#[test]"

  architectural_violations:
    severity: error
    enabled: true
    rules:
      - id: hardcoded_paths
        type: regex
        pattern: '(/tmp/|/var/|/home/)[^"]*"'
        message: "Hardcoded path found - use configuration instead"
```

## Path Pattern Configuration

Rust Guardian uses .gitignore-style patterns for intuitive file filtering:

### **Pattern Syntax**

- **No prefix**: Exclude pattern (like .gitignore)
  ```yaml
  - "target/"           # Exclude target directory
  - "**/*.md"          # Exclude all markdown files
  ```

- **`!` prefix**: Include pattern (override previous excludes)
  ```yaml
  - "!README.md"       # Include README.md even if *.md was excluded
  - "!docs/**/*.md"    # Include all docs markdown files
  ```

- **Glob patterns supported**:
  - `*` - Matches anything except `/`
  - `**` - Matches any number of directories
  - `?` - Matches single character
  - `[abc]` - Matches any character in brackets

### **Pattern Resolution**

Patterns are evaluated in order, with later patterns overriding earlier ones:

```yaml
paths:
  patterns:
    - "**/*.rs"                    # Include all Rust files
    - "target/**"                  # But exclude target directory
    - "**/tests/**"                # And exclude test directories
    - "!integration-tests/**"      # But include integration-tests
    - "!src/core/**/*.rs"          # Always include core modules
```

### **`.guardianignore` File Support**

Like `.gitignore` but for Guardian:

```bash
# .guardianignore in project root
target/
**/*.md
!README.md

# Directory-specific .guardianignore
src/legacy/.guardianignore:
*.old.rs
deprecated/
```

- Works exactly like `.gitignore`
- Can be placed in any directory
- Patterns relative to file location
- Multiple files merged during traversal

## Pattern Types

### Regex Patterns
Text-based pattern matching using regular expressions:

```yaml
- id: temporary_markers
  type: regex
  pattern: '(for now|temporary|placeholder|stub|dummy|fake)'
  case_sensitive: false
  message: "Temporary implementation marker found"
```

### AST Patterns  
Rust syntax tree analysis for semantic understanding:

```yaml
- id: empty_ok_return
  type: ast
  pattern: |
    function:
      body:
        return: Ok(())
      min_statements: 1
  message: "Function returns Ok(()) with no implementation"
```

### Semantic Patterns
Advanced code analysis for architectural compliance:

```yaml
- id: direct_internal_access
  type: import_analysis
  forbidden_imports:
    "src/server": ["core::internal"]
  message: "Direct internal access violates module boundaries"
```

## Automation Integration

For CI/CD pipelines and automated workflows that need to validate code before committing:

```rust
use rust_guardian::{GuardianValidator, Severity};

async fn automated_pre_commit_check(modified_files: Vec<PathBuf>) -> Result<(), String> {
    let guardian = GuardianValidator::new()
        .map_err(|e| format!("Failed to initialize guardian: {}", e))?;
    
    let report = guardian.validate_async(modified_files).await
        .map_err(|e| format!("Validation failed: {}", e))?;
    
    // Only fail on errors, warnings are informational
    let errors: Vec<_> = report.violations
        .iter()
        .filter(|v| v.severity == Severity::Error)
        .collect();
    
    if !errors.is_empty() {
        let mut message = String::from("Code quality violations detected:\n");
        for error in errors {
            message.push_str(&format!(
                "  {}:{}: {}\n",
                error.file_path.display(),
                error.line_number.unwrap_or(0),
                error.message
            ));
        }
        return Err(message);
    }
    
    Ok(())
}
```

## CI/CD Integration

### GitHub Actions

```yaml
- name: Code Quality Check
  run: |
    rust-guardian check --format github --severity error >> $GITHUB_STEP_SUMMARY
    rust-guardian check --format json --severity error > guardian-report.json
    
- name: Upload Results
  uses: actions/upload-artifact@v3
  with:
    name: guardian-report
    path: guardian-report.json
```

### GitLab CI

```yaml
code_quality:
  script:
    - rust-guardian check --format junit --severity error > guardian-report.xml
  artifacts:
    reports:
      junit: guardian-report.xml
```

## Performance

Rust Guardian is designed for speed:

- **Parallel Processing**: Analyzes multiple files concurrently using rayon
- **Intelligent Caching**: Skips unchanged files using hash-based caching
- **Memory Efficient**: Streams large files, limits memory usage
- **Fast Startup**: Embedded patterns, no external dependencies

Benchmarks on a typical medium-sized Rust project (5,000 files, 500k LOC):
- **Cold Run**: ~1.2 seconds
- **Warm Run (cached)**: ~0.2 seconds  
- **Memory Usage**: ~100MB peak

## Watch Mode

For real-time feedback during development:

```bash
rust-guardian watch src/
```

Features:
- **Debounced Updates**: Groups rapid file changes
- **Hot Configuration Reload**: Updates patterns without restart
- **Focused Output**: Only shows changed files
- **Performance Optimized**: Incremental analysis

## Output Formats

### Human (Default)
Colored terminal output with context:

```
❌ Code Quality Violations Found

📁 src/api/handlers.rs
  45:12:todo_comments [error] Placeholder comment detected: TODO
    │ // TODO: Implement error handling

📊 Summary: 1 error, 2 warnings in 156 files (1.2s)
```

### Agent Format
Simplified format for automated processing and agent consumption:

```
[45:src/api/handlers.rs]
Placeholder comment detected: TODO

[102:src/lib.rs]  
Traditional unit tests found - consider integration tests for better architectural validation

[67:src/models.rs]
Function returns Ok(()) with no meaningful implementation

```

### JSON
Machine-readable format for tooling:

```json
{
  "violations": [
    {
      "rule_id": "todo_comments",
      "severity": "error",
      "file_path": "src/lib.rs", 
      "line_number": 45,
      "column_number": 12,
      "message": "Placeholder comment detected: TODO",
      "context": "    // TODO: Implement error handling"
    }
  ],
  "summary": {
    "total_files": 156,
    "violations_by_severity": {
      "error": 1,
      "warning": 2,
      "info": 0
    },
    "execution_time_ms": 1200
  }
}
```

### JUnit XML
For CI/CD test result integration:

```xml
<testsuite name="rust-guardian" tests="3" failures="1" errors="0" time="1.2">
  <testcase classname="placeholders" name="todo_comments">
    <failure message="Placeholder comment detected: TODO">
      File: src/lib.rs:45:12
      Context: // TODO: Implement error handling
    </failure>
  </testcase>
</testsuite>
```

## Rule Reference

### Built-in Pattern Categories

#### Placeholders (`placeholders`)
- `todo_comments`: TODO, FIXME, HACK, XXX, BUG, REFACTOR comments
- `temporary_implementation`: "for now", "placeholder", "stub" markers  
- `unimplemented_macros`: unimplemented!(), todo!(), unreachable!(), panic!()

#### Incomplete Implementations (`incomplete_implementations`)
- `empty_ok_return`: Functions returning Ok(()) with no logic
- `minimal_function`: Functions with insufficient implementation

#### Architectural Violations (`architectural_violations`)  
- `domain_header_missing`: Missing domain module headers
- `boundary_violation`: Module boundary violations in imports
- `hardcoded_paths`: Hardcoded file paths instead of configuration

#### Testing Requirements (`testing_requirements`)
- `untested_public_function`: Public functions lacking test coverage

### Severity Levels

- **Error**: Blocks commits, fails CI/CD builds
- **Warning**: Informational, doesn't fail builds by default
- **Info**: Documentation and suggestions

## Advanced Usage

### Advanced Path Configuration Examples

**Complex filtering with override patterns:**
```yaml
paths:
  patterns:
    # Start with broad exclusions
    - "**/target/**"           # Exclude all target directories
    - "**/*.md"               # Exclude all markdown
    - "**/tests/**"           # Exclude test directories
    - "vendor/"               # Exclude vendor
    
    # Then selectively include what we want
    - "!README.md"            # But include README
    - "!docs/**/*.md"         # Include all documentation
    - "!integration-tests/"    # Include integration tests specifically
    - "!src/core/**"          # Always analyze core modules
    
    # Final specific exclusions
    - "src/core/benches/"     # But not benchmarks in core
```

**Project-specific example:**
```yaml
paths:
  patterns:
    # Legacy code - exclude by default
    - "legacy/**"
    - "deprecated/**"
    
    # But include specific legacy modules being refactored
    - "!legacy/auth/" 
    - "!legacy/models/"
    
    # Generated code exclusions
    - "**/*.generated.rs"
    - "**/*.pb.rs"           # Protocol buffers
    - "src/schema.rs"        # Diesel schema
    
    # But include hand-maintained generated code
    - "!src/api/generated/custom_*.rs"
```

### Custom Patterns

Extend with project-specific patterns:

```yaml
patterns:
  custom:
    severity: warning
    enabled: true
    rules:
      - id: deprecated_api_usage
        type: regex
        pattern: 'deprecated_function\('
        message: "Use new_function() instead of deprecated_function()"
        
      - id: missing_error_context
        type: ast
        pattern: |
          function:
            returns: Result
            body:
              missing: ".context(" 
        message: "Result should include error context"
```

### Programmatic Configuration

```rust
use rust_guardian::{PatternConfig, PatternRule, RuleType, Severity};

let mut config = PatternConfig::default();

config.add_rule(PatternRule {
    id: "custom_check".to_string(),
    rule_type: RuleType::Regex,
    severity: Severity::Warning,
    pattern: r"\bFIXME\b".to_string(),
    message: "FIXME comment found".to_string(),
    enabled: true,
    ..Default::default()
});

let guardian = GuardianValidator::with_config(config)?;
```

### Integration with Pre-commit Hooks

`.pre-commit-hooks.yaml`:

```yaml
repos:
  - repo: local
    hooks:
      - id: rust-guardian
        name: Rust Guardian
        entry: rust-guardian check --severity error
        language: system
        files: '\.(rs|toml|yaml)$'
        pass_filenames: false
```

## Contributing

We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

### Development Setup

```bash
git clone https://github.com/cloudfunnels/rust-guardian
cd rust-guardian
cargo build
cargo test
```

### Running Tests

```bash
# Unit tests
cargo test

# Integration tests  
cargo test --test integration

# Performance benchmarks
cargo bench
```

## License

Licensed under the MIT License. See [LICENSE](LICENSE) for details.

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for release history.

## Why Guardian Exists

Every Rust developer knows the gap between "it compiles" and "it's complete." This gap becomes a chasm when using AI assistance or working in teams. AI generates syntactically perfect code filled with TODOs and placeholders. Teams merge "temporary" solutions that become permanent. Technical debt accumulates invisibly.

Guardian was born from a simple realization: **Compilable ≠ Complete**.

We built Guardian because we believe every line of code deserves to be finished, not just functional. Whether written by human, AI, or collaborative development, code should be complete, intentional, and ready for production.

This tool enforces what code reviews miss, what AI forgets to finish, and what "we'll fix it later" never addresses. It's not just about catching TODOs - it's about ensuring that every function that compiles actually does what it promises.

Guardian stands watch so you can focus on creating, knowing that nothing incomplete will slip through.

Built with love for the craft of software development.

Done and done.

— The Rust Guardian Team

## Support

- **Documentation**: [docs.rs/rust-guardian]https://docs.rs/rust-guardian
- **Issues**: [GitHub Issues]https://github.com/cloudfunnels/rust-guardian/issues
- **Discussions**: [GitHub Discussions]https://github.com/cloudfunnels/rust-guardian/discussions