@@ -24,7 +24,7 @@ use std::str;
24
24
use std:: collections:: HashSet ;
25
25
use std:: iter:: FromIterator ;
26
26
27
- use getopts:: Options ;
27
+ use getopts:: { Options , Matches } ;
28
28
use rustc_serialize:: json:: Json ;
29
29
30
30
fn main ( ) {
@@ -70,11 +70,7 @@ fn execute() -> i32 {
70
70
return success;
71
71
}
72
72
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) ;
78
74
79
75
match format_crate ( verbosity, workspace_hitlist) {
80
76
Err ( e) => {
@@ -154,41 +150,35 @@ pub struct Target {
154
150
kind : TargetKind ,
155
151
}
156
152
157
- #[ derive( Debug ) ]
153
+ #[ derive( Debug , PartialEq , Eq ) ]
158
154
pub enum WorkspaceHitlist {
159
155
All ,
160
156
Some ( Vec < String > ) ,
161
157
None ,
162
158
}
163
159
164
160
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
176
166
}
177
167
}
178
168
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" ) ) ,
184
174
}
185
175
}
186
176
}
187
177
188
178
// Returns a vector of all compile targets of a crate
189
179
fn get_targets ( workspace_hitlist : WorkspaceHitlist ) -> Result < Vec < Target > , std:: io:: Error > {
190
180
let mut targets: Vec < Target > = vec ! [ ] ;
191
- if workspace_hitlist. is_none ( ) {
181
+ if workspace_hitlist == WorkspaceHitlist :: None {
192
182
let output = try!( Command :: new ( "cargo" ) . arg ( "read-manifest" ) . output ( ) ) ;
193
183
if output. status . success ( ) {
194
184
// 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::
210
200
// This happens when cargo-fmt is not used inside a crate or
211
201
// is used inside a workspace.
212
202
// 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
214
204
let output = Command :: new ( "cargo" ) . arg ( "metadata" )
215
205
. arg ( "--no-deps" )
216
206
. output ( ) ?;
217
207
if output. status . success ( ) {
218
208
let data = & String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
219
209
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 {
221
211
HashSet :: from_iter ( workspace_hitlist. get_some ( ) . unwrap ( ) )
222
212
} else {
223
213
HashSet :: new ( ) // Unused
@@ -227,24 +217,20 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
227
217
. as_array ( )
228
218
. unwrap ( )
229
219
. into_iter ( )
230
- . filter ( |member| if workspace_hitlist. is_all ( ) {
220
+ . filter ( |member| if workspace_hitlist == WorkspaceHitlist :: All {
231
221
true
232
222
} else {
233
223
let member_name = member. find ( "name" )
234
224
. unwrap ( )
235
225
. as_string ( )
236
226
. 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 ( )
242
228
} )
243
229
. collect ( ) ;
244
230
if hitlist. len ( ) != 0 {
245
231
// Mimick cargo of only outputting one <package> spec.
246
232
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 " ,
248
234
hitlist. iter( ) . next( ) . unwrap( ) ) ) ) ;
249
235
}
250
236
for member in members {
0 commit comments