Skip to content

Commit d6d9e66

Browse files
Merge #801
801: Make config vectors optional. r=Emilgardis a=Alexhuszagh Allows us to differentiate between an empty array and an array that was not provided when merging config files. #754 (comment) Required for #754. Co-authored-by: Alex Huszagh <[email protected]>
2 parents d639e21 + 830f836 commit d6d9e66

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ impl Config {
179179
&self,
180180
target: &Target,
181181
env: impl Fn(&Environment, &Target) -> (Option<Vec<String>>, Option<Vec<String>>),
182-
config_build: impl for<'a> Fn(&'a CrossToml) -> &'a [String],
183-
config_target: impl for<'a> Fn(&'a CrossToml, &Target) -> &'a [String],
182+
config_build: impl for<'a> Fn(&'a CrossToml) -> Option<&'a [String]>,
183+
config_target: impl for<'a> Fn(&'a CrossToml, &Target) -> Option<&'a [String]>,
184184
) -> Result<Vec<String>> {
185185
let (env_build, env_target) = env(&self.env, target);
186186

@@ -241,12 +241,12 @@ impl Config {
241241
fn sum_of_env_toml_values<'a>(
242242
&'a self,
243243
env_values: Option<Vec<String>>,
244-
toml_getter: impl FnOnce(&'a CrossToml) -> &'a [String],
244+
toml_getter: impl FnOnce(&'a CrossToml) -> Option<&'a [String]>,
245245
) -> Result<Vec<String>> {
246246
let mut collect = vec![];
247247
if let Some(mut vars) = env_values {
248248
collect.append(&mut vars);
249-
} else if let Some(toml_values) = self.toml.as_ref().map(toml_getter) {
249+
} else if let Some(toml_values) = self.toml.as_ref().and_then(toml_getter) {
250250
collect.extend(toml_values.iter().cloned());
251251
}
252252

src/cross_toml.rs

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ use std::collections::{BTreeSet, HashMap};
88
/// Environment configuration
99
#[derive(Debug, Deserialize, PartialEq, Eq, Default)]
1010
pub struct CrossEnvConfig {
11-
#[serde(default)]
12-
volumes: Vec<String>,
13-
#[serde(default)]
14-
passthrough: Vec<String>,
11+
volumes: Option<Vec<String>>,
12+
passthrough: Option<Vec<String>>,
1513
}
1614

1715
/// Build configuration
@@ -88,23 +86,23 @@ impl CrossToml {
8886
}
8987

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

9593
/// Returns the list of environment variables to pass through for `target`,
96-
pub fn env_passthrough_target(&self, target: &Target) -> &[String] {
97-
self.get_vec(target, |e| &e.passthrough)
94+
pub fn env_passthrough_target(&self, target: &Target) -> Option<&[String]> {
95+
self.get_vec(target, |e| e.passthrough.as_deref())
9896
}
9997

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

105103
/// Returns the list of environment variables to pass through for `target`,
106-
pub fn env_volumes_target(&self, target: &Target) -> &[String] {
107-
self.get_vec(target, |e| &e.volumes)
104+
pub fn env_volumes_target(&self, target: &Target) -> Option<&[String]> {
105+
self.get_vec(target, |e| e.volumes.as_deref())
108106
}
109107

110108
/// Returns the default target to build,
@@ -140,8 +138,12 @@ impl CrossToml {
140138
(build, target)
141139
}
142140

143-
fn get_vec(&self, target: &Target, get: impl Fn(&CrossEnvConfig) -> &[String]) -> &[String] {
144-
self.get_target(target).map_or(&[], |t| get(&t.env))
141+
fn get_vec(
142+
&self,
143+
target: &Target,
144+
get: impl Fn(&CrossEnvConfig) -> Option<&[String]>,
145+
) -> Option<&[String]> {
146+
self.get_target(target).and_then(|t| get(&t.env))
145147
}
146148
}
147149

@@ -169,8 +171,8 @@ mod tests {
169171
targets: HashMap::new(),
170172
build: CrossBuildConfig {
171173
env: CrossEnvConfig {
172-
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
173-
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
174+
volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]),
175+
passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]),
174176
},
175177
xargo: Some(true),
176178
build_std: None,
@@ -203,8 +205,8 @@ mod tests {
203205
},
204206
CrossTargetConfig {
205207
env: CrossEnvConfig {
206-
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
207-
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
208+
passthrough: Some(vec!["VAR1".to_string(), "VAR2".to_string()]),
209+
volumes: Some(vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()]),
208210
},
209211
xargo: Some(false),
210212
build_std: Some(true),
@@ -234,4 +236,57 @@ mod tests {
234236

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

0 commit comments

Comments
 (0)