@@ -36,14 +36,7 @@ mod auth;
3636///
3737/// This is loaded based on the `--registry` flag and the config settings.
3838#[ derive( Debug ) ]
39- pub struct RegistryConfig {
40- /// The index URL. If `None`, use crates.io.
41- pub index : Option < String > ,
42- pub credential : Credential ,
43- }
44-
45- #[ derive( Debug ) ]
46- pub enum Credential {
39+ pub enum RegistryConfig {
4740 None ,
4841 /// The authentication token.
4942 Token ( String ) ,
@@ -56,23 +49,23 @@ impl RegistryConfig {
5649 ///
5750 /// [`None`]: Credential::None
5851 pub fn is_none ( & self ) -> bool {
59- matches ! ( & self . credential , Credential :: None )
52+ matches ! ( self , Self :: None )
6053 }
6154 /// Returns `true` if the credential is [`Token`].
6255 ///
6356 /// [`Token`]: Credential::Token
6457 pub fn is_token ( & self ) -> bool {
65- matches ! ( & self . credential , Credential :: Token ( ..) )
58+ matches ! ( self , Self :: Token ( ..) )
6659 }
6760 pub fn as_token ( & self ) -> Option < & str > {
68- if let Credential :: Token ( v) = & self . credential {
61+ if let Self :: Token ( v) = self {
6962 Some ( & * v)
7063 } else {
7164 None
7265 }
7366 }
7467 pub fn as_process ( & self ) -> Option < & ( PathBuf , Vec < String > ) > {
75- if let Credential :: Process ( v) = & self . credential {
68+ if let Self :: Process ( v) = self {
7669 Some ( v)
7770 } else {
7871 None
@@ -133,8 +126,8 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
133126 let ( mut registry, _reg_cfg, reg_id) = registry (
134127 opts. config ,
135128 opts. token . clone ( ) ,
136- opts. index . clone ( ) ,
137- publish_registry,
129+ opts. index . as_deref ( ) ,
130+ publish_registry. as_deref ( ) ,
138131 true ,
139132 !opts. dry_run ,
140133 ) ?;
@@ -371,17 +364,14 @@ pub fn registry_configuration(
371364) -> CargoResult < RegistryConfig > {
372365 let err_both = |token_key : & str , proc_key : & str | {
373366 Err ( format_err ! (
374- "both `{TOKEN_KEY }` and `{PROC_KEY }` \
367+ "both `{token_key }` and `{proc_key }` \
375368 were specified in the config\n \
376369 Only one of these values may be set, remove one or the other to proceed.",
377- TOKEN_KEY = token_key,
378- PROC_KEY = proc_key,
379370 ) )
380371 } ;
381372 // `registry.default` is handled in command-line parsing.
382- let ( index , token, process) = match registry {
373+ let ( token, process) = match registry {
383374 Some ( registry) => {
384- let index = Some ( config. get_registry_index ( registry) ?. to_string ( ) ) ;
385375 let token_key = format ! ( "registries.{registry}.token" ) ;
386376 let token = config. get_string ( & token_key) ?. map ( |p| p. val ) ;
387377 let process = if config. cli_unstable ( ) . credential_process {
@@ -399,7 +389,7 @@ pub fn registry_configuration(
399389 } else {
400390 None
401391 } ;
402- ( index , token, process)
392+ ( token, process)
403393 }
404394 None => {
405395 // Use crates.io default.
@@ -415,21 +405,18 @@ pub fn registry_configuration(
415405 } else {
416406 None
417407 } ;
418- ( None , token, process)
408+ ( token, process)
419409 }
420410 } ;
421411
422412 let credential_process =
423413 process. map ( |process| ( process. path . resolve_program ( config) , process. args ) ) ;
424414
425- Ok ( RegistryConfig {
426- index,
427- credential : match ( token, credential_process) {
428- ( None , None ) => Credential :: None ,
429- ( None , Some ( process) ) => Credential :: Process ( process) ,
430- ( Some ( x) , None ) => Credential :: Token ( x) ,
431- ( Some ( _) , Some ( _) ) => unreachable ! ( "Only one of these values may be set." ) ,
432- } ,
415+ Ok ( match ( token, credential_process) {
416+ ( None , None ) => RegistryConfig :: None ,
417+ ( None , Some ( process) ) => RegistryConfig :: Process ( process) ,
418+ ( Some ( x) , None ) => RegistryConfig :: Token ( x) ,
419+ ( Some ( _) , Some ( _) ) => unreachable ! ( "Only one of these values may be set." ) ,
433420 } )
434421}
435422
@@ -447,8 +434,8 @@ pub fn registry_configuration(
447434fn registry (
448435 config : & Config ,
449436 token : Option < String > ,
450- index : Option < String > ,
451- registry : Option < String > ,
437+ index : Option < & str > ,
438+ registry : Option < & str > ,
452439 force_update : bool ,
453440 validate_token : bool ,
454441) -> CargoResult < ( Registry , RegistryConfig , SourceId ) > {
@@ -457,9 +444,12 @@ fn registry(
457444 bail ! ( "both `--index` and `--registry` should not be set at the same time" ) ;
458445 }
459446 // Parse all configuration options
460- let reg_cfg = registry_configuration ( config, registry. as_deref ( ) ) ?;
461- let opt_index = reg_cfg. index . as_deref ( ) . or_else ( || index. as_deref ( ) ) ;
462- let sid = get_source_id ( config, opt_index, registry. as_deref ( ) ) ?;
447+ let reg_cfg = registry_configuration ( config, registry) ?;
448+ let opt_index = registry
449+ . map ( |r| config. get_registry_index ( r) )
450+ . transpose ( ) ?
451+ . map ( |u| u. to_string ( ) ) ;
452+ let sid = get_source_id ( config, opt_index. as_deref ( ) . or ( index) , registry) ?;
463453 if !sid. is_remote_registry ( ) {
464454 bail ! (
465455 "{} does not support API commands.\n \
@@ -512,13 +502,8 @@ fn registry(
512502 ) ?;
513503 reg_cfg. as_token ( ) . map ( |t| t. to_owned ( ) )
514504 } else {
515- let token = auth:: auth_token (
516- config,
517- token. as_deref ( ) ,
518- & reg_cfg. credential ,
519- registry. as_deref ( ) ,
520- & api_host,
521- ) ?;
505+ let token =
506+ auth:: auth_token ( config, token. as_deref ( ) , & reg_cfg, registry, & api_host) ?;
522507 Some ( token)
523508 }
524509 }
@@ -730,7 +715,8 @@ pub fn registry_login(
730715 token : Option < String > ,
731716 reg : Option < String > ,
732717) -> CargoResult < ( ) > {
733- let ( registry, reg_cfg, _) = registry ( config, token. clone ( ) , None , reg. clone ( ) , false , false ) ?;
718+ let ( registry, reg_cfg, _) =
719+ registry ( config, token. clone ( ) , None , reg. as_deref ( ) , false , false ) ?;
734720
735721 let token = match token {
736722 Some ( token) => token,
@@ -752,7 +738,7 @@ pub fn registry_login(
752738 }
753739 } ;
754740
755- if let Credential :: Token ( old_token) = & reg_cfg. credential {
741+ if let RegistryConfig :: Token ( old_token) = & reg_cfg {
756742 if old_token == & token {
757743 config. shell ( ) . status ( "Login" , "already logged in" ) ?;
758744 return Ok ( ( ) ) ;
@@ -778,7 +764,7 @@ pub fn registry_login(
778764}
779765
780766pub fn registry_logout ( config : & Config , reg : Option < String > ) -> CargoResult < ( ) > {
781- let ( registry, reg_cfg, _) = registry ( config, None , None , reg. clone ( ) , false , false ) ?;
767+ let ( registry, reg_cfg, _) = registry ( config, None , None , reg. as_deref ( ) , false , false ) ?;
782768 let reg_name = reg. as_deref ( ) . unwrap_or ( CRATES_IO_DOMAIN ) ;
783769 if reg_cfg. is_none ( ) {
784770 config. shell ( ) . status (
@@ -826,8 +812,8 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
826812 let ( mut registry, _, _) = registry (
827813 config,
828814 opts. token . clone ( ) ,
829- opts. index . clone ( ) ,
830- opts. registry . clone ( ) ,
815+ opts. index . as_deref ( ) ,
816+ opts. registry . as_deref ( ) ,
831817 true ,
832818 true ,
833819 ) ?;
@@ -902,7 +888,8 @@ pub fn yank(
902888 None => bail ! ( "a version must be specified to yank" ) ,
903889 } ;
904890
905- let ( mut registry, _, _) = registry ( config, token, index, reg, true , true ) ?;
891+ let ( mut registry, _, _) =
892+ registry ( config, token, index. as_deref ( ) , reg. as_deref ( ) , true , true ) ?;
906893
907894 if undo {
908895 config
@@ -961,7 +948,8 @@ pub fn search(
961948 prefix
962949 }
963950
964- let ( mut registry, _, source_id) = registry ( config, None , index, reg, false , false ) ?;
951+ let ( mut registry, _, source_id) =
952+ registry ( config, None , index. as_deref ( ) , reg. as_deref ( ) , false , false ) ?;
965953 let ( crates, total_crates) = registry. search ( query, limit) . with_context ( || {
966954 format ! (
967955 "failed to retrieve search results from the registry at {}" ,
0 commit comments