@@ -55,6 +55,8 @@ enum SourceKind {
55
55
Path ,
56
56
/// A remote registry.
57
57
Registry ,
58
+ /// A sparse registry.
59
+ SparseRegistry ,
58
60
/// A local filesystem-based registry.
59
61
LocalRegistry ,
60
62
/// A directory-based registry.
@@ -100,6 +102,20 @@ impl SourceId {
100
102
SourceId { inner }
101
103
}
102
104
105
+ fn remote_source_kind ( url : & Url ) -> ( SourceKind , Url ) {
106
+ if url. as_str ( ) . starts_with ( "sparse+" ) {
107
+ let url = url
108
+ . to_string ( )
109
+ . strip_prefix ( "sparse+" )
110
+ . expect ( "we just found that prefix" )
111
+ . into_url ( )
112
+ . expect ( "a valid url without a protocol specifier should still be valid" ) ;
113
+ ( SourceKind :: SparseRegistry , url)
114
+ } else {
115
+ ( SourceKind :: Registry , url. to_owned ( ) )
116
+ }
117
+ }
118
+
103
119
/// Parses a source URL and returns the corresponding ID.
104
120
///
105
121
/// ## Example
@@ -142,8 +158,8 @@ impl SourceId {
142
158
. with_precise ( Some ( "locked" . to_string ( ) ) ) )
143
159
}
144
160
"sparse" => {
145
- let url = string . into_url ( ) ?;
146
- Ok ( SourceId :: new ( SourceKind :: Registry , url, None ) ?
161
+ let url = url . into_url ( ) ?;
162
+ Ok ( SourceId :: new ( SourceKind :: SparseRegistry , url, None ) ?
147
163
. with_precise ( Some ( "locked" . to_string ( ) ) ) )
148
164
}
149
165
"path" => {
@@ -180,12 +196,14 @@ impl SourceId {
180
196
/// Use [`SourceId::for_alt_registry`] if a name can provided, which
181
197
/// generates better messages for cargo.
182
198
pub fn for_registry ( url : & Url ) -> CargoResult < SourceId > {
183
- SourceId :: new ( SourceKind :: Registry , url. clone ( ) , None )
199
+ let ( kind, url) = Self :: remote_source_kind ( url) ;
200
+ SourceId :: new ( kind, url, None )
184
201
}
185
202
186
203
/// Creates a `SourceId` from a remote registry URL with given name.
187
204
pub fn for_alt_registry ( url : & Url , name : & str ) -> CargoResult < SourceId > {
188
- SourceId :: new ( SourceKind :: Registry , url. clone ( ) , Some ( name) )
205
+ let ( kind, url) = Self :: remote_source_kind ( url) ;
206
+ SourceId :: new ( kind, url, Some ( name) )
189
207
}
190
208
191
209
/// Creates a SourceId from a local registry path.
@@ -218,7 +236,7 @@ impl SourceId {
218
236
if Self :: crates_io_is_sparse ( config) ? {
219
237
config. check_registry_index_not_set ( ) ?;
220
238
let url = CRATES_IO_HTTP_INDEX . into_url ( ) . unwrap ( ) ;
221
- SourceId :: new ( SourceKind :: Registry , url, Some ( CRATES_IO_REGISTRY ) )
239
+ SourceId :: new ( SourceKind :: SparseRegistry , url, Some ( CRATES_IO_REGISTRY ) )
222
240
} else {
223
241
Self :: crates_io ( config)
224
242
}
@@ -245,8 +263,9 @@ impl SourceId {
245
263
return Self :: crates_io ( config) ;
246
264
}
247
265
let url = config. get_registry_index ( key) ?;
266
+ let ( kind, url) = Self :: remote_source_kind ( & url) ;
248
267
Ok ( SourceId :: wrap ( SourceIdInner {
249
- kind : SourceKind :: Registry ,
268
+ kind,
250
269
canonical_url : CanonicalUrl :: new ( & url) ?,
251
270
url,
252
271
precise : None ,
@@ -313,16 +332,24 @@ impl SourceId {
313
332
pub fn is_registry ( self ) -> bool {
314
333
matches ! (
315
334
self . inner. kind,
316
- SourceKind :: Registry | SourceKind :: LocalRegistry
335
+ SourceKind :: Registry | SourceKind :: SparseRegistry | SourceKind :: LocalRegistry
317
336
)
318
337
}
319
338
339
+ /// Returns `true` if this source is from a sparse registry.
340
+ pub fn is_sparse ( self ) -> bool {
341
+ matches ! ( self . inner. kind, SourceKind :: SparseRegistry )
342
+ }
343
+
320
344
/// Returns `true` if this source is a "remote" registry.
321
345
///
322
346
/// "remote" may also mean a file URL to a git index, so it is not
323
347
/// necessarily "remote". This just means it is not `local-registry`.
324
348
pub fn is_remote_registry ( self ) -> bool {
325
- matches ! ( self . inner. kind, SourceKind :: Registry )
349
+ matches ! (
350
+ self . inner. kind,
351
+ SourceKind :: Registry | SourceKind :: SparseRegistry
352
+ )
326
353
}
327
354
328
355
/// Returns `true` if this source from a Git repository.
@@ -346,11 +373,9 @@ impl SourceId {
346
373
} ;
347
374
Ok ( Box :: new ( PathSource :: new ( & path, self , config) ) )
348
375
}
349
- SourceKind :: Registry => Ok ( Box :: new ( RegistrySource :: remote (
350
- self ,
351
- yanked_whitelist,
352
- config,
353
- ) ?) ) ,
376
+ SourceKind :: Registry | SourceKind :: SparseRegistry => Ok ( Box :: new (
377
+ RegistrySource :: remote ( self , yanked_whitelist, config) ?,
378
+ ) ) ,
354
379
SourceKind :: LocalRegistry => {
355
380
let path = match self . inner . url . to_file_path ( ) {
356
381
Ok ( p) => p,
@@ -397,7 +422,7 @@ impl SourceId {
397
422
/// Returns `true` if the remote registry is the standard <https://crates.io>.
398
423
pub fn is_crates_io ( self ) -> bool {
399
424
match self . inner . kind {
400
- SourceKind :: Registry => { }
425
+ SourceKind :: Registry | SourceKind :: SparseRegistry => { }
401
426
_ => return false ,
402
427
}
403
428
let url = self . inner . url . as_str ( ) ;
@@ -529,7 +554,9 @@ impl fmt::Display for SourceId {
529
554
Ok ( ( ) )
530
555
}
531
556
SourceKind :: Path => write ! ( f, "{}" , url_display( & self . inner. url) ) ,
532
- SourceKind :: Registry => write ! ( f, "registry `{}`" , self . display_registry_name( ) ) ,
557
+ SourceKind :: Registry | SourceKind :: SparseRegistry => {
558
+ write ! ( f, "registry `{}`" , self . display_registry_name( ) )
559
+ }
533
560
SourceKind :: LocalRegistry => write ! ( f, "registry `{}`" , url_display( & self . inner. url) ) ,
534
561
SourceKind :: Directory => write ! ( f, "dir {}" , url_display( & self . inner. url) ) ,
535
562
}
@@ -643,6 +670,10 @@ impl Ord for SourceKind {
643
670
( SourceKind :: Registry , _) => Ordering :: Less ,
644
671
( _, SourceKind :: Registry ) => Ordering :: Greater ,
645
672
673
+ ( SourceKind :: SparseRegistry , SourceKind :: SparseRegistry ) => Ordering :: Equal ,
674
+ ( SourceKind :: SparseRegistry , _) => Ordering :: Less ,
675
+ ( _, SourceKind :: SparseRegistry ) => Ordering :: Greater ,
676
+
646
677
( SourceKind :: LocalRegistry , SourceKind :: LocalRegistry ) => Ordering :: Equal ,
647
678
( SourceKind :: LocalRegistry , _) => Ordering :: Less ,
648
679
( _, SourceKind :: LocalRegistry ) => Ordering :: Greater ,
@@ -714,14 +745,14 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
714
745
ref url,
715
746
..
716
747
} => {
717
- // For sparse http registry the URL already contains the prefix.
718
- if url . scheme ( ) . starts_with ( "sparse+" ) {
719
- // e.g. sparse+http://example.com
720
- write ! ( f , "{url}" )
721
- } else {
722
- // e.g. registry+http://example.com
723
- write ! ( f , "registry+{url}" )
724
- }
748
+ write ! ( f , " registry+{url}" )
749
+ }
750
+ SourceIdInner {
751
+ kind : SourceKind :: SparseRegistry ,
752
+ ref url ,
753
+ ..
754
+ } => {
755
+ write ! ( f , "sparse+{url}" )
725
756
}
726
757
SourceIdInner {
727
758
kind : SourceKind :: LocalRegistry ,
0 commit comments