Skip to content

Commit 12b9fd4

Browse files
committed
refactor(toml): Split out TomlPackageTemplate from InheritableFields
This allows us to move bookkeeping / logic out of the schema
1 parent 0eda16f commit 12b9fd4

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed

src/cargo/core/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub use self::workspace::{
1414
find_workspace_root, resolve_relative_path, MaybePackage, Workspace, WorkspaceConfig,
1515
WorkspaceRootConfig,
1616
};
17-
pub use crate::util::toml::schema::InheritableFields;
1817

1918
pub mod compiler;
2019
pub mod dependency;

src/cargo/core/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::util::edit_distance;
2323
use crate::util::errors::{CargoResult, ManifestError};
2424
use crate::util::interning::InternedString;
2525
use crate::util::toml::{
26-
read_manifest, schema::InheritableFields, schema::TomlDependency, schema::TomlProfiles,
26+
read_manifest, schema::TomlDependency, schema::TomlProfiles, InheritableFields,
2727
};
2828
use crate::util::RustVersion;
2929
use crate::util::{config::ConfigRelativePath, Config, Filesystem, IntoUrl};

src/cargo/util/toml/mod.rs

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl schema::TomlManifest {
380380
config: &Config,
381381
resolved_path: &Path,
382382
workspace_config: &WorkspaceConfig,
383-
) -> CargoResult<schema::InheritableFields> {
383+
) -> CargoResult<InheritableFields> {
384384
match workspace_config {
385385
WorkspaceConfig::Root(root) => Ok(root.inheritable().clone()),
386386
WorkspaceConfig::Member {
@@ -446,12 +446,14 @@ impl schema::TomlManifest {
446446

447447
let workspace_config = match (me.workspace.as_ref(), package.workspace.as_ref()) {
448448
(Some(toml_config), None) => {
449-
let mut inheritable = toml_config.package.clone().unwrap_or_default();
450-
inheritable.update_ws_path(package_root.to_path_buf());
451-
inheritable.update_deps(toml_config.dependencies.clone());
452449
let lints = toml_config.lints.clone();
453450
let lints = verify_lints(lints)?;
454-
inheritable.update_lints(lints);
451+
let inheritable = InheritableFields {
452+
package: toml_config.package.clone(),
453+
dependencies: toml_config.dependencies.clone(),
454+
lints,
455+
_ws_root: package_root.to_path_buf(),
456+
};
455457
if let Some(ws_deps) = &inheritable.dependencies {
456458
for (name, dep) in ws_deps {
457459
unused_dep_keys(
@@ -494,7 +496,7 @@ impl schema::TomlManifest {
494496

495497
let resolved_path = package_root.join("Cargo.toml");
496498

497-
let inherit_cell: LazyCell<schema::InheritableFields> = LazyCell::new();
499+
let inherit_cell: LazyCell<InheritableFields> = LazyCell::new();
498500
let inherit =
499501
|| inherit_cell.try_borrow_with(|| get_ws(config, &resolved_path, &workspace_config));
500502

@@ -645,7 +647,7 @@ impl schema::TomlManifest {
645647
new_deps: Option<&BTreeMap<String, schema::InheritableDependency>>,
646648
kind: Option<DepKind>,
647649
workspace_config: &WorkspaceConfig,
648-
inherit_cell: &LazyCell<schema::InheritableFields>,
650+
inherit_cell: &LazyCell<InheritableFields>,
649651
) -> CargoResult<Option<BTreeMap<String, schema::InheritableDependency>>> {
650652
let Some(dependencies) = new_deps else {
651653
return Ok(None);
@@ -1164,12 +1166,14 @@ impl schema::TomlManifest {
11641166
.transpose()?;
11651167
let workspace_config = match me.workspace {
11661168
Some(ref toml_config) => {
1167-
let mut inheritable = toml_config.package.clone().unwrap_or_default();
1168-
inheritable.update_ws_path(root.to_path_buf());
1169-
inheritable.update_deps(toml_config.dependencies.clone());
11701169
let lints = toml_config.lints.clone();
11711170
let lints = verify_lints(lints)?;
1172-
inheritable.update_lints(lints);
1171+
let inheritable = InheritableFields {
1172+
package: toml_config.package.clone(),
1173+
dependencies: toml_config.dependencies.clone(),
1174+
lints,
1175+
_ws_root: root.to_path_buf(),
1176+
};
11731177
let ws_root_config = WorkspaceRootConfig::new(
11741178
root,
11751179
&toml_config.members,
@@ -1363,7 +1367,7 @@ fn unused_dep_keys(
13631367
fn inheritable_from_path(
13641368
config: &Config,
13651369
workspace_path: PathBuf,
1366-
) -> CargoResult<schema::InheritableFields> {
1370+
) -> CargoResult<InheritableFields> {
13671371
// Workspace path should have Cargo.toml at the end
13681372
let workspace_path_root = workspace_path.parent().unwrap();
13691373

@@ -1446,39 +1450,49 @@ fn unique_build_targets(
14461450
}
14471451

14481452
/// Defines simple getter methods for inheritable fields.
1449-
macro_rules! inheritable_field_getter {
1453+
macro_rules! package_field_getter {
14501454
( $(($key:literal, $field:ident -> $ret:ty),)* ) => (
14511455
$(
1452-
#[doc = concat!("Gets the field `workspace.", $key, "`.")]
1456+
#[doc = concat!("Gets the field `workspace.package", $key, "`.")]
14531457
fn $field(&self) -> CargoResult<$ret> {
1454-
let Some(val) = &self.$field else {
1455-
bail!("`workspace.{}` was not defined", $key);
1458+
let Some(val) = self.package.as_ref().and_then(|p| p.$field.as_ref()) else {
1459+
bail!("`workspace.package.{}` was not defined", $key);
14561460
};
14571461
Ok(val.clone())
14581462
}
14591463
)*
14601464
)
14611465
}
14621466

1463-
impl schema::InheritableFields {
1464-
inheritable_field_getter! {
1467+
/// A group of fields that are inheritable by members of the workspace
1468+
#[derive(Clone, Debug, Default)]
1469+
pub struct InheritableFields {
1470+
package: Option<schema::TomlPackageTemplate>,
1471+
dependencies: Option<BTreeMap<String, schema::TomlDependency>>,
1472+
lints: Option<schema::TomlLints>,
1473+
1474+
// Bookkeeping to help when resolving values from above
1475+
_ws_root: PathBuf,
1476+
}
1477+
1478+
impl InheritableFields {
1479+
package_field_getter! {
14651480
// Please keep this list lexicographically ordered.
1466-
("lints", lints -> schema::TomlLints),
1467-
("package.authors", authors -> Vec<String>),
1468-
("package.badges", badges -> BTreeMap<String, BTreeMap<String, String>>),
1469-
("package.categories", categories -> Vec<String>),
1470-
("package.description", description -> String),
1471-
("package.documentation", documentation -> String),
1472-
("package.edition", edition -> String),
1473-
("package.exclude", exclude -> Vec<String>),
1474-
("package.homepage", homepage -> String),
1475-
("package.include", include -> Vec<String>),
1476-
("package.keywords", keywords -> Vec<String>),
1477-
("package.license", license -> String),
1478-
("package.publish", publish -> schema::VecStringOrBool),
1479-
("package.repository", repository -> String),
1480-
("package.rust-version", rust_version -> RustVersion),
1481-
("package.version", version -> semver::Version),
1481+
("authors", authors -> Vec<String>),
1482+
("badges", badges -> BTreeMap<String, BTreeMap<String, String>>),
1483+
("categories", categories -> Vec<String>),
1484+
("description", description -> String),
1485+
("documentation", documentation -> String),
1486+
("edition", edition -> String),
1487+
("exclude", exclude -> Vec<String>),
1488+
("homepage", homepage -> String),
1489+
("include", include -> Vec<String>),
1490+
("keywords", keywords -> Vec<String>),
1491+
("license", license -> String),
1492+
("publish", publish -> schema::VecStringOrBool),
1493+
("repository", repository -> String),
1494+
("rust-version", rust_version -> RustVersion),
1495+
("version", version -> semver::Version),
14821496
}
14831497

14841498
/// Gets a workspace dependency with the `name`.
@@ -1500,17 +1514,28 @@ impl schema::InheritableFields {
15001514
Ok(dep)
15011515
}
15021516

1517+
/// Gets the field `workspace.lint`.
1518+
fn lints(&self) -> CargoResult<schema::TomlLints> {
1519+
let Some(val) = &self.lints else {
1520+
bail!("`workspace.lints` was not defined");
1521+
};
1522+
Ok(val.clone())
1523+
}
1524+
15031525
/// Gets the field `workspace.package.license-file`.
15041526
fn license_file(&self, package_root: &Path) -> CargoResult<String> {
1505-
let Some(license_file) = &self.license_file else {
1527+
let Some(license_file) = self.package.as_ref().and_then(|p| p.license_file.as_ref()) else {
15061528
bail!("`workspace.package.license-file` was not defined");
15071529
};
15081530
resolve_relative_path("license-file", &self._ws_root, package_root, license_file)
15091531
}
15101532

15111533
/// Gets the field `workspace.package.readme`.
15121534
fn readme(&self, package_root: &Path) -> CargoResult<schema::StringOrBool> {
1513-
let Some(readme) = readme_for_package(self._ws_root.as_path(), self.readme.as_ref()) else {
1535+
let Some(readme) = readme_for_package(
1536+
self._ws_root.as_path(),
1537+
self.package.as_ref().and_then(|p| p.readme.as_ref()),
1538+
) else {
15141539
bail!("`workspace.package.readme` was not defined");
15151540
};
15161541
resolve_relative_path("readme", &self._ws_root, package_root, &readme)
@@ -1520,18 +1545,6 @@ impl schema::InheritableFields {
15201545
fn ws_root(&self) -> &PathBuf {
15211546
&self._ws_root
15221547
}
1523-
1524-
fn update_deps(&mut self, deps: Option<BTreeMap<String, schema::TomlDependency>>) {
1525-
self.dependencies = deps;
1526-
}
1527-
1528-
fn update_lints(&mut self, lints: Option<schema::TomlLints>) {
1529-
self.lints = lints;
1530-
}
1531-
1532-
fn update_ws_path(&mut self, ws_root: PathBuf) {
1533-
self._ws_root = ws_root;
1534-
}
15351548
}
15361549

15371550
impl schema::TomlPackage {
@@ -1587,7 +1600,7 @@ impl schema::TomlInheritedDependency {
15871600
fn inherit_with<'a>(
15881601
&self,
15891602
name: &str,
1590-
inheritable: impl FnOnce() -> CargoResult<&'a schema::InheritableFields>,
1603+
inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>,
15911604
cx: &mut Context<'_, '_>,
15921605
) -> CargoResult<schema::TomlDependency> {
15931606
fn default_features_msg(label: &str, ws_def_feat: Option<bool>, cx: &mut Context<'_, '_>) {

src/cargo/util/toml/schema.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,15 @@ pub struct TomlWorkspace {
8383
pub metadata: Option<toml::Value>,
8484

8585
// Properties that can be inherited by members.
86-
pub package: Option<InheritableFields>,
86+
pub package: Option<TomlPackageTemplate>,
8787
pub dependencies: Option<BTreeMap<String, TomlDependency>>,
8888
pub lints: Option<TomlLints>,
8989
}
9090

9191
/// A group of fields that are inheritable by members of the workspace
9292
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
9393
#[serde(rename_all = "kebab-case")]
94-
pub struct InheritableFields {
95-
// We use skip here since it will never be present when deserializing
96-
// and we don't want it present when serializing
97-
#[serde(skip)]
98-
pub dependencies: Option<BTreeMap<String, TomlDependency>>,
99-
#[serde(skip)]
100-
pub lints: Option<TomlLints>,
101-
94+
pub struct TomlPackageTemplate {
10295
pub version: Option<semver::Version>,
10396
pub authors: Option<Vec<String>>,
10497
pub description: Option<String>,
@@ -116,10 +109,6 @@ pub struct InheritableFields {
116109
pub exclude: Option<Vec<String>>,
117110
pub include: Option<Vec<String>>,
118111
pub rust_version: Option<RustVersion>,
119-
// We use skip here since it will never be present when deserializing
120-
// and we don't want it present when serializing
121-
#[serde(skip)]
122-
pub _ws_root: PathBuf,
123112
}
124113

125114
/// Represents the `package`/`project` sections of a `Cargo.toml`.

0 commit comments

Comments
 (0)