use crate::args::FormatArgs;
use crate::utils::{print_diagnostics, read_input};
use std::fs;
pub fn handle_format(args: FormatArgs) {
if args.in_place && args.file.is_none() {
eprintln!("Error: Cannot perform in-place formatting when reading from standard input.");
std::process::exit(1);
}
if args.in_place && args.output.is_some() {
eprintln!("Error: Cannot specify both --in-place and --output file.");
std::process::exit(1);
}
let (input, label) = match read_input(&args.file) {
Ok(res) => res,
Err(e) => {
eprintln!("Error reading input: {}", e);
std::process::exit(1);
}
};
let mut settings = jsonette::get_settings();
if let Some(sort_keys) = args.sort_keys {
settings.format.sort_keys = sort_keys;
}
if let Some(use_tabs) = args.use_tabs {
settings.format.use_tabs = use_tabs;
}
if let Some(indent) = args.indent {
settings.format.indent = indent;
}
if let Some(line_ending) = &args.line_ending {
settings.format.line_ending = match line_ending.as_str() {
"lf" => jsonette::LineEnding::LF,
"crlf" => jsonette::LineEnding::CRLF,
_ => unreachable!(),
};
}
if let Some(folding_style) = &args.folding_style {
settings.format.folding_style = match folding_style.as_str() {
"expanded" => jsonette::FoldingStyle::Expanded,
"compact" => jsonette::FoldingStyle::Compact,
_ => unreachable!(),
};
}
jsonette::set_in_memory_settings(settings);
let node = match jsonette::parse(&input) {
Ok(node) => node,
Err(diags) => {
print_diagnostics(&input, &diags, &label);
std::process::exit(1);
}
};
let output = if args.minify {
jsonette::minify(&node)
} else {
jsonette::format(&node)
};
if args.in_place {
let path = args.file.as_ref().unwrap();
let mut final_output = output;
if !args.minify && !final_output.ends_with('\n') {
final_output.push('\n');
}
if let Err(e) = fs::write(path, &final_output) {
eprintln!("Error writing formatted file: {}", e);
std::process::exit(1);
}
} else if let Some(out_path) = &args.output {
let mut final_output = output;
if !args.minify && !final_output.ends_with('\n') {
final_output.push('\n');
}
if let Err(e) = fs::write(out_path, &final_output) {
eprintln!("Error writing output file: {}", e);
std::process::exit(1);
}
} else {
print!("{}", output);
if !args.minify && !output.ends_with('\n') {
println!();
}
}
}