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
//! Example: Quality Gate with Violation Details (Issue #129, #226 Fix)
//!
//! This example demonstrates that the quality-gate command now shows
//! detailed violation information instead of just counts. With #226,
//! violations now carry optional `ViolationDetails` for explainability.
//!
//! # Usage
//! ```bash
//! cargo run --example quality_gate_violations
//! ```
//!
//! # Related
//! - GitHub Issue #129: quality-gate sub-command doesn't report violations
//! - GitHub Issue #226: entropy violations lack explainability
//! - Fix: ViolationDetails struct with affected_files, example_code, fix_suggestion, score_factors
use pmat::cli::analysis_utilities::{
format_quality_gate_output, QualityGateResults, QualityViolation, ViolationDetails,
};
use pmat::cli::QualityGateOutputFormat;
fn main() {
println!("=== Quality Gate Violation Reporting Demo ===\n");
// Create sample results with violations
let results = QualityGateResults {
passed: false,
total_violations: 5,
// Of the 5 findings, 4 are verdict-bearing (warning/error) and one is
// advisory (info). `passed` follows this count, not total_violations —
// an informational finding must not decide a gate verdict.
blocking_violations: 4,
complexity_violations: 2,
dead_code_violations: 1,
satd_violations: 1,
entropy_violations: 1,
security_violations: 0,
duplicate_violations: 0,
coverage_violations: 0,
section_violations: 0,
provability_violations: 0,
provability_score: None,
violations: vec![], // Simplified for backwards compat
};
// Create sample violations — note the new `details` field (#226)
let violations = vec![
QualityViolation::new(
"complexity",
"error",
"src/parser.rs",
Some(42),
"Cyclomatic complexity 25 exceeds threshold 20",
),
QualityViolation::new(
"complexity",
"warning",
"src/analyzer.rs",
Some(100),
"Cyclomatic complexity 18 approaching threshold",
),
QualityViolation::new(
"dead_code",
"warning",
"src/utils.rs",
Some(55),
"Function 'unused_helper' is never called",
),
QualityViolation::new(
"satd",
"info",
"src/main.rs",
Some(10),
"TODO: Refactor this function",
),
// Entropy violation WITH explainability details (#226)
// Note: structural code hashing groups only structurally identical patterns,
// so variation_score is 0.0 (all matches are identical after normalization).
QualityViolation::new(
"entropy",
"warning",
"src/config.rs",
None,
"ResourceManagement pattern repeated 8 times (saves 120 lines)",
)
.with_details(ViolationDetails {
affected_files: vec!["src/config.rs".to_string(), "src/settings.rs".to_string()],
example_code: Some("let guard = mutex.lock()".to_string()),
fix_suggestion: Some("Extract shared resource guard helper".to_string()),
score_factors: vec![
"pattern_type: ResourceManagement".to_string(),
"repetitions: 8".to_string(),
"variation_score: 0.0 (structurally identical)".to_string(),
],
}),
];
// Demo: Summary format (now shows violations)
println!("--- Summary Format (default) ---\n");
let summary =
format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Summary)
.expect("formatting should work");
println!("{}", summary);
// Demo: JSON format (clean output with ViolationDetails)
println!("\n--- JSON Format (clean, no progress lines - #230) ---\n");
let json = format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Json)
.expect("formatting should work");
println!("{}", json);
// Demo: Detailed format (full violation list)
println!("\n--- Detailed Format ---\n");
let detailed =
format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Detailed)
.expect("formatting should work");
println!("{}", detailed);
println!("\n=== End of Demo ===");
println!("\nDemonstrates fixes for #129 (violation reporting), #226 (entropy explainability),");
println!("and #230 (clean JSON output).");
}