1
1
use std:: fmt;
2
2
3
3
/// Key for a configuration variable.
4
+ ///
5
+ /// This type represents a configuration variable that we're looking up in
6
+ /// Cargo's configuration. This structure simultaneously keeps track of a
7
+ /// corresponding environment variable name as well as a TOML config name. The
8
+ /// intention here is that this is built up and torn down over time efficiently,
9
+ /// avoiding clones and such as possible.
4
10
#[ derive( Debug , Clone ) ]
5
11
pub struct ConfigKey {
12
+ // The current environment variable this configuration key maps to. This is
13
+ // updated with `push` methods and looks like `CARGO_FOO_BAR` for pushing
14
+ // `foo` and then `bar`.
6
15
env : String ,
16
+ // The current environment variable this configuration key maps to. This is
17
+ // updated with `push` methods and looks like `foo.bar` for pushing
18
+ // `foo` and then `bar`.
7
19
config : String ,
20
+ // This is used to keep track of how many sub-keys have been pushed on this
21
+ // `ConfigKey`. Each element of this vector is a new sub-key pushed onto
22
+ // this `ConfigKey`. Each element is a pair of `usize` where the first item
23
+ // is an index into `env` and the second item is an index into `config`.
24
+ // These indices are used on `pop` to truncate `env` and `config` to rewind
25
+ // back to the previous `ConfigKey` state before a `push`.
8
26
parts : Vec < ( usize , usize ) > ,
9
27
}
10
28
11
29
impl ConfigKey {
30
+ /// Creates a new blank configuration key which is ready to get built up by
31
+ /// using `push` and `push_sensitive`.
12
32
pub fn new ( ) -> ConfigKey {
13
33
ConfigKey {
14
34
env : "CARGO" . to_string ( ) ,
@@ -17,6 +37,10 @@ impl ConfigKey {
17
37
}
18
38
}
19
39
40
+ /// Creates a `ConfigKey` from the `key` specified.
41
+ ///
42
+ /// The `key` specified is expected to be a period-separated toml
43
+ /// configuration key.
20
44
pub fn from_str ( key : & str ) -> ConfigKey {
21
45
let mut cfg = ConfigKey :: new ( ) ;
22
46
for part in key. split ( '.' ) {
@@ -25,11 +49,22 @@ impl ConfigKey {
25
49
return cfg;
26
50
}
27
51
52
+ /// Pushes a new sub-key on this `ConfigKey`. This sub-key should be
53
+ /// equivalent to accessing a sub-table in TOML.
54
+ ///
55
+ /// Note that this considers `name` to be case-insensitive, meaning that the
56
+ /// corrseponding toml key is appended with this `name` as-is and the
57
+ /// corresponding env key is appended with `name` after transforming it to
58
+ /// uppercase characters.
28
59
pub fn push ( & mut self , name : & str ) {
29
60
let env = name. replace ( "-" , "_" ) . to_uppercase ( ) ;
30
61
self . _push ( & env, name) ;
31
62
}
32
63
64
+ /// Performs the same function as `push` except that the corresponding
65
+ /// environment variable does not get the uppercase letters of `name` but
66
+ /// instead `name` is pushed raw onto the corresponding environment
67
+ /// variable.
33
68
pub fn push_sensitive ( & mut self , name : & str ) {
34
69
self . _push ( name, name) ;
35
70
}
@@ -46,16 +81,22 @@ impl ConfigKey {
46
81
self . config . push_str ( config) ;
47
82
}
48
83
84
+ /// Rewinds this `ConfigKey` back to the state it was at before the last
85
+ /// `push` method being called.
49
86
pub fn pop ( & mut self ) {
50
87
let ( env, config) = self . parts . pop ( ) . unwrap ( ) ;
51
88
self . env . truncate ( env) ;
52
89
self . config . truncate ( config) ;
53
90
}
54
91
92
+ /// Returns the corresponding environment variable key for this
93
+ /// configuration value.
55
94
pub fn as_env_key ( & self ) -> & str {
56
95
& self . env
57
96
}
58
97
98
+ /// Returns the corresponding TOML (period-separated) key for this
99
+ /// configuration value.
59
100
pub fn as_config_key ( & self ) -> & str {
60
101
& self . config
61
102
}
0 commit comments