use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use clap::Args;
use rayon::prelude::*;
use super::io::{self, gather_inputs};
use super::{Cli, exit};
#[derive(Args, Debug)]
pub struct FormatArgs {
paths: Vec<PathBuf>,
#[arg(long, value_name = "PATH", conflicts_with = "write")]
stdin_filename: Option<PathBuf>,
#[arg(long, conflicts_with = "check")]
write: bool,
#[arg(long)]
check: bool,
#[arg(long)]
quiet: bool,
}
pub fn run(args: &FormatArgs, cli: &Cli) -> Result<u8> {
let reads_stdin = io::reads_stdin(&args.paths);
if args.write && reads_stdin {
bail!("--write requires file inputs, not stdin");
}
let anchor = if let Some(filename) = &args.stdin_filename {
if !reads_stdin {
bail!("--stdin-filename requires stdin input");
}
let absolute = std::path::absolute(filename)?;
absolute.parent().unwrap_or(&absolute).to_path_buf()
} else {
io::discovery_anchor(&args.paths)
};
let config = cli.load_config(&anchor)?;
let mut inputs = gather_inputs(&args.paths)?;
if let (Some(filename), [input]) = (&args.stdin_filename, inputs.as_mut_slice()) {
input.label = filename.display().to_string();
}
if !args.write && !args.check && inputs.len() > 1 {
bail!("stdout formatting requires one input. Use --write or --check for multiple files");
}
let candidates: Vec<_> = inputs
.par_iter()
.map(|i| config.try_format(&i.text))
.collect();
let formatted = inputs
.iter()
.zip(candidates)
.map(|(input, result)| result.with_context(|| format!("cannot format {}", input.label)))
.collect::<Result<Vec<_>>>()?;
let mut unformatted = 0usize;
for (input, formatted) in inputs.iter().zip(&formatted) {
if args.check {
if *formatted != input.text {
unformatted += 1;
if !args.quiet {
eprintln!("would reformat: {}", input.label);
}
}
continue;
}
if let Some(path) = input.path.as_ref().filter(|_| args.write) {
if *formatted != input.text {
std::fs::write(path, formatted)?;
}
} else {
print!("{formatted}");
}
}
if args.check && unformatted > 0 {
if !args.quiet {
eprintln!("{unformatted} file(s) need formatting");
}
return Ok(exit::FINDINGS);
}
Ok(exit::OK)
}