Skip to content

Commit b392e7b

Browse files
committed
Adding some config doc comments.
1 parent c7f6528 commit b392e7b

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/cargo/util/config/mod.rs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
//! Cargo's config system.
2+
//!
3+
//! The `Config` object contains general information about the environment,
4+
//! and provides access to Cargo's configuration files.
5+
//!
6+
//! ## Config value API
7+
//!
8+
//! The primary API for fetching user-defined config values is the
9+
//! `Config::get` method. It uses `serde` to translate config values to a
10+
//! target type.
11+
//!
12+
//! There are a variety of helper types for deserializing some common formats:
13+
//!
14+
//! - `value::Value`: This type provides access to the location where the
15+
//! config value was defined.
16+
//! - `ConfigRelativePath`: For a path that is relative to where it is
17+
//! defined.
18+
//! - `PathAndArgs`: Similar to `ConfigRelativePath`, but also supports a list
19+
//! of arguments, useful for programs to execute.
20+
//! - `StringList`: Get a value that is either a list or a whitespace split
21+
//! string.
22+
//!
23+
//! ## Map key recommendations
24+
//!
25+
//! Handling tables that have arbitrary keys can be tricky, particularly if it
26+
//! should support environment variables. In general, if possible, the caller
27+
//! should pass the full key path into the `get()` method so that the config
28+
//! deserializer can properly handle environment variables (which need to be
29+
//! uppercased, and dashes converted to underscores).
30+
//!
31+
//! A good example is the `[target]` table. The code will request
32+
//! `target.$TRIPLE` and the config system can then appropriately fetch
33+
//! environment variables like `CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER`.
34+
//! Conversely, it is not possible do the same thing for the `cfg()` target
35+
//! tables (because Cargo must fetch all of them), so those do not support
36+
//! environment variables.
37+
//!
38+
//! ## Internal API
39+
//!
40+
//! Internally config values are stored with the `ConfigValue` type after they
41+
//! have been loaded from disk. This is similar to the `toml::Value` type, but
42+
//! includes the definition location. The `get()` method uses serde to
43+
//! translate from `ConfigValue` and environment variables to the caller's
44+
//! desired type.
45+
146
use std::cell::{RefCell, RefMut};
247
use std::collections::hash_map::Entry::{Occupied, Vacant};
348
use std::collections::{HashMap, HashSet};
@@ -321,6 +366,7 @@ impl Config {
321366
.map(AsRef::as_ref)
322367
}
323368

369+
/// Gets profiles defined in config files.
324370
pub fn profiles(&self) -> CargoResult<&ConfigProfiles> {
325371
self.profiles.try_borrow_with(|| {
326372
let ocp = self.get::<Option<ConfigProfiles>>("profile")?;
@@ -340,16 +386,28 @@ impl Config {
340386
})
341387
}
342388

389+
/// Which package sources have been updated, used to ensure it is only done once.
343390
pub fn updated_sources(&self) -> RefMut<'_, HashSet<SourceId>> {
344391
self.updated_sources
345392
.borrow_with(|| RefCell::new(HashSet::new()))
346393
.borrow_mut()
347394
}
348395

396+
/// Gets all config values from disk.
397+
///
398+
/// This will lazy-load the values as necessary. Callers are responsible
399+
/// for checking environment variables. Callers outside of the `config`
400+
/// module should avoid using this.
349401
pub fn values(&self) -> CargoResult<&HashMap<String, ConfigValue>> {
350402
self.values.try_borrow_with(|| self.load_values())
351403
}
352404

405+
/// Gets a mutable copy of the on-disk config values.
406+
///
407+
/// This requires the config values to already have been loaded. This
408+
/// currently only exists for `cargo vendor` to remove the `source`
409+
/// entries. This doesn't respect environment variables. You should avoid
410+
/// using this if possible.
353411
pub fn values_mut(&mut self) -> CargoResult<&mut HashMap<String, ConfigValue>> {
354412
match self.values.borrow_mut() {
355413
Some(map) => Ok(map),
@@ -368,17 +426,25 @@ impl Config {
368426
}
369427
}
370428

429+
/// Reloads on-disk configuration values, starting at the given path and
430+
/// walking up its ancestors.
371431
pub fn reload_rooted_at<P: AsRef<Path>>(&mut self, path: P) -> CargoResult<()> {
372432
let values = self.load_values_from(path.as_ref())?;
373433
self.values.replace(values);
374434
self.merge_cli_args()?;
375435
Ok(())
376436
}
377437

438+
/// The current working directory.
378439
pub fn cwd(&self) -> &Path {
379440
&self.cwd
380441
}
381442

443+
/// The `target` output directory to use.
444+
///
445+
/// Returns `None` if the user has not chosen an explicit directory.
446+
///
447+
/// Callers should prefer `Workspace::target_dir` instead.
382448
pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
383449
if let Some(dir) = &self.target_dir {
384450
Ok(Some(dir.clone()))
@@ -430,7 +496,7 @@ impl Config {
430496
Ok(Some(val.clone()))
431497
}
432498

433-
// Helper primarily for testing.
499+
/// Helper primarily for testing.
434500
pub fn set_env(&mut self, env: HashMap<String, String>) {
435501
self.env = env;
436502
}
@@ -477,7 +543,11 @@ impl Config {
477543
self.get::<Option<Value<String>>>(key)
478544
}
479545

480-
/// Get a config value that is expected to be a
546+
/// Get a config value that is expected to be a path.
547+
///
548+
/// This returns a relative path if the value does not contain any
549+
/// directory separators. See `ConfigRelativePath::resolve_program` for
550+
/// more details.
481551
pub fn get_path(&self, key: &str) -> CargoResult<OptValue<PathBuf>> {
482552
self.get::<Option<Value<ConfigRelativePath>>>(key).map(|v| {
483553
v.map(|v| Value {
@@ -532,6 +602,7 @@ impl Config {
532602
get_value_typed! {get_bool, bool, Boolean, "true/false"}
533603
get_value_typed! {get_string_priv, String, String, "a string"}
534604

605+
/// Generate an error when the given value is the wrong type.
535606
fn expected<T>(&self, ty: &str, key: &ConfigKey, val: &CV) -> CargoResult<T> {
536607
val.expected(ty, &key.to_string())
537608
.map_err(|e| failure::format_err!("invalid configuration for key `{}`\n{}", key, e))

src/cargo/util/config/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct TargetConfig {
2727
pub rustflags: OptValue<StringList>,
2828
/// The path of the linker for this target.
2929
pub linker: OptValue<ConfigRelativePath>,
30-
/// The path of archiver (lib builder) for this target.
30+
/// The path of archiver (lib builder) for this target. DEPRECATED/UNUSED
3131
pub ar: OptValue<ConfigRelativePath>,
3232
/// Build script override for the given library name.
3333
///

src/cargo/util/config/value.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ pub enum Definition {
5959
}
6060

6161
impl Definition {
62+
/// Root directory where this is defined.
63+
///
64+
/// If from a file, it is the directory above `.cargo/config`.
65+
/// CLI and env are the current working directory.
6266
pub fn root<'a>(&'a self, config: &'a Config) -> &'a Path {
6367
match self {
6468
Definition::Path(p) => p.parent().unwrap().parent().unwrap(),

0 commit comments

Comments
 (0)