Skip to content

Commit

Permalink
Part 5 of RFC2906 - Add support for inheriting rust-version
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 13, 2022
1 parent 403c6bd commit 327976f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ pub struct InheritableFields {
publish: Option<VecStringOrBool>,
edition: Option<String>,
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
rust_version: Option<String>,
ws_root: PathBuf,
}

Expand All @@ -1682,6 +1683,7 @@ impl InheritableFields {
publish: Option<VecStringOrBool>,
edition: Option<String>,
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
rust_version: Option<String>,
ws_root: PathBuf,
) -> InheritableFields {
Self {
Expand All @@ -1700,6 +1702,7 @@ impl InheritableFields {
publish,
edition,
badges,
rust_version,
ws_root,
}
}
Expand Down Expand Up @@ -1828,6 +1831,13 @@ impl InheritableFields {
})
}

pub fn rust_version(&self) -> CargoResult<String> {
self.rust_version.clone().map_or(
Err(anyhow!("`workspace.rust-version` was not defined")),
|d| Ok(d),
)
}

pub fn badges(&self) -> CargoResult<BTreeMap<String, BTreeMap<String, String>>> {
self.badges
.clone()
Expand Down
14 changes: 12 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ pub struct TomlWorkspaceField {
#[serde(rename_all = "kebab-case")]
pub struct TomlProject {
edition: Option<MaybeWorkspace<String>>,
rust_version: Option<String>,
rust_version: Option<MaybeWorkspace<String>>,
name: InternedString,
#[serde(deserialize_with = "version_trim_whitespace")]
version: MaybeWorkspace<semver::Version>,
Expand Down Expand Up @@ -1111,6 +1111,8 @@ pub struct TomlWorkspace {
publish: Option<VecStringOrBool>,
edition: Option<String>,
badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
#[serde(rename = "rust-version")]
rust_version: Option<String>,

// Note that this field must come last due to the way toml serialization
// works which requires tables to be emitted after all values.
Expand Down Expand Up @@ -1376,6 +1378,7 @@ impl TomlManifest {
config.publish.clone(),
config.edition.clone(),
config.badges.clone(),
config.rust_version.clone(),
package_root.to_path_buf(),
);

Expand Down Expand Up @@ -1441,7 +1444,12 @@ impl TomlManifest {
}

let rust_version = if let Some(rust_version) = &project.rust_version {
let req = match semver::VersionReq::parse(rust_version) {
let rust_version = rust_version
.clone()
.resolve(&features, "rust_version", || {
get_ws(config, resolved_path.clone(), workspace_config.clone())?.rust_version()
})?;
let req = match semver::VersionReq::parse(&rust_version) {
// Exclude semver operators like `^` and pre-release identifiers
Ok(req) if rust_version.chars().all(|c| c.is_ascii_digit() || c == '.') => req,
_ => bail!("`rust-version` must be a value like \"1.32\""),
Expand Down Expand Up @@ -1843,6 +1851,7 @@ impl TomlManifest {
.categories
.as_ref()
.map(|_| MaybeWorkspace::Defined(metadata.categories.clone()));
project.rust_version = rust_version.clone().map(|rv| MaybeWorkspace::Defined(rv));

let profiles = me.profile.clone();
if let Some(profiles) = &profiles {
Expand Down Expand Up @@ -2064,6 +2073,7 @@ impl TomlManifest {
config.publish.clone(),
config.edition.clone(),
config.badges.clone(),
config.rust_version.clone(),
root.to_path_buf(),
);
WorkspaceConfig::Root(WorkspaceRootConfig::new(
Expand Down
7 changes: 7 additions & 0 deletions tests/testsuite/inheritable_workspace_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn permit_additional_workspace_fields() {
categories = ["development-tools"]
publish = false
edition = "2018"
rust-version = "1.60"
[workspace.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
Expand Down Expand Up @@ -130,6 +131,7 @@ fn inherit_own_workspace_fields() {
categories = { workspace = true }
publish = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
[workspace]
members = []
Expand All @@ -144,6 +146,7 @@ fn inherit_own_workspace_fields() {
categories = ["development-tools"]
publish = true
edition = "2018"
rust-version = "1.60"
[workspace.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
"#,
Expand Down Expand Up @@ -194,6 +197,7 @@ cargo-features = ["workspace-inheritance"]
[package]
edition = "2018"
rust-version = "1.60"
name = "foo"
version = "1.2.3"
authors = ["Rustaceans"]
Expand Down Expand Up @@ -590,6 +594,7 @@ fn inherit_workspace_fields() {
categories = ["development-tools"]
publish = true
edition = "2018"
rust-version = "1.60"
[workspace.badges]
gitlab = { repository = "https://gitlab.com/rust-lang/rust", branch = "master" }
"#,
Expand All @@ -616,6 +621,7 @@ fn inherit_workspace_fields() {
categories = { workspace = true }
publish = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
"#,
)
.file("LICENSE", "license")
Expand Down Expand Up @@ -669,6 +675,7 @@ cargo-features = ["workspace-inheritance"]
[package]
edition = "2018"
rust-version = "1.60"
name = "bar"
version = "1.2.3"
authors = ["Rustaceans"]
Expand Down

0 comments on commit 327976f

Please sign in to comment.