foch_cli/cli/handler/check/
mod.rs1use crate::cli::arg::{AnalysisModeArg, CheckArgs, CheckChannelArg, CheckOutputFormat};
2use crate::cli::handler::{HandlerResult, resolve_input_source};
3use foch::check::run_checks_with_options;
4use foch::game::eu4::analysis::report::render_text;
5use foch::input::{CheckOptions, Config, InputRequest};
6use foch::model::{AnalysisMode, ChannelMode, CheckResult};
7
8pub fn handle_check(check_args: &CheckArgs, config: Config) -> HandlerResult {
9 let request = InputRequest::new(
10 resolve_input_source(check_args.playset_path.as_deref(), &config)?,
11 config,
12 );
13 let run_options = CheckOptions {
14 analysis_mode: to_analysis_mode(check_args.analysis_mode),
15 channel_mode: to_channel_mode(check_args.channel),
16 include_game_base: !check_args.no_game_base,
17 };
18
19 let result = run_checks_with_options(request, run_options.clone());
20 if let Some(path) = check_args.parse_issue_report.as_ref() {
21 std::fs::write(
22 path,
23 serde_json::to_string_pretty(&result.parse_issue_report)?,
24 )?;
25 }
26
27 let output_result = select_output_result(&result, run_options.channel_mode);
28 let rendered = match check_args.format {
29 CheckOutputFormat::Text => render_text(
30 &output_result,
31 !check_args.no_color,
32 run_options.channel_mode,
33 ),
34 CheckOutputFormat::Json => serde_json::to_string_pretty(&output_result)?,
35 };
36
37 if let Some(path) = check_args.output.as_ref() {
38 std::fs::write(path, rendered)?;
39 println!("check result written to: {}", path.display());
40 } else {
41 println!("{rendered}");
42 }
43
44 if result.has_fatal_errors() {
45 return Ok(1);
46 }
47
48 if check_args.strict && result.has_strict_findings() {
49 return Ok(2);
50 }
51
52 Ok(0)
53}
54
55fn select_output_result(result: &CheckResult, channel_mode: ChannelMode) -> CheckResult {
56 let mut output = result.clone();
57 output.findings = result.filtered_findings(channel_mode);
58 if channel_mode == ChannelMode::Strict {
59 output.advisory_findings.clear();
60 }
61 output
62}
63
64fn to_analysis_mode(mode: AnalysisModeArg) -> AnalysisMode {
65 match mode {
66 AnalysisModeArg::Basic => AnalysisMode::Basic,
67 AnalysisModeArg::Semantic => AnalysisMode::Semantic,
68 }
69}
70
71fn to_channel_mode(channel: CheckChannelArg) -> ChannelMode {
72 match channel {
73 CheckChannelArg::Strict => ChannelMode::Strict,
74 CheckChannelArg::All => ChannelMode::All,
75 }
76}