diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 51d58f5f5d4..d235c7108d4 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1595,7 +1595,42 @@ pub struct InheritableFields { ws_root: PathBuf, } +/// Defines simple getter methods for inheritable fields. +macro_rules! inheritable_field_getter { + ( $(($path:literal, $field:ident -> $ret:ty),)* ) => ( + $( + #[doc = concat!("Gets the field `workspace.", $path, "`.")] + pub fn $field(&self) -> CargoResult<$ret> { + if let Some(val) = &self.$field { + return Ok(val.clone()) + } + bail!("`workspace.{}` was not defined", $path); + } + )* + ) +} + impl InheritableFields { + inheritable_field_getter! { + ("dependencies", dependencies -> BTreeMap), + ("lints", lints -> TomlLints), + ("package.authors", authors -> Vec), + ("package.badges", badges -> BTreeMap>), + ("package.categories", categories -> Vec), + ("package.description", description -> String), + ("package.documentation", documentation -> String), + ("package.edition", edition -> String), + ("package.exclude", exclude -> Vec), + ("package.homepage", homepage -> String), + ("package.include", include -> Vec), + ("package.keywords", keywords -> Vec), + ("package.license", license -> String), + ("package.publish", publish -> VecStringOrBool), + ("package.repository", repository -> String), + ("package.rust-version", rust_version -> String), + ("package.version", version -> semver::Version), + } + pub fn update_deps(&mut self, deps: Option>) { self.dependencies = deps; } @@ -1608,19 +1643,6 @@ impl InheritableFields { self.ws_root = ws_root; } - pub fn dependencies(&self) -> CargoResult> { - self.dependencies.clone().map_or( - Err(anyhow!("`workspace.dependencies` was not defined")), - |d| Ok(d), - ) - } - - pub fn lints(&self) -> CargoResult { - self.lints - .clone() - .map_or(Err(anyhow!("`workspace.lints` was not defined")), |d| Ok(d)) - } - pub fn get_dependency(&self, name: &str, package_root: &Path) -> CargoResult { self.dependencies.clone().map_or( Err(anyhow!("`workspace.dependencies` was not defined")), @@ -1642,41 +1664,6 @@ impl InheritableFields { ) } - pub fn version(&self) -> CargoResult { - self.version.clone().map_or( - Err(anyhow!("`workspace.package.version` was not defined")), - |d| Ok(d), - ) - } - - pub fn authors(&self) -> CargoResult> { - self.authors.clone().map_or( - Err(anyhow!("`workspace.package.authors` was not defined")), - |d| Ok(d), - ) - } - - pub fn description(&self) -> CargoResult { - self.description.clone().map_or( - Err(anyhow!("`workspace.package.description` was not defined")), - |d| Ok(d), - ) - } - - pub fn homepage(&self) -> CargoResult { - self.homepage.clone().map_or( - Err(anyhow!("`workspace.package.homepage` was not defined")), - |d| Ok(d), - ) - } - - pub fn documentation(&self) -> CargoResult { - self.documentation.clone().map_or( - Err(anyhow!("`workspace.package.documentation` was not defined")), - |d| Ok(d), - ) - } - pub fn readme(&self, package_root: &Path) -> CargoResult { readme_for_package(self.ws_root.as_path(), self.readme.clone()).map_or( Err(anyhow!("`workspace.package.readme` was not defined")), @@ -1688,27 +1675,6 @@ impl InheritableFields { ) } - pub fn keywords(&self) -> CargoResult> { - self.keywords.clone().map_or( - Err(anyhow!("`workspace.package.keywords` was not defined")), - |d| Ok(d), - ) - } - - pub fn categories(&self) -> CargoResult> { - self.categories.clone().map_or( - Err(anyhow!("`workspace.package.categories` was not defined")), - |d| Ok(d), - ) - } - - pub fn license(&self) -> CargoResult { - self.license.clone().map_or( - Err(anyhow!("`workspace.package.license` was not defined")), - |d| Ok(d), - ) - } - pub fn license_file(&self, package_root: &Path) -> CargoResult { self.license_file.clone().map_or( Err(anyhow!("`workspace.package.license_file` was not defined")), @@ -1716,55 +1682,6 @@ impl InheritableFields { ) } - pub fn repository(&self) -> CargoResult { - self.repository.clone().map_or( - Err(anyhow!("`workspace.package.repository` was not defined")), - |d| Ok(d), - ) - } - - pub fn publish(&self) -> CargoResult { - self.publish.clone().map_or( - Err(anyhow!("`workspace.package.publish` was not defined")), - |d| Ok(d), - ) - } - - pub fn edition(&self) -> CargoResult { - self.edition.clone().map_or( - Err(anyhow!("`workspace.package.edition` was not defined")), - |d| Ok(d), - ) - } - - pub fn rust_version(&self) -> CargoResult { - self.rust_version.clone().map_or( - Err(anyhow!("`workspace.package.rust-version` was not defined")), - |d| Ok(d), - ) - } - - pub fn badges(&self) -> CargoResult>> { - self.badges.clone().map_or( - Err(anyhow!("`workspace.package.badges` was not defined")), - |d| Ok(d), - ) - } - - pub fn exclude(&self) -> CargoResult> { - self.exclude.clone().map_or( - Err(anyhow!("`workspace.package.exclude` was not defined")), - |d| Ok(d), - ) - } - - pub fn include(&self) -> CargoResult> { - self.include.clone().map_or( - Err(anyhow!("`workspace.package.include` was not defined")), - |d| Ok(d), - ) - } - pub fn ws_root(&self) -> &PathBuf { &self.ws_root }