diff options
| author | bjorn3 <bjorn3@users.noreply.github.com> | 2021-06-03 17:45:09 +0200 | 
|---|---|---|
| committer | bjorn3 <bjorn3@users.noreply.github.com> | 2022-06-03 16:46:19 +0000 | 
| commit | fc1df4ff170ec137ecab7d7aa7f4dc894bb17449 (patch) | |
| tree | 4069f5bb6a9d28342893fe91c47f49d0833bf4ab /compiler/rustc_target/src/json.rs | |
| parent | fc2abe6952a00c21a2bb77ad8bd86dcb6e6abea2 (diff) | |
| download | rust-fc1df4ff170ec137ecab7d7aa7f4dc894bb17449.tar.gz rust-fc1df4ff170ec137ecab7d7aa7f4dc894bb17449.zip | |
Use serde_json for target spec json
Diffstat (limited to 'compiler/rustc_target/src/json.rs')
| -rw-r--r-- | compiler/rustc_target/src/json.rs | 91 | 
1 files changed, 91 insertions, 0 deletions
| diff --git a/compiler/rustc_target/src/json.rs b/compiler/rustc_target/src/json.rs new file mode 100644 index 00000000000..b5d92635212 --- /dev/null +++ b/compiler/rustc_target/src/json.rs @@ -0,0 +1,91 @@ +use std::borrow::Cow; +use std::collections::BTreeMap; + +pub use serde_json::Value as Json; +use serde_json::{Map, Number}; + +pub trait ToJson { + fn to_json(&self) -> Json; +} + +impl ToJson for Json { + fn to_json(&self) -> Json { + self.clone() + } +} + +macro_rules! to_json_impl_num { + ($($t:ty), +) => ( + $(impl ToJson for $t { + fn to_json(&self) -> Json { + Json::Number(Number::from(*self)) + } + })+ + ) +} + +to_json_impl_num! { isize, i8, i16, i32, i64, usize, u8, u16, u32, u64 } + +impl ToJson for bool { + fn to_json(&self) -> Json { + Json::Bool(*self) + } +} + +impl ToJson for str { + fn to_json(&self) -> Json { + Json::String(self.to_owned()) + } +} + +impl ToJson for String { + fn to_json(&self) -> Json { + Json::String(self.to_owned()) + } +} + +impl<'a> ToJson for Cow<'a, str> { + fn to_json(&self) -> Json { + Json::String(self.to_string()) + } +} + +impl<A: ToJson> ToJson for [A] { + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<A: ToJson> ToJson for Vec<A> { + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<'a, A: ToJson> ToJson for Cow<'a, [A]> +where + [A]: ToOwned, +{ + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> { + fn to_json(&self) -> Json { + let mut d = Map::new(); + for (key, value) in self { + d.insert(key.to_string(), value.to_json()); + } + Json::Object(d) + } +} + +impl<A: ToJson> ToJson for Option<A> { + fn to_json(&self) -> Json { + match *self { + None => Json::Null, + Some(ref value) => value.to_json(), + } + } +} | 
