Skip to content

Commit 49654b3

Browse files
committed
rust: Support arrays in target JSON
Some configurations, such as enabled sanitizers, are arrays Signed-off-by: Matthew Maurer <[email protected]>
1 parent b6e2b2e commit 49654b3

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

scripts/generate_rust_target.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,40 @@ enum Value {
2020
Boolean(bool),
2121
Number(i32),
2222
String(String),
23+
Array(Vec<Value>),
2324
Object(Object),
2425
}
2526

2627
type Object = Vec<(String, Value)>;
2728

28-
/// Minimal "almost JSON" generator (e.g. no `null`s, no arrays, no escaping),
29+
fn comma_sep<T>(seq: &[T], formatter: &mut Formatter<'_>, f: impl Fn(&mut Formatter<'_>, &T) -> Result) -> Result {
30+
if let [ref rest @ .., ref last] = seq[..] {
31+
for v in rest {
32+
f(formatter, v)?;
33+
formatter.write_str(",")?;
34+
}
35+
f(formatter, last)?;
36+
}
37+
Ok(())
38+
}
39+
40+
/// Minimal "almost JSON" generator (e.g. no `null`s, no escaping),
2941
/// enough for this purpose.
3042
impl Display for Value {
3143
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
3244
match self {
3345
Value::Boolean(boolean) => write!(formatter, "{}", boolean),
3446
Value::Number(number) => write!(formatter, "{}", number),
3547
Value::String(string) => write!(formatter, "\"{}\"", string),
48+
Value::Array(values) => {
49+
formatter.write_str("[")?;
50+
comma_sep(&values[..], formatter, |formatter, v| v.fmt(formatter))?;
51+
formatter.write_str("]")
52+
}
3653
Value::Object(object) => {
3754
formatter.write_str("{")?;
38-
if let [ref rest @ .., ref last] = object[..] {
39-
for (key, value) in rest {
40-
write!(formatter, "\"{}\": {},", key, value)?;
41-
}
42-
write!(formatter, "\"{}\": {}", last.0, last.1)?;
43-
}
55+
comma_sep(&object[..], formatter, |formatter, v|
56+
write!(formatter, "\"{}\": {}", v.0, v.1))?;
4457
formatter.write_str("}")
4558
}
4659
}
@@ -77,9 +90,9 @@ impl From<Object> for Value {
7790
}
7891
}
7992

80-
impl Push<&str> for TargetSpec {
81-
fn push(&mut self, key: &str, value: &str) {
82-
self.push(key, value.to_string());
93+
impl <T: Into<Value>, const N: usize> From<[T; N]> for Value {
94+
fn from(i: [T; N]) -> Self {
95+
Self::Array(i.into_iter().map(|v| v.into()).collect())
8396
}
8497
}
8598

0 commit comments

Comments
 (0)