From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_target/src/json.rs | 91 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 compiler/rustc_target/src/json.rs (limited to 'compiler/rustc_target/src/json.rs') diff --git a/compiler/rustc_target/src/json.rs b/compiler/rustc_target/src/json.rs new file mode 100644 index 000000000..b5d926352 --- /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 ToJson for [A] { + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl ToJson for Vec { + 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 ToJson for BTreeMap { + 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 ToJson for Option { + fn to_json(&self) -> Json { + match *self { + None => Json::Null, + Some(ref value) => value.to_json(), + } + } +} -- cgit v1.2.3