@@ -107,6 +107,8 @@ impl RegistryCredentialConfig {
107107
108108/// Returns the `Registry` and `Source` based on command-line and config settings.
109109///
110+ /// * `source_ids`: The source IDs for the registry. It contains the original source ID and
111+ /// the replacement source ID.
110112/// * `token_from_cmdline`: The token from the command-line. If not set, uses the token
111113/// from the config.
112114/// * `index`: The index URL from the command-line.
@@ -116,13 +118,12 @@ impl RegistryCredentialConfig {
116118/// * `token_required`: If `true`, the token will be set.
117119fn registry (
118120 gctx : & GlobalContext ,
121+ source_ids : & RegistrySourceIds ,
119122 token_from_cmdline : Option < Secret < & str > > ,
120123 reg_or_index : Option < & RegistryOrIndex > ,
121124 force_update : bool ,
122125 token_required : Option < Operation < ' _ > > ,
123- ) -> CargoResult < ( Registry , RegistrySourceIds ) > {
124- let source_ids = get_source_id ( gctx, reg_or_index) ?;
125-
126+ ) -> CargoResult < Registry > {
126127 let is_index = reg_or_index. map ( |v| v. is_index ( ) ) . unwrap_or_default ( ) ;
127128 if is_index && token_required. is_some ( ) && token_from_cmdline. is_none ( ) {
128129 bail ! ( "command-line argument --index requires --token to be specified" ) ;
@@ -165,9 +166,11 @@ fn registry(
165166 None
166167 } ;
167168 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 ,
171174 ) )
172175}
173176
@@ -188,25 +191,11 @@ fn get_source_id(
188191 gctx : & GlobalContext ,
189192 reg_or_index : Option < & RegistryOrIndex > ,
190193) -> 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+
203197 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) ) ;
210199 } else {
211200 Ok ( RegistrySourceIds {
212201 original : sid,
@@ -215,6 +204,56 @@ fn get_source_id(
215204 }
216205}
217206
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+
218257struct RegistrySourceIds {
219258 /// Use when looking up the auth token, or writing out `Cargo.lock`
220259 original : SourceId ,
0 commit comments