@@ -380,7 +380,7 @@ impl schema::TomlManifest {
380
380
config : & Config ,
381
381
resolved_path : & Path ,
382
382
workspace_config : & WorkspaceConfig ,
383
- ) -> CargoResult < schema :: InheritableFields > {
383
+ ) -> CargoResult < InheritableFields > {
384
384
match workspace_config {
385
385
WorkspaceConfig :: Root ( root) => Ok ( root. inheritable ( ) . clone ( ) ) ,
386
386
WorkspaceConfig :: Member {
@@ -446,12 +446,14 @@ impl schema::TomlManifest {
446
446
447
447
let workspace_config = match ( me. workspace . as_ref ( ) , package. workspace . as_ref ( ) ) {
448
448
( 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 ( ) ) ;
452
449
let lints = toml_config. lints . clone ( ) ;
453
450
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
+ } ;
455
457
if let Some ( ws_deps) = & inheritable. dependencies {
456
458
for ( name, dep) in ws_deps {
457
459
unused_dep_keys (
@@ -494,7 +496,7 @@ impl schema::TomlManifest {
494
496
495
497
let resolved_path = package_root. join ( "Cargo.toml" ) ;
496
498
497
- let inherit_cell: LazyCell < schema :: InheritableFields > = LazyCell :: new ( ) ;
499
+ let inherit_cell: LazyCell < InheritableFields > = LazyCell :: new ( ) ;
498
500
let inherit =
499
501
|| inherit_cell. try_borrow_with ( || get_ws ( config, & resolved_path, & workspace_config) ) ;
500
502
@@ -645,7 +647,7 @@ impl schema::TomlManifest {
645
647
new_deps : Option < & BTreeMap < String , schema:: InheritableDependency > > ,
646
648
kind : Option < DepKind > ,
647
649
workspace_config : & WorkspaceConfig ,
648
- inherit_cell : & LazyCell < schema :: InheritableFields > ,
650
+ inherit_cell : & LazyCell < InheritableFields > ,
649
651
) -> CargoResult < Option < BTreeMap < String , schema:: InheritableDependency > > > {
650
652
let Some ( dependencies) = new_deps else {
651
653
return Ok ( None ) ;
@@ -1164,12 +1166,14 @@ impl schema::TomlManifest {
1164
1166
. transpose ( ) ?;
1165
1167
let workspace_config = match me. workspace {
1166
1168
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 ( ) ) ;
1170
1169
let lints = toml_config. lints . clone ( ) ;
1171
1170
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
+ } ;
1173
1177
let ws_root_config = WorkspaceRootConfig :: new (
1174
1178
root,
1175
1179
& toml_config. members ,
@@ -1363,7 +1367,7 @@ fn unused_dep_keys(
1363
1367
fn inheritable_from_path (
1364
1368
config : & Config ,
1365
1369
workspace_path : PathBuf ,
1366
- ) -> CargoResult < schema :: InheritableFields > {
1370
+ ) -> CargoResult < InheritableFields > {
1367
1371
// Workspace path should have Cargo.toml at the end
1368
1372
let workspace_path_root = workspace_path. parent ( ) . unwrap ( ) ;
1369
1373
@@ -1446,39 +1450,49 @@ fn unique_build_targets(
1446
1450
}
1447
1451
1448
1452
/// Defines simple getter methods for inheritable fields.
1449
- macro_rules! inheritable_field_getter {
1453
+ macro_rules! package_field_getter {
1450
1454
( $( ( $key: literal, $field: ident -> $ret: ty) , ) * ) => (
1451
1455
$(
1452
- #[ doc = concat!( "Gets the field `workspace." , $key, "`." ) ]
1456
+ #[ doc = concat!( "Gets the field `workspace.package " , $key, "`." ) ]
1453
1457
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) ;
1456
1460
} ;
1457
1461
Ok ( val. clone( ) )
1458
1462
}
1459
1463
) *
1460
1464
)
1461
1465
}
1462
1466
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 ! {
1465
1480
// 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 ) ,
1482
1496
}
1483
1497
1484
1498
/// Gets a workspace dependency with the `name`.
@@ -1500,17 +1514,28 @@ impl schema::InheritableFields {
1500
1514
Ok ( dep)
1501
1515
}
1502
1516
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
+
1503
1525
/// Gets the field `workspace.package.license-file`.
1504
1526
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 {
1506
1528
bail ! ( "`workspace.package.license-file` was not defined" ) ;
1507
1529
} ;
1508
1530
resolve_relative_path ( "license-file" , & self . _ws_root , package_root, license_file)
1509
1531
}
1510
1532
1511
1533
/// Gets the field `workspace.package.readme`.
1512
1534
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 {
1514
1539
bail ! ( "`workspace.package.readme` was not defined" ) ;
1515
1540
} ;
1516
1541
resolve_relative_path ( "readme" , & self . _ws_root , package_root, & readme)
@@ -1520,18 +1545,6 @@ impl schema::InheritableFields {
1520
1545
fn ws_root ( & self ) -> & PathBuf {
1521
1546
& self . _ws_root
1522
1547
}
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
- }
1535
1548
}
1536
1549
1537
1550
impl schema:: TomlPackage {
@@ -1587,7 +1600,7 @@ impl schema::TomlInheritedDependency {
1587
1600
fn inherit_with < ' a > (
1588
1601
& self ,
1589
1602
name : & str ,
1590
- inheritable : impl FnOnce ( ) -> CargoResult < & ' a schema :: InheritableFields > ,
1603
+ inheritable : impl FnOnce ( ) -> CargoResult < & ' a InheritableFields > ,
1591
1604
cx : & mut Context < ' _ , ' _ > ,
1592
1605
) -> CargoResult < schema:: TomlDependency > {
1593
1606
fn default_features_msg ( label : & str , ws_def_feat : Option < bool > , cx : & mut Context < ' _ , ' _ > ) {
0 commit comments