Skip to content

Commit bf87d9b

Browse files
committed
Rename WorkspaceHitlist to CargoFmtStrategy
1 parent fbe06c6 commit bf87d9b

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/bin/cargo-fmt.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ fn execute() -> i32 {
8484
return success;
8585
}
8686

87-
let workspace_hitlist = WorkspaceHitlist::from_matches(&matches);
87+
let strategy = CargoFmtStrategy::from_matches(&matches);
8888

89-
match format_crate(verbosity, &workspace_hitlist) {
89+
match format_crate(verbosity, &strategy) {
9090
Err(e) => {
9191
print_usage_to_stderr(&opts, &e.to_string());
9292
failure
@@ -127,9 +127,9 @@ pub enum Verbosity {
127127

128128
fn format_crate(
129129
verbosity: Verbosity,
130-
workspace_hitlist: &WorkspaceHitlist,
130+
strategy: &CargoFmtStrategy,
131131
) -> Result<ExitStatus, io::Error> {
132-
let targets = get_targets(workspace_hitlist)?;
132+
let targets = get_targets(strategy)?;
133133

134134
// Currently only bin and lib files get formatted
135135
let files: Vec<_> = targets
@@ -227,37 +227,33 @@ impl Hash for Target {
227227
}
228228

229229
#[derive(Debug, PartialEq, Eq)]
230-
pub enum WorkspaceHitlist {
230+
pub enum CargoFmtStrategy {
231+
/// Format every packages and dependencies.
231232
All,
233+
/// Format pacakges that are specified by the command line argument.
232234
Some(Vec<String>),
233-
None,
235+
/// Format the root packages only.
236+
Root,
234237
}
235238

236-
impl WorkspaceHitlist {
237-
pub fn get_some(&self) -> Option<&[String]> {
238-
if let WorkspaceHitlist::Some(ref hitlist) = *self {
239-
Some(hitlist)
240-
} else {
241-
None
242-
}
243-
}
244-
245-
pub fn from_matches(matches: &Matches) -> WorkspaceHitlist {
239+
impl CargoFmtStrategy {
240+
pub fn from_matches(matches: &Matches) -> CargoFmtStrategy {
246241
match (matches.opt_present("all"), matches.opt_present("p")) {
247-
(false, false) => WorkspaceHitlist::None,
248-
(true, _) => WorkspaceHitlist::All,
249-
(false, true) => WorkspaceHitlist::Some(matches.opt_strs("p")),
242+
(false, false) => CargoFmtStrategy::Root,
243+
(true, _) => CargoFmtStrategy::All,
244+
(false, true) => CargoFmtStrategy::Some(matches.opt_strs("p")),
250245
}
251246
}
252247
}
253248

254-
fn get_targets(workspace_hitlist: &WorkspaceHitlist) -> Result<HashSet<Target>, io::Error> {
249+
/// Based on the specified CargoFmtStrategy, returns a set of main source files.
250+
fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error> {
255251
let mut targets = HashSet::new();
256252

257-
match *workspace_hitlist {
258-
WorkspaceHitlist::None => get_targets_root_only(&mut targets)?,
259-
WorkspaceHitlist::All => get_targets_recursive(None, &mut targets, &mut HashSet::new())?,
260-
WorkspaceHitlist::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
253+
match *strategy {
254+
CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?,
255+
CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut HashSet::new())?,
256+
CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
261257
}
262258

263259
if targets.is_empty() {
@@ -317,25 +313,25 @@ fn get_targets_recursive(
317313
}
318314

319315
fn get_targets_with_hitlist(
320-
target_names: &[String],
316+
hitlist: &[String],
321317
targets: &mut HashSet<Target>,
322318
) -> Result<(), io::Error> {
323319
let metadata = get_cargo_metadata(None)?;
324320

325-
let mut hitlist: HashSet<&String> = HashSet::from_iter(target_names);
321+
let mut workspace_hitlist: HashSet<&String> = HashSet::from_iter(hitlist);
326322

327323
for package in metadata.packages {
328324
for target in package.targets {
329-
if hitlist.remove(&target.name) {
325+
if workspace_hitlist.remove(&target.name) {
330326
targets.insert(Target::from_target(&target));
331327
}
332328
}
333329
}
334330

335-
if hitlist.is_empty() {
331+
if workspace_hitlist.is_empty() {
336332
Ok(())
337333
} else {
338-
let package = hitlist.iter().next().unwrap();
334+
let package = workspace_hitlist.iter().next().unwrap();
339335
Err(io::Error::new(
340336
io::ErrorKind::InvalidInput,
341337
format!("package `{}` is not a member of the workspace", package),

0 commit comments

Comments
 (0)