Skip to content

Commit fc0a731

Browse files
committed
Make config vectors optional.
Allows us to differentiate between an empty array and an array that was not provided when merging config files. #754 (comment) Required for #754.
1 parent d639e21 commit fc0a731

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

src/cross_toml.rs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::collections::{BTreeSet, HashMap};
99
#[derive(Debug, Deserialize, PartialEq, Eq, Default)]
1010
pub struct CrossEnvConfig {
1111
#[serde(default)]
12-
volumes: Vec<String>,
12+
volumes: Option<Vec<String>>,
1313
#[serde(default)]
14-
passthrough: Vec<String>,
14+
passthrough: Option<Vec<String>>,
1515
}
1616

1717
/// Build configuration
@@ -89,22 +89,22 @@ impl CrossToml {
8989

9090
/// Returns the list of environment variables to pass through for `build`,
9191
pub fn env_passthrough_build(&self) -> &[String] {
92-
&self.build.env.passthrough
92+
self.build.env.passthrough.as_deref().unwrap_or(&[])
9393
}
9494

9595
/// Returns the list of environment variables to pass through for `target`,
9696
pub fn env_passthrough_target(&self, target: &Target) -> &[String] {
97-
self.get_vec(target, |e| &e.passthrough)
97+
self.get_vec(target, |e| e.passthrough.as_deref().unwrap_or(&[]))
9898
}
9999

100100
/// Returns the list of environment variables to pass through for `build`,
101101
pub fn env_volumes_build(&self) -> &[String] {
102-
&self.build.env.volumes
102+
self.build.env.volumes.as_deref().unwrap_or(&[])
103103
}
104104

105105
/// Returns the list of environment variables to pass through for `target`,
106106
pub fn env_volumes_target(&self, target: &Target) -> &[String] {
107-
self.get_vec(target, |e| &e.volumes)
107+
self.get_vec(target, |e| e.volumes.as_deref().unwrap_or(&[]))
108108
}
109109

110110
/// Returns the default target to build,
@@ -169,8 +169,8 @@ mod tests {
169169
targets: HashMap::new(),
170170
build: CrossBuildConfig {
171171
env: CrossEnvConfig {
172-
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
173-
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
172+
volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]),
173+
passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]),
174174
},
175175
xargo: Some(true),
176176
build_std: None,
@@ -203,8 +203,8 @@ mod tests {
203203
},
204204
CrossTargetConfig {
205205
env: CrossEnvConfig {
206-
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
207-
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
206+
passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]),
207+
volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]),
208208
},
209209
xargo: Some(false),
210210
build_std: Some(true),
@@ -234,4 +234,57 @@ mod tests {
234234

235235
Ok(())
236236
}
237+
238+
#[test]
239+
pub fn parse_mixed_toml() -> Result<()> {
240+
let mut target_map = HashMap::new();
241+
target_map.insert(
242+
Target::BuiltIn {
243+
triple: "aarch64-unknown-linux-gnu".to_string(),
244+
},
245+
CrossTargetConfig {
246+
env: CrossEnvConfig {
247+
passthrough: None,
248+
volumes: Some(vec!["VOL".to_string()]),
249+
},
250+
xargo: Some(false),
251+
build_std: None,
252+
image: None,
253+
runner: None,
254+
},
255+
);
256+
257+
let cfg = CrossToml {
258+
targets: target_map,
259+
build: CrossBuildConfig {
260+
env: CrossEnvConfig {
261+
volumes: None,
262+
passthrough: Some(vec![]),
263+
},
264+
xargo: Some(true),
265+
build_std: None,
266+
default_target: None,
267+
},
268+
};
269+
270+
let test_str = r#"
271+
[build]
272+
xargo = true
273+
274+
[build.env]
275+
passthrough = []
276+
277+
[target.aarch64-unknown-linux-gnu]
278+
xargo = false
279+
280+
[target.aarch64-unknown-linux-gnu.env]
281+
volumes = ["VOL"]
282+
"#;
283+
let (parsed_cfg, unused) = CrossToml::parse(test_str)?;
284+
285+
assert_eq!(parsed_cfg, cfg);
286+
assert!(unused.is_empty());
287+
288+
Ok(())
289+
}
237290
}

0 commit comments

Comments
 (0)