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.

#754 (comment)

Required for #754.
  • Loading branch information
Alexhuszagh committed Jun 15, 2022
1 parent d639e21 commit 462e40a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ impl Config {
&self,
target: &Target,
env: impl Fn(&Environment, &Target) -> (Option<Vec<String>>, Option<Vec<String>>),
config_build: impl for<'a> Fn(&'a CrossToml) -> &'a [String],
config_target: impl for<'a> Fn(&'a CrossToml, &Target) -> &'a [String],
config_build: impl for<'a> Fn(&'a CrossToml) -> Option<&'a [String]>,
config_target: impl for<'a> Fn(&'a CrossToml, &Target) -> Option<&'a [String]>,
) -> Result<Vec<String>> {
let (env_build, env_target) = env(&self.env, target);

Expand Down Expand Up @@ -241,12 +241,12 @@ impl Config {
fn sum_of_env_toml_values<'a>(
&'a self,
env_values: Option<Vec<String>>,
toml_getter: impl FnOnce(&'a CrossToml) -> &'a [String],
toml_getter: impl FnOnce(&'a CrossToml) -> Option<&'a [String]>,
) -> Result<Vec<String>> {
let mut collect = vec![];
if let Some(mut vars) = env_values {
collect.append(&mut vars);
} else if let Some(toml_values) = self.toml.as_ref().map(toml_getter) {
} else if let Some(Some(toml_values)) = self.toml.as_ref().map(toml_getter) {
collect.extend(toml_values.iter().cloned());
}

Expand Down
91 changes: 73 additions & 18 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use std::collections::{BTreeSet, HashMap};
/// Environment configuration
#[derive(Debug, Deserialize, PartialEq, Eq, Default)]
pub struct CrossEnvConfig {
#[serde(default)]
volumes: Vec<String>,
#[serde(default)]
passthrough: Vec<String>,
volumes: Option<Vec<String>>,
passthrough: Option<Vec<String>>,
}

/// Build configuration
Expand Down Expand Up @@ -88,23 +86,23 @@ impl CrossToml {
}

/// Returns the list of environment variables to pass through for `build`,
pub fn env_passthrough_build(&self) -> &[String] {
&self.build.env.passthrough
pub fn env_passthrough_build(&self) -> Option<&[String]> {
self.build.env.passthrough.as_deref()
}

/// 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)
pub fn env_passthrough_target(&self, target: &Target) -> Option<&[String]> {
self.get_vec(target, |e| e.passthrough.as_deref())
}

/// Returns the list of environment variables to pass through for `build`,
pub fn env_volumes_build(&self) -> &[String] {
&self.build.env.volumes
pub fn env_volumes_build(&self) -> Option<&[String]> {
self.build.env.volumes.as_deref()
}

/// 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)
pub fn env_volumes_target(&self, target: &Target) -> Option<&[String]> {
self.get_vec(target, |e| e.volumes.as_deref())
}

/// Returns the default target to build,
Expand Down Expand Up @@ -140,8 +138,12 @@ impl CrossToml {
(build, target)
}

fn get_vec(&self, target: &Target, get: impl Fn(&CrossEnvConfig) -> &[String]) -> &[String] {
self.get_target(target).map_or(&[], |t| get(&t.env))
fn get_vec(
&self,
target: &Target,
get: impl Fn(&CrossEnvConfig) -> Option<&[String]>,
) -> Option<&[String]> {
self.get_target(target).and_then(|t| get(&t.env))
}
}

Expand Down Expand Up @@ -169,8 +171,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 +205,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 +236,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 462e40a

Please sign in to comment.