rustc_target/
json.rs

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 crate::callconv::Conv {
96    fn to_json(&self) -> Json {
97        let buf: String;
98        let s = match self {
99            Self::C => "C",
100            Self::Rust => "Rust",
101            Self::Cold => "Cold",
102            Self::PreserveMost => "PreserveMost",
103            Self::PreserveAll => "PreserveAll",
104            Self::ArmAapcs => "ArmAapcs",
105            Self::CCmseNonSecureCall => "CCmseNonSecureCall",
106            Self::CCmseNonSecureEntry => "CCmseNonSecureEntry",
107            Self::Msp430Intr => "Msp430Intr",
108            Self::X86Fastcall => "X86Fastcall",
109            Self::X86Intr => "X86Intr",
110            Self::X86Stdcall => "X86Stdcall",
111            Self::X86ThisCall => "X86ThisCall",
112            Self::X86VectorCall => "X86VectorCall",
113            Self::X86_64SysV => "X86_64SysV",
114            Self::X86_64Win64 => "X86_64Win64",
115            Self::GpuKernel => "GpuKernel",
116            Self::AvrInterrupt => "AvrInterrupt",
117            Self::AvrNonBlockingInterrupt => "AvrNonBlockingInterrupt",
118            Self::RiscvInterrupt { kind } => {
119                buf = format!("RiscvInterrupt({})", kind.as_str());
120                &buf
121            }
122        };
123        Json::String(s.to_owned())
124    }
125}
126
127impl ToJson for TargetMetadata {
128    fn to_json(&self) -> Json {
129        json!({
130            "description": self.description,
131            "tier": self.tier,
132            "host_tools": self.host_tools,
133            "std": self.std,
134        })
135    }
136}
137
138impl ToJson for rustc_abi::Endian {
139    fn to_json(&self) -> Json {
140        self.as_str().to_json()
141    }
142}