xtask_lib/tasks/fmt.rs
1//! `cargo xtask fmt` — check or apply rustfmt formatting.
2//!
3//! In check mode (the default, used in CI) the task exits non-zero if any
4//! file would be reformatted. With `--apply` the formatting is written back
5//! to disk.
6
7use anyhow::Result;
8use clap::Args;
9
10use super::util::{cmd, workspace};
11
12/// Arguments for `cargo xtask fmt`.
13#[derive(Debug, Args)]
14pub struct FmtArgs {
15 /// Apply formatting instead of checking.
16 ///
17 /// Without this flag the command runs `cargo fmt --all --check`, which is
18 /// suitable for CI. Pass `--apply` to reformat files in place.
19 #[arg(long)]
20 pub apply: bool,
21}
22
23/// Runs `cargo fmt` across the entire workspace.
24///
25/// # Errors
26///
27/// Returns an error if `cargo fmt` exits with a non-zero status (i.e. files
28/// are not formatted when running in check mode).
29pub fn run(args: FmtArgs) -> Result<()> {
30 let root = workspace::root()?;
31
32 let mut fmt_args = vec!["fmt", "--all"];
33 if !args.apply {
34 fmt_args.push("--check");
35 }
36
37 cmd::run("cargo", &fmt_args, &root, &[])
38}