Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config vectors optional. #801

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(toml_values) = self.toml.as_ref().and_then(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(())
}
}