@@ -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 config. cli_unstable ( ) . sparse_registry {
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
}
@@ -230,8 +248,9 @@ impl SourceId {
230
248
return Self :: crates_io ( config) ;
231
249
}
232
250
let url = config. get_registry_index ( key) ?;
251
+ let ( kind, url) = Self :: remote_source_kind ( & url) ;
233
252
Ok ( SourceId :: wrap ( SourceIdInner {
234
- kind : SourceKind :: Registry ,
253
+ kind,
235
254
canonical_url : CanonicalUrl :: new ( & url) ?,
236
255
url,
237
256
precise : None ,
@@ -298,16 +317,24 @@ impl SourceId {
298
317
pub fn is_registry ( self ) -> bool {
299
318
matches ! (
300
319
self . inner. kind,
301
- SourceKind :: Registry | SourceKind :: LocalRegistry
320
+ SourceKind :: Registry | SourceKind :: SparseRegistry | SourceKind :: LocalRegistry
302
321
)
303
322
}
304
323
324
+ /// Returns `true` if this source is from a sparse registry.
325
+ pub fn is_sparse ( self ) -> bool {
326
+ matches ! ( self . inner. kind, SourceKind :: SparseRegistry )
327
+ }
328
+
305
329
/// Returns `true` if this source is a "remote" registry.
306
330
///
307
331
/// "remote" may also mean a file URL to a git index, so it is not
308
332
/// necessarily "remote". This just means it is not `local-registry`.
309
333
pub fn is_remote_registry ( self ) -> bool {
310
- matches ! ( self . inner. kind, SourceKind :: Registry )
334
+ matches ! (
335
+ self . inner. kind,
336
+ SourceKind :: Registry | SourceKind :: SparseRegistry
337
+ )
311
338
}
312
339
313
340
/// Returns `true` if this source from a Git repository.
@@ -331,11 +358,9 @@ impl SourceId {
331
358
} ;
332
359
Ok ( Box :: new ( PathSource :: new ( & path, self , config) ) )
333
360
}
334
- SourceKind :: Registry => Ok ( Box :: new ( RegistrySource :: remote (
335
- self ,
336
- yanked_whitelist,
337
- config,
338
- ) ?) ) ,
361
+ SourceKind :: Registry | SourceKind :: SparseRegistry => Ok ( Box :: new (
362
+ RegistrySource :: remote ( self , yanked_whitelist, config) ?,
363
+ ) ) ,
339
364
SourceKind :: LocalRegistry => {
340
365
let path = match self . inner . url . to_file_path ( ) {
341
366
Ok ( p) => p,
@@ -382,7 +407,7 @@ impl SourceId {
382
407
/// Returns `true` if the remote registry is the standard <https://crates.io>.
383
408
pub fn is_crates_io ( self ) -> bool {
384
409
match self . inner . kind {
385
- SourceKind :: Registry => { }
410
+ SourceKind :: Registry | SourceKind :: SparseRegistry => { }
386
411
_ => return false ,
387
412
}
388
413
let url = self . inner . url . as_str ( ) ;
@@ -514,7 +539,9 @@ impl fmt::Display for SourceId {
514
539
Ok ( ( ) )
515
540
}
516
541
SourceKind :: Path => write ! ( f, "{}" , url_display( & self . inner. url) ) ,
517
- SourceKind :: Registry => write ! ( f, "registry `{}`" , self . display_registry_name( ) ) ,
542
+ SourceKind :: Registry | SourceKind :: SparseRegistry => {
543
+ write ! ( f, "registry `{}`" , self . display_registry_name( ) )
544
+ }
518
545
SourceKind :: LocalRegistry => write ! ( f, "registry `{}`" , url_display( & self . inner. url) ) ,
519
546
SourceKind :: Directory => write ! ( f, "dir {}" , url_display( & self . inner. url) ) ,
520
547
}
@@ -628,6 +655,10 @@ impl Ord for SourceKind {
628
655
( SourceKind :: Registry , _) => Ordering :: Less ,
629
656
( _, SourceKind :: Registry ) => Ordering :: Greater ,
630
657
658
+ ( SourceKind :: SparseRegistry , SourceKind :: SparseRegistry ) => Ordering :: Equal ,
659
+ ( SourceKind :: SparseRegistry , _) => Ordering :: Less ,
660
+ ( _, SourceKind :: SparseRegistry ) => Ordering :: Greater ,
661
+
631
662
( SourceKind :: LocalRegistry , SourceKind :: LocalRegistry ) => Ordering :: Equal ,
632
663
( SourceKind :: LocalRegistry , _) => Ordering :: Less ,
633
664
( _, SourceKind :: LocalRegistry ) => Ordering :: Greater ,
@@ -699,14 +730,14 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
699
730
ref url,
700
731
..
701
732
} => {
702
- // For sparse http registry the URL already contains the prefix.
703
- if url . scheme ( ) . starts_with ( "sparse+" ) {
704
- // e.g. sparse+http://example.com
705
- write ! ( f , "{url}" )
706
- } else {
707
- // e.g. registry+http://example.com
708
- write ! ( f , "registry+{url}" )
709
- }
733
+ write ! ( f , " registry+{url}" )
734
+ }
735
+ SourceIdInner {
736
+ kind : SourceKind :: SparseRegistry ,
737
+ ref url ,
738
+ ..
739
+ } => {
740
+ write ! ( f , "sparse+{url}" )
710
741
}
711
742
SourceIdInner {
712
743
kind : SourceKind :: LocalRegistry ,
0 commit comments