1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::types::Config;
use clap::{Args, Parser};
use std::num::NonZeroUsize;
use std::path::PathBuf;
#[derive(Debug, Parser)]
#[command(
name = "cargo-ff",
bin_name = "cargo ff",
about = "Fast Format is a fast drop-in replacement for cargo fmt",
version
)]
#[non_exhaustive]
pub struct Cli {
/// Run rustfmt in --check mode (exit non-zero if any file would change).
#[arg(long)]
pub check: bool,
/// Format every workspace member, regardless of which package
/// `--manifest-path` (or the current directory) implicitly selects.
/// Mirrors `cargo fmt --all`.
#[arg(long)]
pub all: bool,
/// Format only the given workspace member(s). Can be repeated.
#[arg(short = 'p', long = "package")]
pub packages: Vec<String>,
/// Path to the workspace's Cargo.toml (forwarded to `cargo metadata`).
#[arg(long)]
pub manifest_path: Option<PathBuf>,
/// Extra arguments forwarded to rustfmt (after `--`).
#[arg(last = true)]
pub rustfmt_args: Vec<String>,
#[command(flatten)]
pub ff: FfArgs,
}
/// cargo-ff-specific flags. All long names are prefixed `--ff-*` so they
/// can never collide with a flag added upstream by `cargo fmt`.
#[derive(Debug, Args)]
#[non_exhaustive]
pub struct FfArgs {
/// Number of worker threads. Defaults to `available_parallelism()`.
#[arg(long = "ff-workers")]
pub workers: Option<NonZeroUsize>,
/// Bounded-channel capacity. Default 512. Hidden — benchmarking knob.
#[arg(long = "ff-channel-capacity", hide = true)]
pub channel_capacity: Option<usize>,
/// Crates per rustfmt invocation. Higher amortizes spawn cost; lower
/// gives finer scheduling granularity. Hidden — benchmarking knob.
#[arg(long = "ff-batch-size", hide = true)]
pub batch_size: Option<usize>,
/// Experimental: skip rustfmt for crates whose `*.rs` mtimes match
/// the prior successful run. May produce stale results if files
/// outside `manifest_dir` are pulled in via `#[path]`.
#[arg(long = "ff-experimental-cache", hide = true)]
pub experimental_cache: bool,
/// Emit advisory warnings to stderr (off by default).
#[arg(long = "ff-warnings")]
pub warnings: bool,
}
impl Cli {
/// Parse argv, stripping the cargo-subcommand `argv[1] == "ff"` if present.
#[must_use]
pub fn parse_argv() -> Self {
let mut args: Vec<std::ffi::OsString> = std::env::args_os().collect();
if args.len() >= 2 && args[1] == "ff" {
args.remove(1);
}
Self::parse_from(args)
}
pub fn into_config(self) -> Config {
Config {
manifest_path: self.manifest_path,
packages: self.packages,
all: self.all,
check: self.check,
rustfmt_args: self.rustfmt_args,
workers: self.ff.workers.map(NonZeroUsize::get),
channel_capacity: self.ff.channel_capacity,
batch_size: self.ff.batch_size,
experimental_cache: self.ff.experimental_cache,
warnings: self.ff.warnings,
}
}
}