@@ -190,25 +190,11 @@ fn get_source_id(
190190 gctx : & GlobalContext ,
191191 reg_or_index : Option < & RegistryOrIndex > ,
192192) -> CargoResult < RegistrySourceIds > {
193- let sid = match reg_or_index {
194- None => SourceId :: crates_io ( gctx) ?,
195- Some ( RegistryOrIndex :: Index ( url) ) => SourceId :: for_registry ( url) ?,
196- Some ( RegistryOrIndex :: Registry ( r) ) => SourceId :: alt_registry ( gctx, r) ?,
197- } ;
198- // Load source replacements that are built-in to Cargo.
199- let builtin_replacement_sid = SourceConfigMap :: empty ( gctx) ?
200- . load ( sid, & HashSet :: new ( ) ) ?
201- . replaced_source_id ( ) ;
202- let replacement_sid = SourceConfigMap :: new ( gctx) ?
203- . load ( sid, & HashSet :: new ( ) ) ?
204- . replaced_source_id ( ) ;
193+ let sid = get_initial_source_id ( gctx, reg_or_index) ?;
194+ let ( builtin_replacement_sid, replacement_sid) = get_replacement_source_ids ( gctx, sid) ?;
195+
205196 if reg_or_index. is_none ( ) && replacement_sid != builtin_replacement_sid {
206- // Neither --registry nor --index was passed and the user has configured source-replacement.
207- if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
208- bail ! ( "crates-io is replaced with remote registry {replacement_name};\n include `--registry {replacement_name}` or `--registry crates-io`" ) ;
209- } else {
210- bail ! ( "crates-io is replaced with non-remote-registry source {replacement_sid};\n include `--registry crates-io` to use crates.io" ) ;
211- }
197+ bail ! ( gen_replacement_error( replacement_sid) ) ;
212198 } else {
213199 Ok ( RegistrySourceIds {
214200 original : sid,
@@ -217,6 +203,56 @@ fn get_source_id(
217203 }
218204}
219205
206+ fn get_initial_source_id (
207+ gctx : & GlobalContext ,
208+ reg_or_index : Option < & RegistryOrIndex > ,
209+ ) -> CargoResult < SourceId > {
210+ match reg_or_index {
211+ None => SourceId :: crates_io ( gctx) ,
212+ Some ( reg_or_index) => get_initial_source_id_from_registry_or_index ( gctx, reg_or_index) ,
213+ }
214+ }
215+
216+ fn get_initial_source_id_from_registry_or_index (
217+ gctx : & GlobalContext ,
218+ reg_or_index : & RegistryOrIndex ,
219+ ) -> CargoResult < SourceId > {
220+ match reg_or_index {
221+ RegistryOrIndex :: Index ( url) => SourceId :: for_registry ( url) ,
222+ RegistryOrIndex :: Registry ( r) => SourceId :: alt_registry ( gctx, r) ,
223+ }
224+ }
225+
226+ fn get_replacement_source_ids (
227+ gctx : & GlobalContext ,
228+ sid : SourceId ,
229+ ) -> CargoResult < ( SourceId , SourceId ) > {
230+ let builtin_replacement_sid = SourceConfigMap :: empty ( gctx) ?
231+ . load ( sid, & HashSet :: new ( ) ) ?
232+ . replaced_source_id ( ) ;
233+ let replacement_sid = SourceConfigMap :: new ( gctx) ?
234+ . load ( sid, & HashSet :: new ( ) ) ?
235+ . replaced_source_id ( ) ;
236+ Ok ( ( builtin_replacement_sid, replacement_sid) )
237+ }
238+
239+ fn gen_replacement_error ( replacement_sid : SourceId ) -> String {
240+ // Neither --registry nor --index was passed and the user has configured source-replacement.
241+ let error_message = if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
242+ format ! (
243+ "crates-io is replaced with remote registry {};\n include `--registry {}` or `--registry crates-io`" ,
244+ replacement_name, replacement_name
245+ )
246+ } else {
247+ format ! (
248+ "crates-io is replaced with non-remote-registry source {};\n include `--registry crates-io` to use crates.io" ,
249+ replacement_sid
250+ )
251+ } ;
252+
253+ error_message
254+ }
255+
220256struct RegistrySourceIds {
221257 /// Use when looking up the auth token, or writing out `Cargo.lock`
222258 original : SourceId ,
0 commit comments