1use std::borrow::Cow;
2use std::collections::BTreeMap;
3
4pub use serde_json::Value as Json;
5use serde_json::{Map, Number, json};
6
7use crate::spec::TargetMetadata;
8
9pub trait ToJson {
10 fn to_json(&self) -> Json;
11}
12
13impl ToJson for Json {
14 fn to_json(&self) -> Json {
15 self.clone()
16 }
17}
18
19macro_rules! to_json_impl_num {
20 ($($t:ty), +) => (
21 $(impl ToJson for $t {
22 fn to_json(&self) -> Json {
23 Json::Number(Number::from(*self))
24 }
25 })+
26 )
27}
28
29to_json_impl_num! { isize, i8, i16, i32, i64, usize, u8, u16, u32, u64 }
30
31impl ToJson for bool {
32 fn to_json(&self) -> Json {
33 Json::Bool(*self)
34 }
35}
36
37impl ToJson for str {
38 fn to_json(&self) -> Json {
39 Json::String(self.to_owned())
40 }
41}
42
43impl ToJson for String {
44 fn to_json(&self) -> Json {
45 Json::String(self.to_owned())
46 }
47}
48
49impl<'a> ToJson for Cow<'a, str> {
50 fn to_json(&self) -> Json {
51 Json::String(self.to_string())
52 }
53}
54
55impl<A: ToJson> ToJson for [A] {
56 fn to_json(&self) -> Json {
57 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
58 }
59}
60
61impl<A: ToJson> ToJson for Vec<A> {
62 fn to_json(&self) -> Json {
63 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
64 }
65}
66
67impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
68where
69 [A]: ToOwned,
70{
71 fn to_json(&self) -> Json {
72 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
73 }
74}
75
76impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
77 fn to_json(&self) -> Json {
78 let mut d = Map::new();
79 for (key, value) in self {
80 d.insert(key.to_string(), value.to_json());
81 }
82 Json::Object(d)
83 }
84}
85
86impl<A: ToJson> ToJson for Option<A> {
87 fn to_json(&self) -> Json {
88 match *self {
89 None => Json::Null,
90 Some(ref value) => value.to_json(),
91 }
92 }
93}
94
95impl ToJson for TargetMetadata {
96 fn to_json(&self) -> Json {
97 json!({
98 "description": self.description,
99 "tier": self.tier,
100 "host_tools": self.host_tools,
101 "std": self.std,
102 })
103 }
104}
105
106impl ToJson for rustc_abi::Endian {
107 fn to_json(&self) -> Json {
108 self.as_str().to_json()
109 }
110}
111
112impl ToJson for rustc_abi::CanonAbi {
113 fn to_json(&self) -> Json {
114 self.to_string().to_json()
115 }
116}