Skip to content

Commit ac997af

Browse files
committed
Improved on the new workspace code for cargo-fmt
1 parent cb36eec commit ac997af

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

src/bin/cargo-fmt.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::str;
2424
use std::collections::HashSet;
2525
use std::iter::FromIterator;
2626

27-
use getopts::Options;
27+
use getopts::{Options, Matches};
2828
use rustc_serialize::json::Json;
2929

3030
fn main() {
@@ -70,11 +70,7 @@ fn execute() -> i32 {
7070
return success;
7171
}
7272

73-
let workspace_hitlist = match (matches.opt_present("all"), matches.opt_present("p")) {
74-
(false, false) => WorkspaceHitlist::None,
75-
(true, _) => WorkspaceHitlist::All,
76-
(false, true) => WorkspaceHitlist::Some(matches.opt_strs("p")),
77-
};
73+
let workspace_hitlist = WorkspaceHitlist::from_matches(&matches);
7874

7975
match format_crate(verbosity, workspace_hitlist) {
8076
Err(e) => {
@@ -154,41 +150,35 @@ pub struct Target {
154150
kind: TargetKind,
155151
}
156152

157-
#[derive(Debug)]
153+
#[derive(Debug, PartialEq, Eq)]
158154
pub enum WorkspaceHitlist {
159155
All,
160156
Some(Vec<String>),
161157
None,
162158
}
163159

164160
impl WorkspaceHitlist {
165-
fn is_all(&self) -> bool {
166-
match *self {
167-
WorkspaceHitlist::All => true,
168-
_ => false,
169-
}
170-
}
171-
172-
fn is_none(&self) -> bool {
173-
match *self {
174-
WorkspaceHitlist::None => true,
175-
_ => false,
161+
pub fn get_some<'a>(&'a self) -> Option<&'a [String]> {
162+
if let &WorkspaceHitlist::Some(ref hitlist) = self {
163+
Some(&hitlist)
164+
} else {
165+
None
176166
}
177167
}
178168

179-
pub fn get_some<'a>(&'a self) -> Option<&'a [String]> {
180-
use std::borrow::Borrow;
181-
match self {
182-
&WorkspaceHitlist::Some(ref hitlist) => Some(hitlist.borrow()),
183-
_ => None,
169+
pub fn from_matches(matches: &Matches) -> WorkspaceHitlist {
170+
match (matches.opt_present("all"), matches.opt_present("p")) {
171+
(false, false) => WorkspaceHitlist::None,
172+
(true, _) => WorkspaceHitlist::All,
173+
(false, true) => WorkspaceHitlist::Some(matches.opt_strs("p")),
184174
}
185175
}
186176
}
187177

188178
// Returns a vector of all compile targets of a crate
189179
fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::io::Error> {
190180
let mut targets: Vec<Target> = vec![];
191-
if workspace_hitlist.is_none() {
181+
if workspace_hitlist == WorkspaceHitlist::None {
192182
let output = try!(Command::new("cargo").arg("read-manifest").output());
193183
if output.status.success() {
194184
// None of the unwraps should fail if output of `cargo read-manifest` is correct
@@ -210,14 +200,14 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
210200
// This happens when cargo-fmt is not used inside a crate or
211201
// is used inside a workspace.
212202
// To ensure backward compatability, we only use `cargo metadata` for workspaces.
213-
// TODO: How do we make sure we use either metadata or read-manifest
203+
// TODO: Is it possible only use metadata or read-manifest
214204
let output = Command::new("cargo").arg("metadata")
215205
.arg("--no-deps")
216206
.output()?;
217207
if output.status.success() {
218208
let data = &String::from_utf8(output.stdout).unwrap();
219209
let json = Json::from_str(data).unwrap();
220-
let mut hitlist: HashSet<&String> = if !workspace_hitlist.is_all() {
210+
let mut hitlist: HashSet<&String> = if workspace_hitlist != WorkspaceHitlist::All {
221211
HashSet::from_iter(workspace_hitlist.get_some().unwrap())
222212
} else {
223213
HashSet::new() // Unused
@@ -227,24 +217,20 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
227217
.as_array()
228218
.unwrap()
229219
.into_iter()
230-
.filter(|member| if workspace_hitlist.is_all() {
220+
.filter(|member| if workspace_hitlist == WorkspaceHitlist::All {
231221
true
232222
} else {
233223
let member_name = member.find("name")
234224
.unwrap()
235225
.as_string()
236226
.unwrap();
237-
if hitlist.take(&member_name.to_string()).is_some() {
238-
true
239-
} else {
240-
false
241-
}
227+
hitlist.take(&member_name.to_string()).is_some()
242228
})
243229
.collect();
244230
if hitlist.len() != 0 {
245231
// Mimick cargo of only outputting one <package> spec.
246232
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput,
247-
format!("Could not find package: {:?}",
233+
format!("package `{}` is not a member of the workspace",
248234
hitlist.iter().next().unwrap())));
249235
}
250236
for member in members {

0 commit comments

Comments
 (0)