@@ -8,24 +8,23 @@ extern crate getopts;
8
8
#[ macro_use]
9
9
extern crate log;
10
10
11
+ use cargo:: {
12
+ core:: { compiler:: CompileMode , Package , PackageId , Source , SourceId , Workspace } ,
13
+ exit_with_error,
14
+ ops:: { compile, CompileOptions } ,
15
+ util:: {
16
+ config:: Config , important_paths:: find_root_manifest_for_wd, CargoError , CargoResult ,
17
+ CliError ,
18
+ } ,
19
+ } ;
11
20
use crates_io:: { Crate , Registry } ;
12
-
13
- use cargo:: exit_with_error;
14
- use cargo:: core:: { Package , PackageId , Source , SourceId , Workspace } ;
15
- use cargo:: core:: compiler:: CompileMode ;
16
- use cargo:: ops:: { compile, CompileOptions } ;
17
- use cargo:: util:: { CargoError , CargoResult , CliError } ;
18
- use cargo:: util:: config:: Config ;
19
- use cargo:: util:: important_paths:: find_root_manifest_for_wd;
20
-
21
21
use getopts:: { Matches , Options } ;
22
-
23
- use std:: env;
24
- use std:: error;
25
- use std:: fmt;
26
- use std:: io:: Write ;
27
- use std:: path:: PathBuf ;
28
- use std:: process:: { Stdio , Command } ;
22
+ use std:: {
23
+ env, error, fmt,
24
+ io:: Write ,
25
+ path:: PathBuf ,
26
+ process:: { Command , Stdio } ,
27
+ } ;
29
28
30
29
/// Very simple error representation.
31
30
#[ derive( Debug ) ]
@@ -51,15 +50,18 @@ fn exact_search(query: &str) -> CargoResult<Crate> {
51
50
52
51
registry
53
52
. search ( query, 1 )
54
- . map_err ( |e|
55
- Error ( format ! ( "failed to retrieve search results from the registry: {}" , e) )
56
- . into ( ) )
53
+ . map_err ( |e| {
54
+ Error ( format ! (
55
+ "failed to retrieve search results from the registry: {}" ,
56
+ e
57
+ ) )
58
+ . into ( )
59
+ } )
57
60
. and_then ( |( mut crates, _) | {
58
61
crates
59
62
. drain ( ..)
60
63
. find ( |krate| krate. name == query)
61
- . ok_or_else ( || Error ( format ! ( "failed to find a matching crate `{}`" , query) )
62
- . into ( ) )
64
+ . ok_or_else ( || Error ( format ! ( "failed to find a matching crate `{}`" , query) ) . into ( ) )
63
65
} )
64
66
}
65
67
@@ -87,9 +89,9 @@ impl<'a> SourceInfo<'a> {
87
89
88
90
debug ! ( "source id loaded: {:?}" , source_id) ;
89
91
90
- Ok ( SourceInfo {
92
+ Ok ( Self {
91
93
id : source_id,
92
- source : source ,
94
+ source,
93
95
} )
94
96
}
95
97
}
@@ -114,34 +116,33 @@ impl<'a> WorkInfo<'a> {
114
116
let workspace = Workspace :: new ( & manifest_path, config) ?;
115
117
let package = workspace. load ( & manifest_path) ?;
116
118
117
- Ok ( WorkInfo {
118
- package,
119
- workspace,
120
- } )
119
+ Ok ( Self { package, workspace } )
121
120
}
122
121
123
122
/// Construct a package/workspace pair by fetching the package of a specified name and
124
123
/// version.
125
- fn remote ( config : & ' a Config , source : & mut SourceInfo < ' a > , info : & NameAndVersion )
126
- -> CargoResult < WorkInfo < ' a > >
127
- {
124
+ fn remote (
125
+ config : & ' a Config ,
126
+ source : & mut SourceInfo < ' a > ,
127
+ info : & NameAndVersion ,
128
+ ) -> CargoResult < WorkInfo < ' a > > {
128
129
// TODO: fall back to locally cached package instance, or better yet, search for it
129
130
// first.
130
131
let package_id = PackageId :: new ( info. name , info. version , & source. id ) ?;
131
132
debug ! ( "(remote) package id: {:?}" , package_id) ;
132
133
let package = source. source . download ( & package_id) ?;
133
134
let workspace = Workspace :: ephemeral ( package. clone ( ) , config, None , false ) ?;
134
135
135
- Ok ( WorkInfo {
136
- package : package,
137
- workspace : workspace,
138
- } )
136
+ Ok ( Self { package, workspace } )
139
137
}
140
138
141
139
/// Obtain the paths to the produced rlib and the dependency output directory.
142
- fn rlib_and_dep_output ( & self , config : & ' a Config , name : & str , current : bool )
143
- -> CargoResult < ( PathBuf , PathBuf ) >
144
- {
140
+ fn rlib_and_dep_output (
141
+ & self ,
142
+ config : & ' a Config ,
143
+ name : & str ,
144
+ current : bool ,
145
+ ) -> CargoResult < ( PathBuf , PathBuf ) > {
145
146
let opts = CompileOptions :: new ( config, CompileMode :: Build ) . unwrap ( ) ;
146
147
147
148
if current {
@@ -161,7 +162,6 @@ impl<'a> WorkInfo<'a> {
161
162
162
163
Ok ( ( rlib. 1 . clone ( ) , compilation. deps_output ) )
163
164
}
164
-
165
165
}
166
166
167
167
/// Perform the heavy lifting.
@@ -170,7 +170,6 @@ impl<'a> WorkInfo<'a> {
170
170
/// and/or defaults, and dispatch the actual analysis.
171
171
// TODO: possibly reduce the complexity by finding where some info can be taken from directly
172
172
fn do_main ( config : & Config , matches : & Matches , explain : bool ) -> CargoResult < ( ) > {
173
- debug ! ( "running cargo-semver" ) ;
174
173
fn parse_arg ( opt : & str ) -> CargoResult < NameAndVersion > {
175
174
let mut split = opt. split ( ':' ) ;
176
175
let name = if let Some ( n) = split. next ( ) {
@@ -188,9 +187,11 @@ fn do_main(config: &Config, matches: &Matches, explain: bool) -> CargoResult<()>
188
187
return Err ( Error ( "spec has to be of form `name:version`" . to_owned ( ) ) . into ( ) ) ;
189
188
}
190
189
191
- Ok ( NameAndVersion { name : name , version : version } )
190
+ Ok ( NameAndVersion { name, version } )
192
191
}
193
192
193
+ debug ! ( "running cargo-semver" ) ;
194
+
194
195
let mut source = SourceInfo :: new ( config) ?;
195
196
196
197
let current = if let Some ( opt) = matches. opt_str ( "C" ) {
@@ -214,7 +215,10 @@ fn do_main(config: &Config, matches: &Matches, explain: bool) -> CargoResult<()>
214
215
( work_info, version)
215
216
} else {
216
217
let stable_crate = exact_search ( & name) ?;
217
- let info = NameAndVersion { name : & name, version : & stable_crate. max_version } ;
218
+ let info = NameAndVersion {
219
+ name : & name,
220
+ version : & stable_crate. max_version ,
221
+ } ;
218
222
let work_info = WorkInfo :: remote ( config, & mut source, & info) ?;
219
223
220
224
( work_info, stable_crate. max_version . clone ( ) )
@@ -224,11 +228,13 @@ fn do_main(config: &Config, matches: &Matches, explain: bool) -> CargoResult<()>
224
228
let ( stable_rlib, stable_deps_output) = stable. rlib_and_dep_output ( config, & name, false ) ?;
225
229
226
230
if matches. opt_present ( "d" ) {
227
- println ! ( "--extern old={} -L{} --extern new={} -L{}" ,
228
- stable_rlib. display( ) ,
229
- stable_deps_output. display( ) ,
230
- current_rlib. display( ) ,
231
- current_deps_output. display( ) ) ;
231
+ println ! (
232
+ "--extern old={} -L{} --extern new={} -L{}" ,
233
+ stable_rlib. display( ) ,
234
+ stable_deps_output. display( ) ,
235
+ current_rlib. display( ) ,
236
+ current_deps_output. display( )
237
+ ) ;
232
238
return Ok ( ( ) ) ;
233
239
}
234
240
@@ -250,10 +256,12 @@ fn do_main(config: &Config, matches: &Matches, explain: bool) -> CargoResult<()>
250
256
if let Some ( ref mut stdin) = child. stdin {
251
257
// The order of the `extern crate` declaration is important here: it will later
252
258
// be used to select the `old` and `new` crates.
253
- stdin. write_fmt ( format_args ! ( "#[allow(unused_extern_crates)] \
254
- extern crate old; \
255
- #[allow(unused_extern_crates)] \
256
- extern crate new;") ) ?;
259
+ stdin. write_fmt ( format_args ! (
260
+ "#[allow(unused_extern_crates)] \
261
+ extern crate old; \
262
+ #[allow(unused_extern_crates)] \
263
+ extern crate new;"
264
+ ) ) ?;
257
265
} else {
258
266
return Err ( Error ( "could not pipe to rustc (wtf?)" . to_owned ( ) ) . into ( ) ) ;
259
267
}
@@ -296,12 +304,30 @@ fn main() {
296
304
opts. optflag ( "V" , "version" , "print version information and exit" ) ;
297
305
opts. optflag ( "e" , "explain" , "print detailed error explanations" ) ;
298
306
opts. optflag ( "d" , "debug" , "print command to debug and exit" ) ;
299
- opts. optopt ( "s" , "stable-path" , "use local path as stable/old crate" , "PATH" ) ;
300
- opts. optopt ( "c" , "current-path" , "use local path as current/new crate" , "PATH" ) ;
301
- opts. optopt ( "S" , "stable-pkg" , "use a `name:version` string as stable/old crate" ,
302
- "NAME:VERSION" ) ;
303
- opts. optopt ( "C" , "current-pkg" , "use a `name:version` string as current/new crate" ,
304
- "NAME:VERSION" ) ;
307
+ opts. optopt (
308
+ "s" ,
309
+ "stable-path" ,
310
+ "use local path as stable/old crate" ,
311
+ "PATH" ,
312
+ ) ;
313
+ opts. optopt (
314
+ "c" ,
315
+ "current-path" ,
316
+ "use local path as current/new crate" ,
317
+ "PATH" ,
318
+ ) ;
319
+ opts. optopt (
320
+ "S" ,
321
+ "stable-pkg" ,
322
+ "use a `name:version` string as stable/old crate" ,
323
+ "NAME:VERSION" ,
324
+ ) ;
325
+ opts. optopt (
326
+ "C" ,
327
+ "current-pkg" ,
328
+ "use a `name:version` string as current/new crate" ,
329
+ "NAME:VERSION" ,
330
+ ) ;
305
331
306
332
let config = match Config :: default ( ) {
307
333
Ok ( cfg) => cfg,
@@ -323,15 +349,17 @@ fn main() {
323
349
return ;
324
350
}
325
351
326
- if ( matches. opt_present ( "s" ) && matches. opt_present ( "S" ) ) ||
327
- matches. opt_count ( "s" ) > 1 || matches. opt_count ( "S" ) > 1
352
+ if ( matches. opt_present ( "s" ) && matches. opt_present ( "S" ) )
353
+ || matches. opt_count ( "s" ) > 1
354
+ || matches. opt_count ( "S" ) > 1
328
355
{
329
356
let msg = "at most one of `-s,--stable-path` and `-S,--stable-pkg` allowed" ;
330
357
err ( & config, Error ( msg. to_owned ( ) ) . into ( ) ) ;
331
358
}
332
359
333
- if ( matches. opt_present ( "c" ) && matches. opt_present ( "C" ) ) ||
334
- matches. opt_count ( "c" ) > 1 || matches. opt_count ( "C" ) > 1
360
+ if ( matches. opt_present ( "c" ) && matches. opt_present ( "C" ) )
361
+ || matches. opt_count ( "c" ) > 1
362
+ || matches. opt_count ( "C" ) > 1
335
363
{
336
364
let msg = "at most one of `-c,--current-path` and `-C,--current-pkg` allowed" ;
337
365
err ( & config, Error ( msg. to_owned ( ) ) . into ( ) ) ;
0 commit comments