Skip to content

Commit d6455a2

Browse files
committed
refactor(toml): use macro to reduce boilerplates
1 parent 5febbe5 commit d6455a2

File tree

1 file changed

+36
-118
lines changed

1 file changed

+36
-118
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 36 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,43 @@ pub struct InheritableFields {
15951595
ws_root: PathBuf,
15961596
}
15971597

1598+
/// Defines simple getter methods for inheritable fields.
1599+
macro_rules! inheritable_field_getter {
1600+
( $(($key:literal, $field:ident -> $ret:ty),)* ) => (
1601+
$(
1602+
#[doc = concat!("Gets the field `workspace.", $key, "`.")]
1603+
pub fn $field(&self) -> CargoResult<$ret> {
1604+
let Some(val) = &self.$field else {
1605+
bail!("`workspace.{}` was not defined", $key);
1606+
};
1607+
Ok(val.clone())
1608+
}
1609+
)*
1610+
)
1611+
}
1612+
15981613
impl InheritableFields {
1614+
inheritable_field_getter! {
1615+
// Please keep this list lexicographically ordered.
1616+
("dependencies", dependencies -> BTreeMap<String, TomlDependency>),
1617+
("lints", lints -> TomlLints),
1618+
("package.authors", authors -> Vec<String>),
1619+
("package.badges", badges -> BTreeMap<String, BTreeMap<String, String>>),
1620+
("package.categories", categories -> Vec<String>),
1621+
("package.description", description -> String),
1622+
("package.documentation", documentation -> String),
1623+
("package.edition", edition -> String),
1624+
("package.exclude", exclude -> Vec<String>),
1625+
("package.homepage", homepage -> String),
1626+
("package.include", include -> Vec<String>),
1627+
("package.keywords", keywords -> Vec<String>),
1628+
("package.license", license -> String),
1629+
("package.publish", publish -> VecStringOrBool),
1630+
("package.repository", repository -> String),
1631+
("package.rust-version", rust_version -> String),
1632+
("package.version", version -> semver::Version),
1633+
}
1634+
15991635
pub fn update_deps(&mut self, deps: Option<BTreeMap<String, TomlDependency>>) {
16001636
self.dependencies = deps;
16011637
}
@@ -1608,19 +1644,6 @@ impl InheritableFields {
16081644
self.ws_root = ws_root;
16091645
}
16101646

1611-
pub fn dependencies(&self) -> CargoResult<BTreeMap<String, TomlDependency>> {
1612-
self.dependencies.clone().map_or(
1613-
Err(anyhow!("`workspace.dependencies` was not defined")),
1614-
|d| Ok(d),
1615-
)
1616-
}
1617-
1618-
pub fn lints(&self) -> CargoResult<TomlLints> {
1619-
self.lints
1620-
.clone()
1621-
.map_or(Err(anyhow!("`workspace.lints` was not defined")), |d| Ok(d))
1622-
}
1623-
16241647
pub fn get_dependency(&self, name: &str, package_root: &Path) -> CargoResult<TomlDependency> {
16251648
self.dependencies.clone().map_or(
16261649
Err(anyhow!("`workspace.dependencies` was not defined")),
@@ -1642,41 +1665,6 @@ impl InheritableFields {
16421665
)
16431666
}
16441667

1645-
pub fn version(&self) -> CargoResult<semver::Version> {
1646-
self.version.clone().map_or(
1647-
Err(anyhow!("`workspace.package.version` was not defined")),
1648-
|d| Ok(d),
1649-
)
1650-
}
1651-
1652-
pub fn authors(&self) -> CargoResult<Vec<String>> {
1653-
self.authors.clone().map_or(
1654-
Err(anyhow!("`workspace.package.authors` was not defined")),
1655-
|d| Ok(d),
1656-
)
1657-
}
1658-
1659-
pub fn description(&self) -> CargoResult<String> {
1660-
self.description.clone().map_or(
1661-
Err(anyhow!("`workspace.package.description` was not defined")),
1662-
|d| Ok(d),
1663-
)
1664-
}
1665-
1666-
pub fn homepage(&self) -> CargoResult<String> {
1667-
self.homepage.clone().map_or(
1668-
Err(anyhow!("`workspace.package.homepage` was not defined")),
1669-
|d| Ok(d),
1670-
)
1671-
}
1672-
1673-
pub fn documentation(&self) -> CargoResult<String> {
1674-
self.documentation.clone().map_or(
1675-
Err(anyhow!("`workspace.package.documentation` was not defined")),
1676-
|d| Ok(d),
1677-
)
1678-
}
1679-
16801668
pub fn readme(&self, package_root: &Path) -> CargoResult<StringOrBool> {
16811669
readme_for_package(self.ws_root.as_path(), self.readme.clone()).map_or(
16821670
Err(anyhow!("`workspace.package.readme` was not defined")),
@@ -1688,83 +1676,13 @@ impl InheritableFields {
16881676
)
16891677
}
16901678

1691-
pub fn keywords(&self) -> CargoResult<Vec<String>> {
1692-
self.keywords.clone().map_or(
1693-
Err(anyhow!("`workspace.package.keywords` was not defined")),
1694-
|d| Ok(d),
1695-
)
1696-
}
1697-
1698-
pub fn categories(&self) -> CargoResult<Vec<String>> {
1699-
self.categories.clone().map_or(
1700-
Err(anyhow!("`workspace.package.categories` was not defined")),
1701-
|d| Ok(d),
1702-
)
1703-
}
1704-
1705-
pub fn license(&self) -> CargoResult<String> {
1706-
self.license.clone().map_or(
1707-
Err(anyhow!("`workspace.package.license` was not defined")),
1708-
|d| Ok(d),
1709-
)
1710-
}
1711-
17121679
pub fn license_file(&self, package_root: &Path) -> CargoResult<String> {
17131680
self.license_file.clone().map_or(
17141681
Err(anyhow!("`workspace.package.license_file` was not defined")),
17151682
|d| resolve_relative_path("license-file", &self.ws_root, package_root, &d),
17161683
)
17171684
}
17181685

1719-
pub fn repository(&self) -> CargoResult<String> {
1720-
self.repository.clone().map_or(
1721-
Err(anyhow!("`workspace.package.repository` was not defined")),
1722-
|d| Ok(d),
1723-
)
1724-
}
1725-
1726-
pub fn publish(&self) -> CargoResult<VecStringOrBool> {
1727-
self.publish.clone().map_or(
1728-
Err(anyhow!("`workspace.package.publish` was not defined")),
1729-
|d| Ok(d),
1730-
)
1731-
}
1732-
1733-
pub fn edition(&self) -> CargoResult<String> {
1734-
self.edition.clone().map_or(
1735-
Err(anyhow!("`workspace.package.edition` was not defined")),
1736-
|d| Ok(d),
1737-
)
1738-
}
1739-
1740-
pub fn rust_version(&self) -> CargoResult<String> {
1741-
self.rust_version.clone().map_or(
1742-
Err(anyhow!("`workspace.package.rust-version` was not defined")),
1743-
|d| Ok(d),
1744-
)
1745-
}
1746-
1747-
pub fn badges(&self) -> CargoResult<BTreeMap<String, BTreeMap<String, String>>> {
1748-
self.badges.clone().map_or(
1749-
Err(anyhow!("`workspace.package.badges` was not defined")),
1750-
|d| Ok(d),
1751-
)
1752-
}
1753-
1754-
pub fn exclude(&self) -> CargoResult<Vec<String>> {
1755-
self.exclude.clone().map_or(
1756-
Err(anyhow!("`workspace.package.exclude` was not defined")),
1757-
|d| Ok(d),
1758-
)
1759-
}
1760-
1761-
pub fn include(&self) -> CargoResult<Vec<String>> {
1762-
self.include.clone().map_or(
1763-
Err(anyhow!("`workspace.package.include` was not defined")),
1764-
|d| Ok(d),
1765-
)
1766-
}
1767-
17681686
pub fn ws_root(&self) -> &PathBuf {
17691687
&self.ws_root
17701688
}

0 commit comments

Comments
 (0)