@@ -36,13 +36,41 @@ mod auth;
36
36
///
37
37
/// This is loaded based on the `--registry` flag and the config settings.
38
38
#[ derive( Debug ) ]
39
- pub struct RegistryConfig {
40
- /// The index URL. If `None`, use crates.io.
41
- pub index : Option < String > ,
39
+ pub enum RegistryConfig {
40
+ None ,
42
41
/// The authentication token.
43
- pub token : Option < String > ,
42
+ Token ( String ) ,
44
43
/// Process used for fetching a token.
45
- pub credential_process : Option < ( PathBuf , Vec < String > ) > ,
44
+ Process ( ( PathBuf , Vec < String > ) ) ,
45
+ }
46
+
47
+ impl RegistryConfig {
48
+ /// Returns `true` if the credential is [`None`].
49
+ ///
50
+ /// [`None`]: Credential::None
51
+ pub fn is_none ( & self ) -> bool {
52
+ matches ! ( self , Self :: None )
53
+ }
54
+ /// Returns `true` if the credential is [`Token`].
55
+ ///
56
+ /// [`Token`]: Credential::Token
57
+ pub fn is_token ( & self ) -> bool {
58
+ matches ! ( self , Self :: Token ( ..) )
59
+ }
60
+ pub fn as_token ( & self ) -> Option < & str > {
61
+ if let Self :: Token ( v) = self {
62
+ Some ( & * v)
63
+ } else {
64
+ None
65
+ }
66
+ }
67
+ pub fn as_process ( & self ) -> Option < & ( PathBuf , Vec < String > ) > {
68
+ if let Self :: Process ( v) = self {
69
+ Some ( v)
70
+ } else {
71
+ None
72
+ }
73
+ }
46
74
}
47
75
48
76
pub struct PublishOpts < ' cfg > {
@@ -98,8 +126,8 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
98
126
let ( mut registry, _reg_cfg, reg_id) = registry (
99
127
opts. config ,
100
128
opts. token . clone ( ) ,
101
- opts. index . clone ( ) ,
102
- publish_registry,
129
+ opts. index . as_deref ( ) ,
130
+ publish_registry. as_deref ( ) ,
103
131
true ,
104
132
!opts. dry_run ,
105
133
) ?;
@@ -336,21 +364,18 @@ pub fn registry_configuration(
336
364
) -> CargoResult < RegistryConfig > {
337
365
let err_both = |token_key : & str , proc_key : & str | {
338
366
Err ( format_err ! (
339
- "both `{TOKEN_KEY }` and `{PROC_KEY }` \
367
+ "both `{token_key }` and `{proc_key }` \
340
368
were specified in the config\n \
341
369
Only one of these values may be set, remove one or the other to proceed.",
342
- TOKEN_KEY = token_key,
343
- PROC_KEY = proc_key,
344
370
) )
345
371
} ;
346
372
// `registry.default` is handled in command-line parsing.
347
- let ( index , token, process) = match registry {
373
+ let ( token, process) = match registry {
348
374
Some ( registry) => {
349
- let index = Some ( config. get_registry_index ( registry) ?. to_string ( ) ) ;
350
- let token_key = format ! ( "registries.{}.token" , registry) ;
375
+ let token_key = format ! ( "registries.{registry}.token" ) ;
351
376
let token = config. get_string ( & token_key) ?. map ( |p| p. val ) ;
352
377
let process = if config. cli_unstable ( ) . credential_process {
353
- let mut proc_key = format ! ( "registries.{}.credential-process" , registry ) ;
378
+ let mut proc_key = format ! ( "registries.{registry }.credential-process" ) ;
354
379
let mut process = config. get :: < Option < config:: PathAndArgs > > ( & proc_key) ?;
355
380
if process. is_none ( ) && token. is_none ( ) {
356
381
// This explicitly ignores the global credential-process if
@@ -364,7 +389,7 @@ pub fn registry_configuration(
364
389
} else {
365
390
None
366
391
} ;
367
- ( index , token, process)
392
+ ( token, process)
368
393
}
369
394
None => {
370
395
// Use crates.io default.
@@ -380,17 +405,18 @@ pub fn registry_configuration(
380
405
} else {
381
406
None
382
407
} ;
383
- ( None , token, process)
408
+ ( token, process)
384
409
}
385
410
} ;
386
411
387
412
let credential_process =
388
413
process. map ( |process| ( process. path . resolve_program ( config) , process. args ) ) ;
389
414
390
- Ok ( RegistryConfig {
391
- index,
392
- token,
393
- credential_process,
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." ) ,
394
420
} )
395
421
}
396
422
@@ -408,8 +434,8 @@ pub fn registry_configuration(
408
434
fn registry (
409
435
config : & Config ,
410
436
token : Option < String > ,
411
- index : Option < String > ,
412
- registry : Option < String > ,
437
+ index : Option < & str > ,
438
+ registry : Option < & str > ,
413
439
force_update : bool ,
414
440
validate_token : bool ,
415
441
) -> CargoResult < ( Registry , RegistryConfig , SourceId ) > {
@@ -418,9 +444,12 @@ fn registry(
418
444
bail ! ( "both `--index` and `--registry` should not be set at the same time" ) ;
419
445
}
420
446
// Parse all configuration options
421
- let reg_cfg = registry_configuration ( config, registry. as_deref ( ) ) ?;
422
- let opt_index = reg_cfg. index . as_deref ( ) . or_else ( || index. as_deref ( ) ) ;
423
- 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) ?;
424
453
if !sid. is_remote_registry ( ) {
425
454
bail ! (
426
455
"{} does not support API commands.\n \
@@ -459,7 +488,7 @@ fn registry(
459
488
// people. It will affect those using source replacement, but
460
489
// hopefully that's a relatively small set of users.
461
490
if token. is_none ( )
462
- && reg_cfg. token . is_some ( )
491
+ && reg_cfg. is_token ( )
463
492
&& registry. is_none ( )
464
493
&& !sid. is_default_registry ( )
465
494
&& !crates_io:: is_url_crates_io ( & api_host)
@@ -471,16 +500,10 @@ fn registry(
471
500
see <https://github.com/rust-lang/cargo/issues/xxx>.\n \
472
501
Use the --token command-line flag to remove this warning.",
473
502
) ?;
474
- reg_cfg. token . clone ( )
503
+ reg_cfg. as_token ( ) . map ( |t| t . to_owned ( ) )
475
504
} else {
476
- let token = auth:: auth_token (
477
- config,
478
- token. as_deref ( ) ,
479
- reg_cfg. token . as_deref ( ) ,
480
- reg_cfg. credential_process . as_ref ( ) ,
481
- registry. as_deref ( ) ,
482
- & api_host,
483
- ) ?;
505
+ let token =
506
+ auth:: auth_token ( config, token. as_deref ( ) , & reg_cfg, registry, & api_host) ?;
484
507
Some ( token)
485
508
}
486
509
}
@@ -692,7 +715,8 @@ pub fn registry_login(
692
715
token : Option < String > ,
693
716
reg : Option < String > ,
694
717
) -> CargoResult < ( ) > {
695
- 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 ) ?;
696
720
697
721
let token = match token {
698
722
Some ( token) => token,
@@ -714,7 +738,7 @@ pub fn registry_login(
714
738
}
715
739
} ;
716
740
717
- if let Some ( old_token) = & reg_cfg. token {
741
+ if let RegistryConfig :: Token ( old_token) = & reg_cfg {
718
742
if old_token == & token {
719
743
config. shell ( ) . status ( "Login" , "already logged in" ) ?;
720
744
return Ok ( ( ) ) ;
@@ -724,7 +748,7 @@ pub fn registry_login(
724
748
auth:: login (
725
749
config,
726
750
token,
727
- reg_cfg. credential_process . as_ref ( ) ,
751
+ reg_cfg. as_process ( ) ,
728
752
reg. as_deref ( ) ,
729
753
registry. host ( ) ,
730
754
) ?;
@@ -740,9 +764,9 @@ pub fn registry_login(
740
764
}
741
765
742
766
pub fn registry_logout ( config : & Config , reg : Option < String > ) -> CargoResult < ( ) > {
743
- 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 ) ?;
744
768
let reg_name = reg. as_deref ( ) . unwrap_or ( CRATES_IO_DOMAIN ) ;
745
- if reg_cfg. credential_process . is_none ( ) && reg_cfg . token . is_none ( ) {
769
+ if reg_cfg. is_none ( ) {
746
770
config. shell ( ) . status (
747
771
"Logout" ,
748
772
format ! ( "not currently logged in to `{}`" , reg_name) ,
@@ -751,7 +775,7 @@ pub fn registry_logout(config: &Config, reg: Option<String>) -> CargoResult<()>
751
775
}
752
776
auth:: logout (
753
777
config,
754
- reg_cfg. credential_process . as_ref ( ) ,
778
+ reg_cfg. as_process ( ) ,
755
779
reg. as_deref ( ) ,
756
780
registry. host ( ) ,
757
781
) ?;
@@ -788,8 +812,8 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
788
812
let ( mut registry, _, _) = registry (
789
813
config,
790
814
opts. token . clone ( ) ,
791
- opts. index . clone ( ) ,
792
- opts. registry . clone ( ) ,
815
+ opts. index . as_deref ( ) ,
816
+ opts. registry . as_deref ( ) ,
793
817
true ,
794
818
true ,
795
819
) ?;
@@ -864,7 +888,8 @@ pub fn yank(
864
888
None => bail ! ( "a version must be specified to yank" ) ,
865
889
} ;
866
890
867
- 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 ) ?;
868
893
869
894
if undo {
870
895
config
@@ -923,7 +948,8 @@ pub fn search(
923
948
prefix
924
949
}
925
950
926
- 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 ) ?;
927
953
let ( crates, total_crates) = registry. search ( query, limit) . with_context ( || {
928
954
format ! (
929
955
"failed to retrieve search results from the registry at {}" ,
0 commit comments