1use std::process::ExitCode;
2
3use fallow_config::OutputFormat;
4
5pub fn emit_error(message: &str, exit_code: u8, output: OutputFormat) -> ExitCode {
8 emit_error_with_style(message, exit_code, output, requested_json_style())
9}
10
11fn requested_json_style() -> crate::json_style::JsonStyle {
12 if std::env::args_os().any(|arg| arg == "--pretty") {
13 crate::json_style::JsonStyle::Pretty
14 } else {
15 crate::json_style::JsonStyle::Compact
16 }
17}
18
19#[expect(
20 clippy::print_stdout,
21 clippy::print_stderr,
22 reason = "structured error emission for CLI surfaces"
23)]
24pub fn emit_error_with_style(
25 message: &str,
26 exit_code: u8,
27 output: OutputFormat,
28 json_style: crate::json_style::JsonStyle,
29) -> ExitCode {
30 if matches!(output, OutputFormat::Json) {
31 let error_obj = fallow_output::ErrorOutput::new(message, exit_code);
32 if let Ok(json) = json_style.serialize(&error_obj) {
33 println!("{json}");
34 }
35 } else {
36 eprintln!("Error: {message}");
37 }
38 ExitCode::from(exit_code)
39}