Skip to content

Commit 35de136

Browse files
author
Paolo Tranquilli
committed
Rust: accept cargo_extra_env entries without =
1 parent 52ef385 commit 35de136

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

rust/extractor/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct Config {
5050
pub cargo_target: Option<String>,
5151
pub cargo_features: Vec<String>,
5252
pub cargo_cfg_overrides: Vec<String>,
53-
pub cargo_extra_env: FxHashMap<String, String>,
53+
pub cargo_extra_env: FxHashMap<String, Option<String>>,
5454
pub cargo_extra_args: Vec<String>,
5555
pub cargo_all_targets: bool,
5656
pub logging_flamegraph: Option<PathBuf>,

rust/extractor/src/config/deserialize.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::Deserializer;
2-
use serde::de::{Error, Unexpected, Visitor};
2+
use serde::de::Visitor;
33
use std::collections::HashMap;
44
use std::fmt::Formatter;
55
use std::hash::BuildHasher;
@@ -36,23 +36,24 @@ impl<'de, T: From<String>> Visitor<'de> for VectorVisitor<T> {
3636
}
3737

3838
impl<'de, S: BuildHasher + Default> Visitor<'de> for MapVisitor<S> {
39-
type Value = HashMap<String, String, S>;
39+
type Value = HashMap<String, Option<String>, S>;
4040

4141
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
4242
formatter.write_str(
43-
"either a sequence, or a comma or newline separated string of key=value entries",
43+
"either a sequence, or a comma or newline separated string of key[=value] entries",
4444
)
4545
}
4646

4747
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
48-
value
48+
Ok(value
4949
.split(['\n', ','])
5050
.map(|s| {
51-
s.split_once('=')
52-
.ok_or_else(|| E::custom(format!("key=value expected, found {s}")))
53-
.map(|(key, value)| (key.to_owned(), value.to_owned()))
51+
match s.split_once('=') {
52+
Some((key, value)) => (key.to_owned(), Some(value.to_owned())),
53+
None => (s.to_owned(), None),
54+
}
5455
})
55-
.collect()
56+
.collect())
5657
}
5758

5859
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
@@ -61,10 +62,14 @@ impl<'de, S: BuildHasher + Default> Visitor<'de> for MapVisitor<S> {
6162
{
6263
let mut ret = HashMap::with_hasher(Default::default());
6364
while let Some(el) = seq.next_element::<String>()? {
64-
let (key, value) = el
65-
.split_once('=')
66-
.ok_or_else(|| A::Error::invalid_value(Unexpected::Str(&el), &self))?;
67-
ret.insert(key.to_owned(), value.to_owned());
65+
match el.split_once('=') {
66+
None => {
67+
ret.insert(el.to_owned(), None);
68+
}
69+
Some((key, value)) => {
70+
ret.insert(key.to_owned(), Some(value.to_owned()));
71+
}
72+
}
6873
}
6974
Ok(ret)
7075
}
@@ -83,7 +88,7 @@ pub(crate) fn deserialize_newline_or_comma_separated_vec<
8388
deserializer.deserialize_seq(VectorVisitor(PhantomData))
8489
}
8590

86-
/// deserialize into a map of `String`s to `String`s either of:
91+
/// deserialize into a map of `String`s to `Option<String>`s either of:
8792
/// * a sequence of elements serializable into `String`s, or
8893
/// * a single element serializable into `String`, then split on `,` and `\n`
8994
pub(crate) fn deserialize_newline_or_comma_separated_map<
@@ -92,6 +97,6 @@ pub(crate) fn deserialize_newline_or_comma_separated_map<
9297
S: BuildHasher + Default,
9398
>(
9499
deserializer: D,
95-
) -> Result<HashMap<String, String, S>, D::Error> {
100+
) -> Result<HashMap<String, Option<String>, S>, D::Error> {
96101
deserializer.deserialize_map(MapVisitor(PhantomData))
97102
}

0 commit comments

Comments
 (0)