Skip to content

Commit 2387e1a

Browse files
committed
Document the new ConfigKey implementation
1 parent 0a9a46e commit 2387e1a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/cargo/util/config/key.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
use std::fmt;
22

33
/// 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.
410
#[derive(Debug, Clone)]
511
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`.
615
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`.
719
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`.
826
parts: Vec<(usize, usize)>,
927
}
1028

1129
impl ConfigKey {
30+
/// Creates a new blank configuration key which is ready to get built up by
31+
/// using `push` and `push_sensitive`.
1232
pub fn new() -> ConfigKey {
1333
ConfigKey {
1434
env: "CARGO".to_string(),
@@ -17,6 +37,10 @@ impl ConfigKey {
1737
}
1838
}
1939

40+
/// Creates a `ConfigKey` from the `key` specified.
41+
///
42+
/// The `key` specified is expected to be a period-separated toml
43+
/// configuration key.
2044
pub fn from_str(key: &str) -> ConfigKey {
2145
let mut cfg = ConfigKey::new();
2246
for part in key.split('.') {
@@ -25,11 +49,22 @@ impl ConfigKey {
2549
return cfg;
2650
}
2751

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.
2859
pub fn push(&mut self, name: &str) {
2960
let env = name.replace("-", "_").to_uppercase();
3061
self._push(&env, name);
3162
}
3263

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.
3368
pub fn push_sensitive(&mut self, name: &str) {
3469
self._push(name, name);
3570
}
@@ -46,16 +81,22 @@ impl ConfigKey {
4681
self.config.push_str(config);
4782
}
4883

84+
/// Rewinds this `ConfigKey` back to the state it was at before the last
85+
/// `push` method being called.
4986
pub fn pop(&mut self) {
5087
let (env, config) = self.parts.pop().unwrap();
5188
self.env.truncate(env);
5289
self.config.truncate(config);
5390
}
5491

92+
/// Returns the corresponding environment variable key for this
93+
/// configuration value.
5594
pub fn as_env_key(&self) -> &str {
5695
&self.env
5796
}
5897

98+
/// Returns the corresponding TOML (period-separated) key for this
99+
/// configuration value.
59100
pub fn as_config_key(&self) -> &str {
60101
&self.config
61102
}

0 commit comments

Comments
 (0)