@@ -39,10 +39,45 @@ mod auth;
39
39
pub struct RegistryConfig {
40
40
/// The index URL. If `None`, use crates.io.
41
41
pub index : Option < String > ,
42
+ pub credential : Credential ,
43
+ }
44
+
45
+ #[ derive( Debug ) ]
46
+ pub enum Credential {
47
+ None ,
42
48
/// The authentication token.
43
- pub token : Option < String > ,
49
+ Token ( String ) ,
44
50
/// Process used for fetching a token.
45
- pub credential_process : Option < ( PathBuf , Vec < String > ) > ,
51
+ Process ( ( PathBuf , Vec < String > ) ) ,
52
+ }
53
+
54
+ impl RegistryConfig {
55
+ /// Returns `true` if the credential is [`None`].
56
+ ///
57
+ /// [`None`]: Credential::None
58
+ pub fn is_none ( & self ) -> bool {
59
+ matches ! ( & self . credential, Credential :: None )
60
+ }
61
+ /// Returns `true` if the credential is [`Token`].
62
+ ///
63
+ /// [`Token`]: Credential::Token
64
+ pub fn is_token ( & self ) -> bool {
65
+ matches ! ( & self . credential, Credential :: Token ( ..) )
66
+ }
67
+ pub fn as_token ( & self ) -> Option < & str > {
68
+ if let Credential :: Token ( v) = & self . credential {
69
+ Some ( & * v)
70
+ } else {
71
+ None
72
+ }
73
+ }
74
+ pub fn as_process ( & self ) -> Option < & ( PathBuf , Vec < String > ) > {
75
+ if let Credential :: Process ( v) = & self . credential {
76
+ Some ( v)
77
+ } else {
78
+ None
79
+ }
80
+ }
46
81
}
47
82
48
83
pub struct PublishOpts < ' cfg > {
@@ -347,10 +382,10 @@ pub fn registry_configuration(
347
382
let ( index, token, process) = match registry {
348
383
Some ( registry) => {
349
384
let index = Some ( config. get_registry_index ( registry) ?. to_string ( ) ) ;
350
- let token_key = format ! ( "registries.{}.token" , registry ) ;
385
+ let token_key = format ! ( "registries.{registry }.token" ) ;
351
386
let token = config. get_string ( & token_key) ?. map ( |p| p. val ) ;
352
387
let process = if config. cli_unstable ( ) . credential_process {
353
- let mut proc_key = format ! ( "registries.{}.credential-process" , registry ) ;
388
+ let mut proc_key = format ! ( "registries.{registry }.credential-process" ) ;
354
389
let mut process = config. get :: < Option < config:: PathAndArgs > > ( & proc_key) ?;
355
390
if process. is_none ( ) && token. is_none ( ) {
356
391
// This explicitly ignores the global credential-process if
@@ -389,8 +424,12 @@ pub fn registry_configuration(
389
424
390
425
Ok ( RegistryConfig {
391
426
index,
392
- token,
393
- credential_process,
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
+ } ,
394
433
} )
395
434
}
396
435
@@ -459,7 +498,7 @@ fn registry(
459
498
// people. It will affect those using source replacement, but
460
499
// hopefully that's a relatively small set of users.
461
500
if token. is_none ( )
462
- && reg_cfg. token . is_some ( )
501
+ && reg_cfg. is_token ( )
463
502
&& registry. is_none ( )
464
503
&& !sid. is_default_registry ( )
465
504
&& !crates_io:: is_url_crates_io ( & api_host)
@@ -471,13 +510,12 @@ fn registry(
471
510
see <https://github.com/rust-lang/cargo/issues/xxx>.\n \
472
511
Use the --token command-line flag to remove this warning.",
473
512
) ?;
474
- reg_cfg. token . clone ( )
513
+ reg_cfg. as_token ( ) . map ( |t| t . to_owned ( ) )
475
514
} else {
476
515
let token = auth:: auth_token (
477
516
config,
478
517
token. as_deref ( ) ,
479
- reg_cfg. token . as_deref ( ) ,
480
- reg_cfg. credential_process . as_ref ( ) ,
518
+ & reg_cfg. credential ,
481
519
registry. as_deref ( ) ,
482
520
& api_host,
483
521
) ?;
@@ -714,7 +752,7 @@ pub fn registry_login(
714
752
}
715
753
} ;
716
754
717
- if let Some ( old_token) = & reg_cfg. token {
755
+ if let Credential :: Token ( old_token) = & reg_cfg. credential {
718
756
if old_token == & token {
719
757
config. shell ( ) . status ( "Login" , "already logged in" ) ?;
720
758
return Ok ( ( ) ) ;
@@ -724,7 +762,7 @@ pub fn registry_login(
724
762
auth:: login (
725
763
config,
726
764
token,
727
- reg_cfg. credential_process . as_ref ( ) ,
765
+ reg_cfg. as_process ( ) ,
728
766
reg. as_deref ( ) ,
729
767
registry. host ( ) ,
730
768
) ?;
@@ -742,7 +780,7 @@ pub fn registry_login(
742
780
pub fn registry_logout ( config : & Config , reg : Option < String > ) -> CargoResult < ( ) > {
743
781
let ( registry, reg_cfg, _) = registry ( config, None , None , reg. clone ( ) , false , false ) ?;
744
782
let reg_name = reg. as_deref ( ) . unwrap_or ( CRATES_IO_DOMAIN ) ;
745
- if reg_cfg. credential_process . is_none ( ) && reg_cfg . token . is_none ( ) {
783
+ if reg_cfg. is_none ( ) {
746
784
config. shell ( ) . status (
747
785
"Logout" ,
748
786
format ! ( "not currently logged in to `{}`" , reg_name) ,
@@ -751,7 +789,7 @@ pub fn registry_logout(config: &Config, reg: Option<String>) -> CargoResult<()>
751
789
}
752
790
auth:: logout (
753
791
config,
754
- reg_cfg. credential_process . as_ref ( ) ,
792
+ reg_cfg. as_process ( ) ,
755
793
reg. as_deref ( ) ,
756
794
registry. host ( ) ,
757
795
) ?;
0 commit comments