]> git.proxmox.com Git - cargo.git/commitdiff
Part 5 of RFC2906 - Add support for inheriting `rust-version`
authorScott Schafer <schaferjscott@gmail.com>
Wed, 13 Apr 2022 17:03:40 +0000 (12:03 -0500)
committerScott Schafer <schaferjscott@gmail.com>
Wed, 13 Apr 2022 17:03:40 +0000 (12:03 -0500)
src/cargo/core/workspace.rs
src/cargo/util/toml/mod.rs
tests/testsuite/inheritable_workspace_fields.rs

index d15dfa22c905db59f6371a8c878edc0f48cc4537..7b57b749e34f088674d0274078c2f1bf4aaf2ab6 100644 (file)
@@ -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,
 }
 
@@ -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 {
@@ -1700,6 +1702,7 @@ impl InheritableFields {
             publish,
             edition,
             badges,
+            rust_version,
             ws_root,
         }
     }
@@ -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()
index 78553de89722e2d6f57701f80edd1d32e18a7692..7778878beadf8b8e28848c7bffb281c91cf0925b 100644 (file)
@@ -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>,
@@ -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.
@@ -1376,6 +1378,7 @@ impl TomlManifest {
                     config.publish.clone(),
                     config.edition.clone(),
                     config.badges.clone(),
+                    config.rust_version.clone(),
                     package_root.to_path_buf(),
                 );
 
@@ -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\""),
@@ -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 {
@@ -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(
index 1b68e17116d01a63e2e3eba247eac1cfdce223fc..dc192942f14c2211993b8a809fae56dbf067f0b6 100644 (file)
@@ -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" }
@@ -130,6 +131,7 @@ fn inherit_own_workspace_fields() {
             categories = { workspace = true }
             publish = { workspace = true }
             edition = { workspace = true }
+            rust-version = { workspace = true }
 
             [workspace]
             members = []
@@ -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" }
             "#,
@@ -194,6 +197,7 @@ cargo-features = ["workspace-inheritance"]
 
 [package]
 edition = "2018"
+rust-version = "1.60"
 name = "foo"
 version = "1.2.3"
 authors = ["Rustaceans"]
@@ -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" }
             "#,
@@ -616,6 +621,7 @@ fn inherit_workspace_fields() {
             categories = { workspace = true }
             publish = { workspace = true }
             edition = { workspace = true }
+            rust-version = { workspace = true }
         "#,
         )
         .file("LICENSE", "license")
@@ -669,6 +675,7 @@ cargo-features = ["workspace-inheritance"]
 
 [package]
 edition = "2018"
+rust-version = "1.60"
 name = "bar"
 version = "1.2.3"
 authors = ["Rustaceans"]