Skip to content

Commit

Permalink
Auto merge of #5552 - ehuss:config-serde, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed May 30, 2018
2 parents a5b13e8 + b3db164 commit 5fc6ead
Show file tree
Hide file tree
Showing 8 changed files with 1,344 additions and 128 deletions.
21 changes: 1 addition & 20 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,7 @@ impl BuildConfig {
its environment, ignoring the `-j` parameter",
)?;
}
let cfg_jobs = match config.get_i64("build.jobs")? {
Some(v) => {
if v.val <= 0 {
bail!(
"build.jobs must be positive, but found {} in {}",
v.val,
v.definition
)
} else if v.val >= i64::from(u32::max_value()) {
bail!(
"build.jobs is too large: found {} in {}",
v.val,
v.definition
)
} else {
Some(v.val as u32)
}
}
None => None,
};
let cfg_jobs: Option<u32> = config.get("build.jobs")?;
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
Ok(BuildConfig {
requested_target: target,
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ pub struct CliUnstable {
pub avoid_dev_deps: bool,
pub minimal_versions: bool,
pub package_features: bool,
pub advanced_env: bool,
}

impl CliUnstable {
Expand Down Expand Up @@ -342,6 +343,7 @@ impl CliUnstable {
"avoid-dev-deps" => self.avoid_dev_deps = true,
"minimal-versions" => self.minimal_versions = true,
"package-features" => self.package_features = true,
"advanced-env" => self.advanced_env = true,
_ => bail!("unknown `-Z` flag specified: {}", k),
}

Expand Down
1 change: 1 addition & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern crate log;
extern crate num_cpus;
extern crate same_file;
extern crate semver;
#[macro_use]
extern crate serde;
#[macro_use]
extern crate serde_derive;
Expand Down
Loading

0 comments on commit 5fc6ead

Please sign in to comment.