Skip to content

Commit 5fc6ead

Browse files
committed
Auto merge of #5552 - ehuss:config-serde, r=alexcrichton
Typed Config Access This introduces a new API for accessing config values using serde to automatically convert to a destination type. By itself this shouldn't introduce any behavioral changes (except for some slight wording changes to error messages). However, it unlocks the ability to use richer data types in the future (such as `profile`, or `source`). Example: ```rust let p: Option<TomlProfile> = config.get("profile.dev")?; ``` Supports environment variables when fetching structs or maps. Note that it can support underscores in env var for struct field names, but not maps. So for example, "opt_level" works, but not "serde_json" (example: `CARGO_PROFILE_DEV_OVERRIDES_serde_OPT_LEVEL`). I don't have any ideas for a workaround (though I feel this is an overuse of env vars). It supports environment variables for lists. The value in the env var will get appended to anything in the config. It uses TOML syntax, and currently only supports strings. Example: `CARGO_FOO=['a', 'b']`. I did *not* modify `get_list` to avoid changing behavior, but that can easily be changed.
2 parents a5b13e8 + b3db164 commit 5fc6ead

File tree

8 files changed

+1344
-128
lines changed

8 files changed

+1344
-128
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,7 @@ impl BuildConfig {
6262
its environment, ignoring the `-j` parameter",
6363
)?;
6464
}
65-
let cfg_jobs = match config.get_i64("build.jobs")? {
66-
Some(v) => {
67-
if v.val <= 0 {
68-
bail!(
69-
"build.jobs must be positive, but found {} in {}",
70-
v.val,
71-
v.definition
72-
)
73-
} else if v.val >= i64::from(u32::max_value()) {
74-
bail!(
75-
"build.jobs is too large: found {} in {}",
76-
v.val,
77-
v.definition
78-
)
79-
} else {
80-
Some(v.val as u32)
81-
}
82-
}
83-
None => None,
84-
};
65+
let cfg_jobs: Option<u32> = config.get("build.jobs")?;
8566
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
8667
Ok(BuildConfig {
8768
requested_target: target,

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub struct CliUnstable {
308308
pub avoid_dev_deps: bool,
309309
pub minimal_versions: bool,
310310
pub package_features: bool,
311+
pub advanced_env: bool,
311312
}
312313

313314
impl CliUnstable {
@@ -342,6 +343,7 @@ impl CliUnstable {
342343
"avoid-dev-deps" => self.avoid_dev_deps = true,
343344
"minimal-versions" => self.minimal_versions = true,
344345
"package-features" => self.package_features = true,
346+
"advanced-env" => self.advanced_env = true,
345347
_ => bail!("unknown `-Z` flag specified: {}", k),
346348
}
347349

src/cargo/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern crate log;
3232
extern crate num_cpus;
3333
extern crate same_file;
3434
extern crate semver;
35+
#[macro_use]
3536
extern crate serde;
3637
#[macro_use]
3738
extern crate serde_derive;

0 commit comments

Comments
 (0)