1mod analyzer;
2mod cli;
3mod config;
4mod coupling;
5mod dry;
6mod findings;
7mod normalize;
8mod pipeline;
9mod report;
10mod scope;
11mod srp;
12mod structural;
13mod tq;
14mod watch;
15
16use std::path::Path;
17
18use clap::{CommandFactory, Parser};
19
20use cli::{Cli, OutputFormat};
21use config::Config;
22
23fn determine_output_format(cli: &Cli) -> OutputFormat {
26 if let Some(ref fmt) = cli.format {
27 fmt.clone()
28 } else if cli.json {
29 OutputFormat::Json
30 } else {
31 OutputFormat::Text
32 }
33}
34
35fn handle_init(content: &str) -> Result<(), i32> {
38 let path = Path::new("rustqual.toml");
39 if path.exists() {
40 eprintln!("Error: rustqual.toml already exists in the current directory.");
41 return Err(1);
42 }
43 match std::fs::write(path, content) {
44 Ok(()) => {
45 eprintln!("Created rustqual.toml with tailored configuration.");
46 Ok(())
47 }
48 Err(e) => {
49 eprintln!("Error writing rustqual.toml: {e}");
50 Err(1)
51 }
52 }
53}
54
55fn handle_completions(shell: clap_complete::Shell) {
58 clap_complete::generate(
59 shell,
60 &mut Cli::command(),
61 "rustqual",
62 &mut std::io::stdout(),
63 );
64}
65
66fn load_explicit_config(config_path: &Path) -> Result<Config, i32> {
69 match std::fs::read_to_string(config_path) {
70 Ok(content) => match toml::from_str(&content) {
71 Ok(c) => Ok(c),
72 Err(e) => {
73 eprintln!("Error parsing config: {e}");
74 Err(2)
75 }
76 },
77 Err(e) => {
78 eprintln!("Error reading config: {e}");
79 Err(2)
80 }
81 }
82}
83
84fn load_auto_config(path: &Path) -> Result<Config, i32> {
87 Config::load(path).map_err(|e| {
88 eprintln!("Error: {e}");
89 2
90 })
91}
92
93fn load_config(cli: &Cli) -> Result<Config, i32> {
96 cli.config
97 .as_ref()
98 .map(|p| load_explicit_config(p))
99 .unwrap_or_else(|| load_auto_config(&cli.path))
100}
101
102fn setup_config(cli: &Cli) -> Result<Config, i32> {
105 let mut config = load_config(cli)?;
106 config.compile();
107 apply_cli_overrides(&mut config, cli);
108 validate_config_weights(&config)?;
109 Ok(config)
110}
111
112fn validate_config_weights(config: &Config) -> Result<(), i32> {
115 config::validate_weights(config).map_err(|e| {
116 eprintln!("Error: {e}");
117 2
118 })
119}
120
121fn apply_cli_overrides(config: &mut Config, cli: &Cli) {
124 if cli.strict_closures {
125 config.strict_closures = true;
126 }
127 if cli.strict_iterators {
128 config.strict_iterator_chains = true;
129 }
130 if cli.allow_recursion {
131 config.allow_recursion = true;
132 }
133 if cli.strict_error_propagation {
134 config.strict_error_propagation = true;
135 }
136 if cli.fail_on_warnings {
137 config.fail_on_warnings = true;
138 }
139 if let Some(ref coverage) = cli.coverage {
140 config.test.coverage_file = Some(coverage.display().to_string());
141 }
142}
143
144fn handle_save_baseline(
147 path: &Path,
148 all_results: &[analyzer::FunctionAnalysis],
149 summary: &report::Summary,
150) -> Result<(), i32> {
151 let baseline = report::create_baseline(all_results, summary);
152 match std::fs::write(path, baseline) {
153 Ok(()) => {
154 eprintln!("Baseline saved to {}", path.display());
155 Ok(())
156 }
157 Err(e) => {
158 eprintln!("Error saving baseline: {e}");
159 Err(1)
160 }
161 }
162}
163
164fn handle_compare(
167 path: &Path,
168 all_results: &[analyzer::FunctionAnalysis],
169 summary: &report::Summary,
170) -> Result<bool, i32> {
171 let baseline_content = std::fs::read_to_string(path).map_err(|e| {
172 eprintln!("Error reading baseline: {e}");
173 1
174 })?;
175 Ok(report::print_comparison(
176 &baseline_content,
177 all_results,
178 summary,
179 ))
180}
181
182fn check_min_quality_score(min_score: f64, summary: &report::Summary) -> Result<(), i32> {
185 let actual = summary.quality_score * analyzer::PERCENTAGE_MULTIPLIER;
186 if actual < min_score {
187 eprintln!(
188 "Quality score {:.1}% is below minimum {:.1}%",
189 actual, min_score,
190 );
191 return Err(1);
192 }
193 Ok(())
194}
195
196fn warn_suppression_ratio(summary: &report::Summary, max_ratio: f64) {
199 if !summary.suppression_ratio_exceeded || summary.total == 0 {
200 return;
201 }
202 eprintln!(
203 "Warning: {} suppression(s) found ({:.1}% of functions, max: {:.1}%)",
204 summary.all_suppressions,
205 summary.all_suppressions as f64 / summary.total as f64 * analyzer::PERCENTAGE_MULTIPLIER,
206 max_ratio * analyzer::PERCENTAGE_MULTIPLIER,
207 );
208}
209
210fn check_fail_on_warnings(config: &Config, summary: &report::Summary) -> Result<(), i32> {
213 if config.fail_on_warnings && summary.suppression_ratio_exceeded {
214 eprintln!("Error: warnings present and --fail-on-warnings is set");
215 return Err(1);
216 }
217 Ok(())
218}
219
220fn check_quality_gates(cli: &Cli, summary: &report::Summary) -> Result<(), i32> {
223 cli.min_quality_score
224 .iter()
225 .try_for_each(|&s| check_min_quality_score(s, summary))
226}
227
228fn check_default_fail(no_fail: bool, total_findings: usize) -> Result<(), i32> {
231 if !no_fail && total_findings > 0 {
232 return Err(1);
233 }
234 Ok(())
235}
236
237fn apply_exit_gates(cli: &Cli, config: &Config, summary: &report::Summary) -> Result<(), i32> {
240 warn_suppression_ratio(summary, config.max_suppression_ratio);
241 check_fail_on_warnings(config, summary)?;
242 check_quality_gates(cli, summary)?;
243 check_default_fail(cli.no_fail, summary.total_findings())
244}
245
246fn sort_by_effort(results: &mut [analyzer::FunctionAnalysis]) {
249 results.sort_by(|a, b| {
250 b.effort_score
251 .unwrap_or(0.0)
252 .partial_cmp(&a.effort_score.unwrap_or(0.0))
253 .unwrap_or(std::cmp::Ordering::Equal)
254 });
255}
256
257pub fn run() -> Result<(), i32> {
259 let mut args: Vec<String> = std::env::args().collect();
260 if args.len() > 1 && args[1] == "qual" {
262 args.remove(1);
263 }
264 let cli = Cli::parse_from(args);
265
266 if cli.init {
267 let files = pipeline::collect_rust_files(&cli.path);
268 let content = if files.is_empty() {
269 config::generate_default_config().to_string()
270 } else {
271 let parsed = pipeline::read_and_parse_files(&files, &cli.path);
272 let default_config = Config::default();
273 let scope_refs: Vec<(&str, &syn::File)> =
274 parsed.iter().map(|(p, _, f)| (p.as_str(), f)).collect();
275 let scope = scope::ProjectScope::from_files(&scope_refs);
276 let analyzer_obj = analyzer::Analyzer::new(&default_config, &scope);
277 let all_results: Vec<_> = parsed
278 .iter()
279 .flat_map(|(path, _, syntax)| analyzer_obj.analyze_file(syntax, path))
280 .collect();
281 let metrics = config::init::extract_init_metrics(files.len(), &all_results);
282 config::generate_tailored_config(&metrics)
283 };
284 return handle_init(&content);
285 }
286 if let Some(shell) = cli.completions {
287 handle_completions(shell);
288 return Ok(());
289 }
290
291 let output_format = determine_output_format(&cli);
292 let config = setup_config(&cli)?;
293
294 if cli.watch {
295 return watch::run_watch_mode(&cli, &config, &output_format);
296 }
297
298 let files = pipeline::collect_filtered_files(&cli.path, &config);
299 let files = if let Some(ref git_ref) = cli.diff {
300 match pipeline::get_git_changed_files(&cli.path, git_ref) {
301 Ok(changed) => {
302 let filtered = pipeline::filter_to_changed(files, &changed);
303 eprintln!(
304 "[diff mode: {} changed file(s) vs {git_ref}]",
305 filtered.len()
306 );
307 filtered
308 }
309 Err(e) => {
310 eprintln!("Warning: {e}. Analyzing all files.");
311 files
312 }
313 }
314 } else {
315 files
316 };
317 if files.is_empty() {
318 eprintln!("No Rust source files found in {}", cli.path.display());
319 return Ok(());
320 }
321
322 let parsed = pipeline::read_and_parse_files(&files, &cli.path);
323 let mut analysis = pipeline::run_analysis(&parsed, &config);
324 if cli.sort_by_effort {
325 sort_by_effort(&mut analysis.results);
326 }
327 if cli.findings {
328 let entries = crate::report::findings_list::collect_all_findings(&analysis);
329 crate::report::findings_list::print_findings(&entries);
330 } else {
331 pipeline::output_results(
332 &analysis,
333 &output_format,
334 cli.verbose,
335 cli.suggestions,
336 &config.coupling,
337 );
338 }
339
340 cli.save_baseline
341 .as_ref()
342 .map(|p| handle_save_baseline(p, &analysis.results, &analysis.summary))
343 .transpose()?;
344 if let Some(ref compare_path) = cli.compare {
345 let regressed = handle_compare(compare_path, &analysis.results, &analysis.summary)?;
346 if cli.fail_on_regression && regressed {
347 return Err(1);
348 }
349 }
350
351 apply_exit_gates(&cli, &config, &analysis.summary)
352}
353
354#[cfg(test)]
355mod tests {
356 use super::*;
357
358 #[test]
361 fn test_output_format_text() {
362 assert_eq!("text".parse::<OutputFormat>().unwrap(), OutputFormat::Text);
363 }
364
365 #[test]
366 fn test_output_format_json() {
367 assert_eq!("json".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
368 }
369
370 #[test]
371 fn test_output_format_github() {
372 assert_eq!(
373 "github".parse::<OutputFormat>().unwrap(),
374 OutputFormat::Github
375 );
376 }
377
378 #[test]
379 fn test_output_format_dot() {
380 assert_eq!("dot".parse::<OutputFormat>().unwrap(), OutputFormat::Dot);
381 }
382
383 #[test]
384 fn test_output_format_sarif() {
385 assert_eq!(
386 "sarif".parse::<OutputFormat>().unwrap(),
387 OutputFormat::Sarif
388 );
389 }
390
391 #[test]
392 fn test_output_format_html() {
393 assert_eq!("html".parse::<OutputFormat>().unwrap(), OutputFormat::Html);
394 }
395
396 #[test]
397 fn test_output_format_case_insensitive() {
398 assert_eq!("JSON".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
399 assert_eq!("Text".parse::<OutputFormat>().unwrap(), OutputFormat::Text);
400 assert_eq!(
401 "GITHUB".parse::<OutputFormat>().unwrap(),
402 OutputFormat::Github
403 );
404 }
405
406 #[test]
407 fn test_output_format_invalid() {
408 assert!("xml".parse::<OutputFormat>().is_err());
409 assert!("csv".parse::<OutputFormat>().is_err());
410 }
411
412 #[test]
413 fn test_output_format_default() {
414 assert_eq!(OutputFormat::default(), OutputFormat::Text);
415 }
416
417 #[test]
420 fn test_apply_cli_overrides_strict_closures() {
421 let mut config = Config::default();
422 let cli = Cli::parse_from(["test", "--strict-closures"]);
423 apply_cli_overrides(&mut config, &cli);
424 assert!(config.strict_closures);
425 }
426
427 #[test]
428 fn test_apply_cli_overrides_allow_recursion() {
429 let mut config = Config::default();
430 let cli = Cli::parse_from(["test", "--allow-recursion"]);
431 apply_cli_overrides(&mut config, &cli);
432 assert!(config.allow_recursion);
433 }
434
435 #[test]
436 fn test_apply_cli_overrides_strict_error_propagation() {
437 let mut config = Config::default();
438 let cli = Cli::parse_from(["test", "--strict-error-propagation"]);
439 apply_cli_overrides(&mut config, &cli);
440 assert!(config.strict_error_propagation);
441 }
442
443 #[test]
444 fn test_apply_cli_overrides_strict_iterators() {
445 let mut config = Config::default();
446 let cli = Cli::parse_from(["test", "--strict-iterators"]);
447 apply_cli_overrides(&mut config, &cli);
448 assert!(config.strict_iterator_chains);
449 }
450
451 #[test]
452 fn test_apply_cli_overrides_no_flags() {
453 let mut config = Config::default();
454 let cli = Cli::parse_from(["test"]);
455 apply_cli_overrides(&mut config, &cli);
456 assert!(!config.strict_closures);
457 assert!(!config.strict_iterator_chains);
458 assert!(!config.allow_recursion);
459 assert!(!config.strict_error_propagation);
460 }
461
462 #[test]
463 fn test_fail_on_warnings_cli_parse() {
464 let cli = Cli::parse_from(["test", "--fail-on-warnings"]);
465 assert!(cli.fail_on_warnings);
466 }
467
468 #[test]
469 fn test_fail_on_warnings_default_false() {
470 let cli = Cli::parse_from(["test"]);
471 assert!(!cli.fail_on_warnings);
472 }
473
474 #[test]
475 fn test_apply_cli_overrides_fail_on_warnings() {
476 let mut config = Config::default();
477 let cli = Cli::parse_from(["test", "--fail-on-warnings"]);
478 apply_cli_overrides(&mut config, &cli);
479 assert!(config.fail_on_warnings);
480 }
481
482 #[test]
483 fn test_fail_on_warnings_config_default() {
484 let config = Config::default();
485 assert!(!config.fail_on_warnings);
486 }
487
488 #[test]
491 fn test_check_fail_on_warnings_passes_when_no_warnings() {
492 let mut config = Config::default();
493 config.fail_on_warnings = true;
494 let summary = crate::report::Summary {
495 total: 10,
496 ..Default::default()
497 };
498 assert!(check_fail_on_warnings(&config, &summary).is_ok());
499 }
500
501 #[test]
502 fn test_check_fail_on_warnings_passes_when_disabled() {
503 let config = Config::default(); let summary = crate::report::Summary {
505 total: 10,
506 suppression_ratio_exceeded: true,
507 ..Default::default()
508 };
509 assert!(check_fail_on_warnings(&config, &summary).is_ok());
510 }
511
512 #[test]
513 fn test_check_fail_on_warnings_exits_when_triggered() {
514 let mut config = Config::default();
515 config.fail_on_warnings = true;
516 let summary = crate::report::Summary {
517 total: 10,
518 suppression_ratio_exceeded: true,
519 ..Default::default()
520 };
521 assert_eq!(check_fail_on_warnings(&config, &summary), Err(1));
522 }
523
524 #[test]
525 fn test_min_quality_score_cli_parse() {
526 let cli = Cli::parse_from(["test", "--min-quality-score", "80.0"]);
527 assert!((cli.min_quality_score.unwrap() - 80.0).abs() < f64::EPSILON);
528 }
529
530 #[test]
531 fn test_check_quality_gates_passes() {
532 let cli = Cli::parse_from(["test", "--min-quality-score", "50.0"]);
533 let mut summary = crate::report::Summary {
534 total: 10,
535 ..Default::default()
536 };
537 summary.compute_quality_score(&crate::config::sections::DEFAULT_QUALITY_WEIGHTS);
538 assert!(check_quality_gates(&cli, &summary).is_ok());
539 }
540
541 #[test]
542 fn test_check_min_quality_score_below_threshold() {
543 let mut summary = crate::report::Summary {
544 total: 10,
545 ..Default::default()
546 };
547 summary.quality_score = 0.5;
548 assert_eq!(check_min_quality_score(90.0, &summary), Err(1));
549 }
550
551 #[test]
552 fn test_check_min_quality_score_above_threshold() {
553 let mut summary = crate::report::Summary {
554 total: 10,
555 ..Default::default()
556 };
557 summary.quality_score = 0.95;
558 assert!(check_min_quality_score(90.0, &summary).is_ok());
559 }
560
561 #[test]
562 fn test_check_quality_gates_below_threshold() {
563 let cli = Cli::parse_from(["test", "--min-quality-score", "90.0"]);
564 let summary = crate::report::Summary {
565 total: 10,
566 quality_score: 0.5,
567 ..Default::default()
568 };
569 assert_eq!(check_quality_gates(&cli, &summary), Err(1));
570 }
571
572 #[test]
573 fn test_check_quality_gates_no_gate_set() {
574 let cli = Cli::parse_from(["test"]);
575 let summary = crate::report::Summary {
576 total: 10,
577 ..Default::default()
578 };
579 assert!(check_quality_gates(&cli, &summary).is_ok());
580 }
581
582 #[test]
583 fn test_check_default_fail_with_findings() {
584 assert_eq!(check_default_fail(false, 5), Err(1));
585 }
586
587 #[test]
588 fn test_check_default_fail_no_fail_mode() {
589 assert!(check_default_fail(true, 5).is_ok());
590 }
591
592 #[test]
593 fn test_check_default_fail_no_findings() {
594 assert!(check_default_fail(false, 0).is_ok());
595 }
596
597 #[test]
598 fn test_determine_output_format_explicit() {
599 let cli = Cli::parse_from(["test", "--format", "json"]);
600 assert_eq!(determine_output_format(&cli), OutputFormat::Json);
601 }
602
603 #[test]
604 fn test_determine_output_format_json_flag() {
605 let cli = Cli::parse_from(["test", "--json"]);
606 assert_eq!(determine_output_format(&cli), OutputFormat::Json);
607 }
608
609 #[test]
610 fn test_determine_output_format_default_text() {
611 let cli = Cli::parse_from(["test"]);
612 assert_eq!(determine_output_format(&cli), OutputFormat::Text);
613 }
614
615 #[test]
616 fn test_determine_output_format_explicit_overrides_json_flag() {
617 let cli = Cli::parse_from(["test", "--json", "--format", "html"]);
618 assert_eq!(determine_output_format(&cli), OutputFormat::Html);
619 }
620
621 #[test]
624 fn test_extract_init_metrics_empty() {
625 let m = config::init::extract_init_metrics(0, &[]);
626 assert_eq!(m.file_count, 0);
627 assert_eq!(m.function_count, 0);
628 assert_eq!(m.max_cognitive, 0);
629 }
630
631 #[test]
632 fn test_extract_init_metrics_with_complexity() {
633 let fa = crate::analyzer::FunctionAnalysis {
634 name: "f".into(),
635 file: "test.rs".into(),
636 line: 1,
637 classification: crate::analyzer::Classification::Operation,
638 parent_type: None,
639 suppressed: false,
640 complexity: Some(crate::analyzer::ComplexityMetrics {
641 cognitive_complexity: 12,
642 cyclomatic_complexity: 8,
643 max_nesting: 3,
644 function_lines: 45,
645 ..Default::default()
646 }),
647 qualified_name: "f".into(),
648 severity: None,
649 cognitive_warning: false,
650 cyclomatic_warning: false,
651 nesting_depth_warning: false,
652 function_length_warning: false,
653 unsafe_warning: false,
654 error_handling_warning: false,
655 complexity_suppressed: false,
656 own_calls: vec![],
657 parameter_count: 0,
658 is_trait_impl: false,
659 is_test: false,
660 effort_score: None,
661 };
662 let results = vec![fa];
663 let m = config::init::extract_init_metrics(5, &results);
664 assert_eq!(m.file_count, 5);
665 assert_eq!(m.function_count, 1);
666 assert_eq!(m.max_cognitive, 12);
667 assert_eq!(m.max_cyclomatic, 8);
668 assert_eq!(m.max_nesting_depth, 3);
669 assert_eq!(m.max_function_lines, 45);
670 }
671
672 #[test]
675 fn test_diff_cli_default_ref() {
676 let cli = Cli::parse_from(["test", "--diff"]);
677 assert_eq!(cli.diff.as_deref(), Some("HEAD"));
678 }
679
680 #[test]
681 fn test_diff_cli_custom_ref() {
682 let cli = Cli::parse_from(["test", "--diff", "main"]);
683 assert_eq!(cli.diff.as_deref(), Some("main"));
684 }
685
686 #[test]
687 fn test_diff_cli_not_set() {
688 let cli = Cli::parse_from(["test"]);
689 assert!(cli.diff.is_none());
690 }
691}