@@ -85,19 +85,6 @@ impl PackageIdSpec {
85
85
} )
86
86
}
87
87
88
- /// Roughly equivalent to `PackageIdSpec::parse(spec)?.query(i)`
89
- pub fn query_str < I > ( spec : & str , i : I ) -> CargoResult < PackageId >
90
- where
91
- I : IntoIterator < Item = PackageId > ,
92
- {
93
- let i: Vec < _ > = i. into_iter ( ) . collect ( ) ;
94
- let spec = PackageIdSpec :: parse ( spec) . with_context ( || {
95
- let suggestion = edit_distance:: closest_msg ( spec, i. iter ( ) , |id| id. name ( ) . as_str ( ) ) ;
96
- format ! ( "invalid package ID specification: `{}`{}" , spec, suggestion)
97
- } ) ?;
98
- spec. query ( i)
99
- }
100
-
101
88
/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
102
89
/// fields filled in.
103
90
pub fn from_package_id ( package_id : PackageId ) -> PackageIdSpec {
@@ -221,9 +208,94 @@ impl PackageIdSpec {
221
208
pub fn set_kind ( & mut self , kind : SourceKind ) {
222
209
self . kind = Some ( kind) ;
223
210
}
211
+ }
212
+
213
+ fn strip_url_protocol ( url : & Url ) -> Url {
214
+ // Ridiculous hoop because `Url::set_scheme` errors when changing to http/https
215
+ let raw = url. to_string ( ) ;
216
+ raw. split_once ( '+' ) . unwrap ( ) . 1 . parse ( ) . unwrap ( )
217
+ }
218
+
219
+ impl fmt:: Display for PackageIdSpec {
220
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
221
+ let mut printed_name = false ;
222
+ match self . url {
223
+ Some ( ref url) => {
224
+ if let Some ( protocol) = self . kind . as_ref ( ) . and_then ( |k| k. protocol ( ) ) {
225
+ write ! ( f, "{protocol}+" ) ?;
226
+ }
227
+ write ! ( f, "{}" , url) ?;
228
+ if let Some ( SourceKind :: Git ( git_ref) ) = self . kind . as_ref ( ) {
229
+ if let Some ( pretty) = git_ref. pretty_ref ( true ) {
230
+ write ! ( f, "?{}" , pretty) ?;
231
+ }
232
+ }
233
+ if url. path_segments ( ) . unwrap ( ) . next_back ( ) . unwrap ( ) != & * self . name {
234
+ printed_name = true ;
235
+ write ! ( f, "#{}" , self . name) ?;
236
+ }
237
+ }
238
+ None => {
239
+ printed_name = true ;
240
+ write ! ( f, "{}" , self . name) ?;
241
+ }
242
+ }
243
+ if let Some ( ref v) = self . version {
244
+ write ! ( f, "{}{}" , if printed_name { "@" } else { "#" } , v) ?;
245
+ }
246
+ Ok ( ( ) )
247
+ }
248
+ }
249
+
250
+ impl ser:: Serialize for PackageIdSpec {
251
+ fn serialize < S > ( & self , s : S ) -> Result < S :: Ok , S :: Error >
252
+ where
253
+ S : ser:: Serializer ,
254
+ {
255
+ self . to_string ( ) . serialize ( s)
256
+ }
257
+ }
258
+
259
+ impl < ' de > de:: Deserialize < ' de > for PackageIdSpec {
260
+ fn deserialize < D > ( d : D ) -> Result < PackageIdSpec , D :: Error >
261
+ where
262
+ D : de:: Deserializer < ' de > ,
263
+ {
264
+ let string = String :: deserialize ( d) ?;
265
+ PackageIdSpec :: parse ( & string) . map_err ( de:: Error :: custom)
266
+ }
267
+ }
268
+
269
+ pub trait PackageIdSpecQuery {
270
+ /// Roughly equivalent to `PackageIdSpec::parse(spec)?.query(i)`
271
+ fn query_str < I > ( spec : & str , i : I ) -> CargoResult < PackageId >
272
+ where
273
+ I : IntoIterator < Item = PackageId > ;
224
274
225
275
/// Checks whether the given `PackageId` matches the `PackageIdSpec`.
226
- pub fn matches ( & self , package_id : PackageId ) -> bool {
276
+ fn matches ( & self , package_id : PackageId ) -> bool ;
277
+
278
+ /// Checks a list of `PackageId`s to find 1 that matches this `PackageIdSpec`. If 0, 2, or
279
+ /// more are found, then this returns an error.
280
+ fn query < I > ( & self , i : I ) -> CargoResult < PackageId >
281
+ where
282
+ I : IntoIterator < Item = PackageId > ;
283
+ }
284
+
285
+ impl PackageIdSpecQuery for PackageIdSpec {
286
+ fn query_str < I > ( spec : & str , i : I ) -> CargoResult < PackageId >
287
+ where
288
+ I : IntoIterator < Item = PackageId > ,
289
+ {
290
+ let i: Vec < _ > = i. into_iter ( ) . collect ( ) ;
291
+ let spec = PackageIdSpec :: parse ( spec) . with_context ( || {
292
+ let suggestion = edit_distance:: closest_msg ( spec, i. iter ( ) , |id| id. name ( ) . as_str ( ) ) ;
293
+ format ! ( "invalid package ID specification: `{}`{}" , spec, suggestion)
294
+ } ) ?;
295
+ spec. query ( i)
296
+ }
297
+
298
+ fn matches ( & self , package_id : PackageId ) -> bool {
227
299
if self . name ( ) != package_id. name ( ) . as_str ( ) {
228
300
return false ;
229
301
}
@@ -249,9 +321,7 @@ impl PackageIdSpec {
249
321
true
250
322
}
251
323
252
- /// Checks a list of `PackageId`s to find 1 that matches this `PackageIdSpec`. If 0, 2, or
253
- /// more are found, then this returns an error.
254
- pub fn query < I > ( & self , i : I ) -> CargoResult < PackageId >
324
+ fn query < I > ( & self , i : I ) -> CargoResult < PackageId >
255
325
where
256
326
I : IntoIterator < Item = PackageId > ,
257
327
{
@@ -342,65 +412,10 @@ impl PackageIdSpec {
342
412
}
343
413
}
344
414
345
- fn strip_url_protocol ( url : & Url ) -> Url {
346
- // Ridiculous hoop because `Url::set_scheme` errors when changing to http/https
347
- let raw = url. to_string ( ) ;
348
- raw. split_once ( '+' ) . unwrap ( ) . 1 . parse ( ) . unwrap ( )
349
- }
350
-
351
- impl fmt:: Display for PackageIdSpec {
352
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
353
- let mut printed_name = false ;
354
- match self . url {
355
- Some ( ref url) => {
356
- if let Some ( protocol) = self . kind . as_ref ( ) . and_then ( |k| k. protocol ( ) ) {
357
- write ! ( f, "{protocol}+" ) ?;
358
- }
359
- write ! ( f, "{}" , url) ?;
360
- if let Some ( SourceKind :: Git ( git_ref) ) = self . kind . as_ref ( ) {
361
- if let Some ( pretty) = git_ref. pretty_ref ( true ) {
362
- write ! ( f, "?{}" , pretty) ?;
363
- }
364
- }
365
- if url. path_segments ( ) . unwrap ( ) . next_back ( ) . unwrap ( ) != & * self . name {
366
- printed_name = true ;
367
- write ! ( f, "#{}" , self . name) ?;
368
- }
369
- }
370
- None => {
371
- printed_name = true ;
372
- write ! ( f, "{}" , self . name) ?;
373
- }
374
- }
375
- if let Some ( ref v) = self . version {
376
- write ! ( f, "{}{}" , if printed_name { "@" } else { "#" } , v) ?;
377
- }
378
- Ok ( ( ) )
379
- }
380
- }
381
-
382
- impl ser:: Serialize for PackageIdSpec {
383
- fn serialize < S > ( & self , s : S ) -> Result < S :: Ok , S :: Error >
384
- where
385
- S : ser:: Serializer ,
386
- {
387
- self . to_string ( ) . serialize ( s)
388
- }
389
- }
390
-
391
- impl < ' de > de:: Deserialize < ' de > for PackageIdSpec {
392
- fn deserialize < D > ( d : D ) -> Result < PackageIdSpec , D :: Error >
393
- where
394
- D : de:: Deserializer < ' de > ,
395
- {
396
- let string = String :: deserialize ( d) ?;
397
- PackageIdSpec :: parse ( & string) . map_err ( de:: Error :: custom)
398
- }
399
- }
400
-
401
415
#[ cfg( test) ]
402
416
mod tests {
403
417
use super :: PackageIdSpec ;
418
+ use super :: PackageIdSpecQuery ;
404
419
use crate :: core:: { GitReference , PackageId , SourceId , SourceKind } ;
405
420
use url:: Url ;
406
421
0 commit comments