@@ -107,6 +107,8 @@ impl RegistryCredentialConfig {
107
107
108
108
/// Returns the `Registry` and `Source` based on command-line and config settings.
109
109
///
110
+ /// * `source_ids`: The source IDs for the registry. It contains the original source ID and
111
+ /// the replacement source ID.
110
112
/// * `token_from_cmdline`: The token from the command-line. If not set, uses the token
111
113
/// from the config.
112
114
/// * `index`: The index URL from the command-line.
@@ -116,13 +118,12 @@ impl RegistryCredentialConfig {
116
118
/// * `token_required`: If `true`, the token will be set.
117
119
fn registry (
118
120
gctx : & GlobalContext ,
121
+ source_ids : & RegistrySourceIds ,
119
122
token_from_cmdline : Option < Secret < & str > > ,
120
123
reg_or_index : Option < & RegistryOrIndex > ,
121
124
force_update : bool ,
122
125
token_required : Option < Operation < ' _ > > ,
123
- ) -> CargoResult < ( Registry , RegistrySourceIds ) > {
124
- let source_ids = get_source_id ( gctx, reg_or_index) ?;
125
-
126
+ ) -> CargoResult < Registry > {
126
127
let is_index = reg_or_index. map ( |v| v. is_index ( ) ) . unwrap_or_default ( ) ;
127
128
if is_index && token_required. is_some ( ) && token_from_cmdline. is_none ( ) {
128
129
bail ! ( "command-line argument --index requires --token to be specified" ) ;
@@ -165,9 +166,11 @@ fn registry(
165
166
None
166
167
} ;
167
168
let handle = http_handle ( gctx) ?;
168
- Ok ( (
169
- Registry :: new_handle ( api_host, token, handle, cfg. auth_required ) ,
170
- source_ids,
169
+ Ok ( Registry :: new_handle (
170
+ api_host,
171
+ token,
172
+ handle,
173
+ cfg. auth_required ,
171
174
) )
172
175
}
173
176
@@ -188,25 +191,11 @@ fn get_source_id(
188
191
gctx : & GlobalContext ,
189
192
reg_or_index : Option < & RegistryOrIndex > ,
190
193
) -> CargoResult < RegistrySourceIds > {
191
- let sid = match reg_or_index {
192
- None => SourceId :: crates_io ( gctx) ?,
193
- Some ( RegistryOrIndex :: Index ( url) ) => SourceId :: for_registry ( url) ?,
194
- Some ( RegistryOrIndex :: Registry ( r) ) => SourceId :: alt_registry ( gctx, r) ?,
195
- } ;
196
- // Load source replacements that are built-in to Cargo.
197
- let builtin_replacement_sid = SourceConfigMap :: empty ( gctx) ?
198
- . load ( sid, & HashSet :: new ( ) ) ?
199
- . replaced_source_id ( ) ;
200
- let replacement_sid = SourceConfigMap :: new ( gctx) ?
201
- . load ( sid, & HashSet :: new ( ) ) ?
202
- . replaced_source_id ( ) ;
194
+ let sid = get_initial_source_id ( gctx, reg_or_index) ?;
195
+ let ( builtin_replacement_sid, replacement_sid) = get_replacement_source_ids ( gctx, sid) ?;
196
+
203
197
if reg_or_index. is_none ( ) && replacement_sid != builtin_replacement_sid {
204
- // Neither --registry nor --index was passed and the user has configured source-replacement.
205
- if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
206
- bail ! ( "crates-io is replaced with remote registry {replacement_name};\n include `--registry {replacement_name}` or `--registry crates-io`" ) ;
207
- } else {
208
- bail ! ( "crates-io is replaced with non-remote-registry source {replacement_sid};\n include `--registry crates-io` to use crates.io" ) ;
209
- }
198
+ bail ! ( gen_replacement_error( replacement_sid) ) ;
210
199
} else {
211
200
Ok ( RegistrySourceIds {
212
201
original : sid,
@@ -215,6 +204,56 @@ fn get_source_id(
215
204
}
216
205
}
217
206
207
+ fn get_initial_source_id (
208
+ gctx : & GlobalContext ,
209
+ reg_or_index : Option < & RegistryOrIndex > ,
210
+ ) -> CargoResult < SourceId > {
211
+ match reg_or_index {
212
+ None => SourceId :: crates_io ( gctx) ,
213
+ Some ( reg_or_index) => get_initial_source_id_from_registry_or_index ( gctx, reg_or_index) ,
214
+ }
215
+ }
216
+
217
+ fn get_initial_source_id_from_registry_or_index (
218
+ gctx : & GlobalContext ,
219
+ reg_or_index : & RegistryOrIndex ,
220
+ ) -> CargoResult < SourceId > {
221
+ match reg_or_index {
222
+ RegistryOrIndex :: Index ( url) => SourceId :: for_registry ( url) ,
223
+ RegistryOrIndex :: Registry ( r) => SourceId :: alt_registry ( gctx, r) ,
224
+ }
225
+ }
226
+
227
+ fn get_replacement_source_ids (
228
+ gctx : & GlobalContext ,
229
+ sid : SourceId ,
230
+ ) -> CargoResult < ( SourceId , SourceId ) > {
231
+ let builtin_replacement_sid = SourceConfigMap :: empty ( gctx) ?
232
+ . load ( sid, & HashSet :: new ( ) ) ?
233
+ . replaced_source_id ( ) ;
234
+ let replacement_sid = SourceConfigMap :: new ( gctx) ?
235
+ . load ( sid, & HashSet :: new ( ) ) ?
236
+ . replaced_source_id ( ) ;
237
+ Ok ( ( builtin_replacement_sid, replacement_sid) )
238
+ }
239
+
240
+ fn gen_replacement_error ( replacement_sid : SourceId ) -> String {
241
+ // Neither --registry nor --index was passed and the user has configured source-replacement.
242
+ let error_message = if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
243
+ format ! (
244
+ "crates-io is replaced with remote registry {};\n include `--registry {}` or `--registry crates-io`" ,
245
+ replacement_name, replacement_name
246
+ )
247
+ } else {
248
+ format ! (
249
+ "crates-io is replaced with non-remote-registry source {};\n include `--registry crates-io` to use crates.io" ,
250
+ replacement_sid
251
+ )
252
+ } ;
253
+
254
+ error_message
255
+ }
256
+
218
257
struct RegistrySourceIds {
219
258
/// Use when looking up the auth token, or writing out `Cargo.lock`
220
259
original : SourceId ,
0 commit comments