@@ -51,14 +51,11 @@ struct SourceIdInner {
51
51
kind : SourceKind ,
52
52
/// For example, the exact Git revision of the specified branch for a Git Source.
53
53
precise : Option < String > ,
54
- /// Name of the registry source for alternative registries
55
- /// WARNING: this is not always set for alt-registries when the name is
56
- /// not known.
57
- name : Option < String > ,
58
- /// Name of the alt registry in the `[registries]` table.
59
- /// WARNING: this is not always set for alt-registries when the name is
60
- /// not known.
61
- alt_registry_key : Option < String > ,
54
+ /// Name of the remote registry.
55
+ ///
56
+ /// WARNING: this is not always set when the name is not known,
57
+ /// e.g. registry coming from `--index` or Cargo.lock
58
+ registry_key : Option < KeyOf > ,
62
59
}
63
60
64
61
/// The possible kinds of code source.
@@ -93,11 +90,22 @@ pub enum GitReference {
93
90
DefaultBranch ,
94
91
}
95
92
93
+ /// Where the remote source key is defined.
94
+ ///
95
+ /// The purpose of this is to provide better diagnostics for different sources of keys.
96
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
97
+ enum KeyOf {
98
+ /// Defined in the `[registries]` table or the built-in `crates-io` key.
99
+ Registry ( String ) ,
100
+ /// Defined in the `[source]` replacement table.
101
+ Source ( String ) ,
102
+ }
103
+
96
104
impl SourceId {
97
105
/// Creates a `SourceId` object from the kind and URL.
98
106
///
99
107
/// The canonical url will be calculated, but the precise field will not
100
- fn new ( kind : SourceKind , url : Url , name : Option < & str > ) -> CargoResult < SourceId > {
108
+ fn new ( kind : SourceKind , url : Url , key : Option < KeyOf > ) -> CargoResult < SourceId > {
101
109
if kind == SourceKind :: SparseRegistry {
102
110
// Sparse URLs are different because they store the kind prefix (sparse+)
103
111
// in the URL. This is because the prefix is necessary to differentiate
@@ -111,8 +119,7 @@ impl SourceId {
111
119
canonical_url : CanonicalUrl :: new ( & url) ?,
112
120
url,
113
121
precise : None ,
114
- name : name. map ( |n| n. into ( ) ) ,
115
- alt_registry_key : None ,
122
+ registry_key : key,
116
123
} ) ;
117
124
Ok ( source_id)
118
125
}
@@ -230,10 +237,18 @@ impl SourceId {
230
237
SourceId :: new ( kind, url. to_owned ( ) , None )
231
238
}
232
239
233
- /// Creates a `SourceId` from a remote registry URL with given name .
234
- pub fn for_alt_registry ( url : & Url , name : & str ) -> CargoResult < SourceId > {
240
+ /// Creates a `SourceId` for a remote registry from the `[registries]` table or crates.io .
241
+ pub fn for_alt_registry ( url : & Url , key : & str ) -> CargoResult < SourceId > {
235
242
let kind = Self :: remote_source_kind ( url) ;
236
- SourceId :: new ( kind, url. to_owned ( ) , Some ( name) )
243
+ let key = KeyOf :: Registry ( key. into ( ) ) ;
244
+ SourceId :: new ( kind, url. to_owned ( ) , Some ( key) )
245
+ }
246
+
247
+ /// Creates a `SourceId` for a remote registry from the `[source]` replacement table.
248
+ pub fn for_source_replacement_registry ( url : & Url , key : & str ) -> CargoResult < SourceId > {
249
+ let kind = Self :: remote_source_kind ( url) ;
250
+ let key = KeyOf :: Source ( key. into ( ) ) ;
251
+ SourceId :: new ( kind, url. to_owned ( ) , Some ( key) )
237
252
}
238
253
239
254
/// Creates a `SourceId` from a local registry path.
@@ -262,7 +277,8 @@ impl SourceId {
262
277
if Self :: crates_io_is_sparse ( config) ? {
263
278
config. check_registry_index_not_set ( ) ?;
264
279
let url = CRATES_IO_HTTP_INDEX . into_url ( ) . unwrap ( ) ;
265
- SourceId :: new ( SourceKind :: SparseRegistry , url, Some ( CRATES_IO_REGISTRY ) )
280
+ let key = KeyOf :: Registry ( CRATES_IO_REGISTRY . into ( ) ) ;
281
+ SourceId :: new ( SourceKind :: SparseRegistry , url, Some ( key) )
266
282
} else {
267
283
Self :: crates_io ( config)
268
284
}
@@ -289,15 +305,7 @@ impl SourceId {
289
305
return Self :: crates_io ( config) ;
290
306
}
291
307
let url = config. get_registry_index ( key) ?;
292
- let kind = Self :: remote_source_kind ( & url) ;
293
- Ok ( SourceId :: wrap ( SourceIdInner {
294
- kind,
295
- canonical_url : CanonicalUrl :: new ( & url) ?,
296
- url,
297
- precise : None ,
298
- name : Some ( key. to_string ( ) ) ,
299
- alt_registry_key : Some ( key. to_string ( ) ) ,
300
- } ) )
308
+ Self :: for_alt_registry ( & url, key)
301
309
}
302
310
303
311
/// Gets this source URL.
@@ -322,10 +330,8 @@ impl SourceId {
322
330
323
331
/// Displays the name of a registry if it has one. Otherwise just the URL.
324
332
pub fn display_registry_name ( self ) -> String {
325
- if self . is_crates_io ( ) {
326
- CRATES_IO_REGISTRY . to_string ( )
327
- } else if let Some ( name) = & self . inner . name {
328
- name. clone ( )
333
+ if let Some ( key) = self . inner . registry_key . as_ref ( ) . map ( |k| k. key ( ) ) {
334
+ key. into ( )
329
335
} else if self . precise ( ) . is_some ( ) {
330
336
// We remove `precise` here to retrieve an permissive version of
331
337
// `SourceIdInner`, which may contain the registry name.
@@ -335,11 +341,10 @@ impl SourceId {
335
341
}
336
342
}
337
343
338
- /// Gets the name of the remote registry as defined in the `[registries]` table.
339
- /// WARNING: alt registries that come from Cargo.lock, or --index will
340
- /// not have a name.
344
+ /// Gets the name of the remote registry as defined in the `[registries]` table,
345
+ /// or the built-in `crates-io` key.
341
346
pub fn alt_registry_key ( & self ) -> Option < & str > {
342
- self . inner . alt_registry_key . as_deref ( )
347
+ self . inner . registry_key . as_ref ( ) ? . alternative_registry ( )
343
348
}
344
349
345
350
/// Returns `true` if this source is from a filesystem path.
@@ -652,10 +657,9 @@ impl Hash for SourceId {
652
657
/// The hash of `SourceIdInner` is used to retrieve its interned value from
653
658
/// `SOURCE_ID_CACHE`. We only care about fields that make `SourceIdInner`
654
659
/// unique. Optional fields not affecting the uniqueness must be excluded,
655
- /// such as [`name`] and [`alt_registry_key `]. That's why this is not derived.
660
+ /// such as [`registry_key `]. That's why this is not derived.
656
661
///
657
- /// [`name`]: SourceIdInner::name
658
- /// [`alt_registry_key`]: SourceIdInner::alt_registry_key
662
+ /// [`registry_key`]: SourceIdInner::registry_key
659
663
impl Hash for SourceIdInner {
660
664
fn hash < S : hash:: Hasher > ( & self , into : & mut S ) {
661
665
self . kind . hash ( into) ;
@@ -868,6 +872,23 @@ impl<'a> fmt::Display for PrettyRef<'a> {
868
872
}
869
873
}
870
874
875
+ impl KeyOf {
876
+ /// Gets the underlying key.
877
+ fn key ( & self ) -> & str {
878
+ match self {
879
+ KeyOf :: Registry ( k) | KeyOf :: Source ( k) => k,
880
+ }
881
+ }
882
+
883
+ /// Gets the key if it's from an alternative registry.
884
+ fn alternative_registry ( & self ) -> Option < & str > {
885
+ match self {
886
+ KeyOf :: Registry ( k) => Some ( k) ,
887
+ _ => None ,
888
+ }
889
+ }
890
+ }
891
+
871
892
#[ cfg( test) ]
872
893
mod tests {
873
894
use super :: { GitReference , SourceId , SourceKind } ;
0 commit comments