Skip to content

Commit 25d7318

Browse files
committed
feat(config-include): support inline and array of tables
Both are now supported ``` include = ['simple.toml', { path = 'other.toml' }] ``` ``` [[include]] path = 'first.toml' [[include]] path = 'second.toml' ``` So in the future we can extend field like `optional` or `if` in the include table On the implementation side, `ConfigInclude` intentionally doesn't derive serde deserialization to avoid any misuse of `GlobalContext::get::<ConfigInclude>()`, which might lead to wrong config loading order.
1 parent 2531164 commit 25d7318

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/cargo/util/context/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,8 +1400,20 @@ impl GlobalContext {
14001400
.enumerate()
14011401
.map(|(idx, cv)| match cv {
14021402
CV::String(s, def) => Ok(ConfigInclude::new(s, def)),
1403+
CV::Table(mut table, def) => {
1404+
// Extract `include.path`
1405+
let s = match table.remove("path") {
1406+
Some(CV::String(s, _)) => s,
1407+
Some(other) => bail!(
1408+
"expected a string, but found {} at `include[{idx}].path` in `{def}`",
1409+
other.desc()
1410+
),
1411+
None => bail!("missing field `path` at `include[{idx}]` in `{def}`"),
1412+
};
1413+
Ok(ConfigInclude::new(s, def))
1414+
}
14031415
other => bail!(
1404-
"expected a string, but found {} at `include[{idx}]` in `{}",
1416+
"expected a string or table, but found {} at `include[{idx}]` in {}",
14051417
other.desc(),
14061418
other.definition(),
14071419
),
@@ -2473,6 +2485,11 @@ pub fn save_credentials(
24732485
}
24742486
}
24752487

2488+
/// Represents a config-include value in the configuration.
2489+
///
2490+
/// This intentionally doesn't derive serde deserialization
2491+
/// to avoid any misuse of `GlobalContext::get::<ConfigInclude>()`,
2492+
/// which might lead to wrong config loading order.
24762493
struct ConfigInclude {
24772494
/// Path to a config-include configuration file.
24782495
/// Could be either relative or absolute.

tests/testsuite/config_include.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,13 @@ fn inline_table_style() {
520520
",
521521
);
522522

523-
// Currently this fails with an error
524523
let gctx = GlobalContextBuilder::new()
525524
.unstable_flag("config-include")
526-
.build_err();
527-
assert_error(
528-
gctx.unwrap_err(),
529-
str![[r#"
530-
could not load Cargo configuration
531-
532-
Caused by:
533-
expected a string, but found table at `include[1]` in `[ROOT]/.cargo/config.toml
534-
"#]],
535-
);
525+
.build();
526+
assert_eq!(gctx.get::<i32>("key1").unwrap(), 1);
527+
assert_eq!(gctx.get::<i32>("key2").unwrap(), 2);
528+
assert_eq!(gctx.get::<i32>("key3").unwrap(), 5);
529+
assert_eq!(gctx.get::<i32>("key4").unwrap(), 6);
536530
}
537531

538532
#[cargo_test]
@@ -565,19 +559,13 @@ fn array_of_tables_style() {
565559
",
566560
);
567561

568-
// Currently this also fails with an error
569562
let gctx = GlobalContextBuilder::new()
570563
.unstable_flag("config-include")
571-
.build_err();
572-
assert_error(
573-
gctx.unwrap_err(),
574-
str![[r#"
575-
could not load Cargo configuration
576-
577-
Caused by:
578-
expected a string, but found table at `include[0]` in `[ROOT]/.cargo/config.toml
579-
"#]],
580-
);
564+
.build();
565+
assert_eq!(gctx.get::<i32>("key1").unwrap(), 1);
566+
assert_eq!(gctx.get::<i32>("key2").unwrap(), 2);
567+
assert_eq!(gctx.get::<i32>("key3").unwrap(), 5);
568+
assert_eq!(gctx.get::<i32>("key4").unwrap(), 6);
581569
}
582570

583571
#[cargo_test]
@@ -603,16 +591,9 @@ fn table_with_unknown_fields() {
603591

604592
let gctx = GlobalContextBuilder::new()
605593
.unstable_flag("config-include")
606-
.build_err();
607-
assert_error(
608-
gctx.unwrap_err(),
609-
str![[r#"
610-
could not load Cargo configuration
611-
612-
Caused by:
613-
expected a string, but found table at `include[0]` in `[ROOT]/.cargo/config.toml
614-
"#]],
615-
);
594+
.build();
595+
assert_eq!(gctx.get::<i32>("key1").unwrap(), 1);
596+
assert_eq!(gctx.get::<i32>("key2").unwrap(), 2);
616597
}
617598

618599
#[cargo_test]
@@ -636,7 +617,7 @@ fn table_missing_required_field() {
636617
could not load Cargo configuration
637618
638619
Caused by:
639-
expected a string, but found table at `include[0]` in `[ROOT]/.cargo/config.toml
620+
missing field `path` at `include[0]` in `[ROOT]/.cargo/config.toml`
640621
"#]],
641622
);
642623
}

0 commit comments

Comments
 (0)