ANDROID: rust: Support arrays in target JSON

Some configurations, such as enabled sanitizers, are arrays

This is Android specific because Rust KCFI is incomplete, and we are
using the binder driver as a staging ground to fix it. This patch is
intended to go upstream once the compiler behaves properly.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
Bug: 316623888
Change-Id: I4cf494f36a07a685f6856aef4a361772baedde2e
This commit is contained in:
Matthew Maurer 2023-09-14 23:38:39 +00:00 committed by Treehugger Robot
parent 4085063afb
commit 2b93c38ece

View File

@ -20,12 +20,24 @@ enum Value {
Boolean(bool),
Number(i32),
String(String),
Array(Vec<Value>),
Object(Object),
}
type Object = Vec<(String, Value)>;
/// Minimal "almost JSON" generator (e.g. no `null`s, no arrays, no escaping),
fn comma_sep<T>(seq: &[T], formatter: &mut Formatter<'_>, f: impl Fn(&mut Formatter<'_>, &T) -> Result) -> Result {
if let [ref rest @ .., ref last] = seq[..] {
for v in rest {
f(formatter, v)?;
formatter.write_str(",")?;
}
f(formatter, last)?;
}
Ok(())
}
/// Minimal "almost JSON" generator (e.g. no `null`s, no escaping),
/// enough for this purpose.
impl Display for Value {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
@ -33,14 +45,15 @@ impl Display for Value {
Value::Boolean(boolean) => write!(formatter, "{}", boolean),
Value::Number(number) => write!(formatter, "{}", number),
Value::String(string) => write!(formatter, "\"{}\"", string),
Value::Array(values) => {
formatter.write_str("[")?;
comma_sep(&values[..], formatter, |formatter, v| v.fmt(formatter))?;
formatter.write_str("]")
}
Value::Object(object) => {
formatter.write_str("{")?;
if let [ref rest @ .., ref last] = object[..] {
for (key, value) in rest {
write!(formatter, "\"{}\": {},", key, value)?;
}
write!(formatter, "\"{}\": {}", last.0, last.1)?;
}
comma_sep(&object[..], formatter, |formatter, v|
write!(formatter, "\"{}\": {}", v.0, v.1))?;
formatter.write_str("}")
}
}
@ -77,9 +90,9 @@ impl Push<String> for TargetSpec {
}
}
impl Push<&str> for TargetSpec {
fn push(&mut self, key: &str, value: &str) {
self.push(key, value.to_string());
impl <T: Into<Value>, const N: usize> From<[T; N]> for Value {
fn from(i: [T; N]) -> Self {
Self::Array(i.into_iter().map(|v| v.into()).collect())
}
}