@@ -57,9 +57,12 @@ impl<'iu> IndexUrl<'iu> {
5757 ) -> Result < Self , Error > {
5858 // If the crates.io registry has been replaced it doesn't matter what
5959 // the protocol for it has been changed to
60- if let Some ( replacement) =
61- get_source_replacement ( config_root. clone ( ) , cargo_home, "crates-io" ) ?
62- {
60+ if let Some ( replacement) = get_source_replacement (
61+ config_root. clone ( ) ,
62+ cargo_home,
63+ crate :: CRATES_IO_INDEX ,
64+ "crates-io" ,
65+ ) ? {
6366 return Ok ( replacement) ;
6467 }
6568
@@ -70,17 +73,16 @@ impl<'iu> IndexUrl<'iu> {
7073 Some ( "sparse" ) => true ,
7174 Some ( "git" ) => false ,
7275 _ => {
73- let sparse_index =
74- read_cargo_config ( config_root, cargo_home, |config| {
75- match config
76- . pointer ( "/registries/crates-io/protocol" )
77- . and_then ( |p| p. as_str ( ) ) ?
78- {
79- "sparse" => Some ( true ) ,
80- "git" => Some ( false ) ,
81- _ => None ,
82- }
83- } ) ?;
76+ let sparse_index = read_cargo_config ( config_root, cargo_home, |_, config| {
77+ match config
78+ . pointer ( "/registries/crates-io/protocol" )
79+ . and_then ( |p| p. as_str ( ) ) ?
80+ {
81+ "sparse" => Some ( true ) ,
82+ "git" => Some ( false ) ,
83+ _ => None ,
84+ }
85+ } ) ?;
8486
8587 if let Some ( si) = sparse_index {
8688 si
@@ -144,20 +146,22 @@ impl<'iu> IndexUrl<'iu> {
144146 }
145147 }
146148
147- if let Some ( replacement) =
148- get_source_replacement ( config_root. clone ( ) , cargo_home, registry_name) ?
149- {
149+ let registry_url = read_cargo_config ( config_root. clone ( ) , cargo_home, |_, config| {
150+ let path = format ! ( "/registries/{registry_name}/index" ) ;
151+ config. pointer ( & path) ?. as_str ( ) . map ( String :: from)
152+ } ) ?
153+ . ok_or_else ( || Error :: UnknownRegistry ( registry_name. into ( ) ) ) ?;
154+
155+ if let Some ( replacement) = get_source_replacement (
156+ config_root. clone ( ) ,
157+ cargo_home,
158+ & registry_url,
159+ registry_name,
160+ ) ? {
150161 return Ok ( replacement) ;
151162 }
152163
153- read_cargo_config ( config_root, cargo_home, |config| {
154- let path = format ! ( "/registries/{registry_name}/index" ) ;
155- config
156- . pointer ( & path) ?
157- . as_str ( )
158- . map ( |si| Self :: NonCratesIo ( si. to_owned ( ) . into ( ) ) )
159- } ) ?
160- . ok_or_else ( || Error :: UnknownRegistry ( registry_name. into ( ) ) )
164+ Ok ( Self :: NonCratesIo ( registry_url. into ( ) ) )
161165 }
162166}
163167
@@ -268,7 +272,7 @@ impl<'il> IndexLocation<'il> {
268272pub ( crate ) fn read_cargo_config < T > (
269273 root : Option < PathBuf > ,
270274 cargo_home : Option < & Path > ,
271- callback : impl Fn ( & toml_span:: value:: Value < ' _ > ) -> Option < T > ,
275+ callback : impl Fn ( & Path , & toml_span:: value:: Value < ' _ > ) -> Option < T > ,
272276) -> Result < Option < T > , Error > {
273277 if let Some ( mut path) = root. or_else ( || {
274278 std:: env:: current_dir ( )
@@ -284,7 +288,7 @@ pub(crate) fn read_cargo_config<T>(
284288 } ;
285289
286290 let toml = toml_span:: parse ( & contents) . map_err ( Box :: new) ?;
287- if let Some ( value) = callback ( & toml) {
291+ if let Some ( value) = callback ( & path , & toml) {
288292 return Ok ( Some ( value) ) ;
289293 }
290294 }
@@ -306,7 +310,7 @@ pub(crate) fn read_cargo_config<T>(
306310 if path. exists ( ) {
307311 let fc = std:: fs:: read_to_string ( & path) ?;
308312 let toml = toml_span:: parse ( & fc) . map_err ( Box :: new) ?;
309- if let Some ( value) = callback ( & toml) {
313+ if let Some ( value) = callback ( & path , & toml) {
310314 return Ok ( Some ( value) ) ;
311315 }
312316 }
@@ -322,20 +326,30 @@ pub(crate) fn read_cargo_config<T>(
322326pub ( crate ) fn get_source_replacement < ' iu > (
323327 root : Option < PathBuf > ,
324328 cargo_home : Option < & Path > ,
329+ registry_url : & str ,
325330 registry_name : & str ,
326331) -> Result < Option < IndexUrl < ' iu > > , Error > {
327- read_cargo_config ( root, cargo_home, |config| {
328- let path = format ! ( "/source/{registry_name}/replace-with" ) ;
329- let repw = config. pointer ( & path) ?. as_str ( ) ?;
332+ read_cargo_config ( root, cargo_home, |config_path, config| {
333+ let sources = config. as_table ( ) ?. get ( "source" ) ?. as_table ( ) ?;
334+ let repw = sources. iter ( ) . find_map ( |( source_name, source) | {
335+ let source = source. as_table ( ) ?;
336+
337+ let matches = registry_name == source_name. name
338+ || registry_url == source. get ( "registry" ) ?. as_str ( ) ?;
339+ matches. then_some ( source. get ( "replace-with" ) ?. as_str ( ) ?)
340+ } ) ?;
341+
330342 let sources = config. pointer ( "/source" ) ?. as_table ( ) ?;
331343 let replace_src = sources. get ( repw) ?. as_table ( ) ?;
332344
333345 if let Some ( rr) = replace_src. get ( "registry" ) {
334346 rr. as_str ( )
335347 . map ( |r| IndexUrl :: NonCratesIo ( r. to_owned ( ) . into ( ) ) )
336348 } else if let Some ( rlr) = replace_src. get ( "local-registry" ) {
337- rlr. as_str ( )
338- . map ( |l| IndexUrl :: Local ( PathBuf :: from ( l) . into ( ) ) )
349+ let rel_path = rlr. as_str ( ) ?;
350+ Some ( IndexUrl :: Local (
351+ config_path. parent ( ) ?. parent ( ) ?. join ( rel_path) . into ( ) ,
352+ ) )
339353 } else {
340354 None
341355 }
0 commit comments