Skip to content

Commit

Permalink
Make config vectors optional.
Browse files Browse the repository at this point in the history
Allows us to differentiate between an empty array and an array that was
not provided when merging config files.

cross-rs#754 (comment)

Required for cross-rs#754.
  • Loading branch information
Alexhuszagh committed Jun 15, 2022
1 parent d639e21 commit fc0a731
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::collections::{BTreeSet, HashMap};
#[derive(Debug, Deserialize, PartialEq, Eq, Default)]
pub struct CrossEnvConfig {
#[serde(default)]
volumes: Vec<String>,
volumes: Option<Vec<String>>,
#[serde(default)]
passthrough: Vec<String>,
passthrough: Option<Vec<String>>,
}

/// Build configuration
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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![]),
},
xargo: Some(true),
build_std: None,
default_target: None,
},
};

let test_str = r#"
[build]
xargo = true
[build.env]
passthrough = []
[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(())
}
}

0 comments on commit fc0a731

Please sign in to comment.