diff --git a/src/cross_toml.rs b/src/cross_toml.rs index 1c125b505..497b3ef65 100644 --- a/src/cross_toml.rs +++ b/src/cross_toml.rs @@ -9,9 +9,9 @@ use std::collections::{BTreeSet, HashMap}; #[derive(Debug, Deserialize, PartialEq, Eq, Default)] pub struct CrossEnvConfig { #[serde(default)] - volumes: Vec, + volumes: Option>, #[serde(default)] - passthrough: Vec, + passthrough: Option>, } /// Build configuration @@ -89,22 +89,22 @@ impl CrossToml { /// Returns the list of environment variables to pass through for `build`, pub fn env_passthrough_build(&self) -> &[String] { - &self.build.env.passthrough + self.build.env.passthrough.as_deref().unwrap_or(&[]) } /// Returns the list of environment variables to pass through for `target`, pub fn env_passthrough_target(&self, target: &Target) -> &[String] { - self.get_vec(target, |e| &e.passthrough) + self.get_vec(target, |e| e.passthrough.as_deref().unwrap_or(&[])) } /// Returns the list of environment variables to pass through for `build`, pub fn env_volumes_build(&self) -> &[String] { - &self.build.env.volumes + self.build.env.volumes.as_deref().unwrap_or(&[]) } /// Returns the list of environment variables to pass through for `target`, pub fn env_volumes_target(&self, target: &Target) -> &[String] { - self.get_vec(target, |e| &e.volumes) + self.get_vec(target, |e| e.volumes.as_deref().unwrap_or(&[])) } /// Returns the default target to build, @@ -169,8 +169,8 @@ mod tests { targets: HashMap::new(), build: CrossBuildConfig { env: CrossEnvConfig { - volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()], - passthrough: vec!["VAR1".to_string(), "VAR2".to_string()], + volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]), + passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]), }, xargo: Some(true), build_std: None, @@ -203,8 +203,8 @@ mod tests { }, CrossTargetConfig { env: CrossEnvConfig { - passthrough: vec!["VAR1".to_string(), "VAR2".to_string()], - volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()], + passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]), + volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]), }, xargo: Some(false), build_std: Some(true), @@ -234,4 +234,57 @@ mod tests { Ok(()) } + + #[test] + pub fn parse_mixed_toml() -> Result<()> { + let mut target_map = HashMap::new(); + target_map.insert( + Target::BuiltIn { + triple: "aarch64-unknown-linux-gnu".to_string(), + }, + CrossTargetConfig { + env: CrossEnvConfig { + passthrough: None, + volumes: Some(vec!["VOL".to_string()]), + }, + xargo: Some(false), + build_std: None, + image: None, + runner: None, + }, + ); + + let cfg = CrossToml { + targets: target_map, + build: CrossBuildConfig { + env: CrossEnvConfig { + volumes: None, + passthrough: Some(vec!["VAR".to_string()]), + }, + xargo: Some(true), + build_std: None, + default_target: None, + }, + }; + + let test_str = r#" + [build] + xargo = true + + [build.env] + passthrough = ["VAR"] + + [target.aarch64-unknown-linux-gnu] + xargo = false + + [target.aarch64-unknown-linux-gnu.env] + volumes = ["VOL"] + "#; + let (parsed_cfg, unused) = CrossToml::parse(test_str)?; + + assert_eq!(parsed_cfg, cfg); + assert!(unused.is_empty()); + + Ok(()) + } }