Skip to main content

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
29impl ToJson for isize {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for i8 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for i16 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for i32 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for i64 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for usize {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for u8 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for u16 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for u32 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}
impl ToJson for u64 {
    fn to_json(&self) -> Json { Json::Number(Number::from(*self)) }
}to_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<T: ToJson, U: ToJson> ToJson for (T, U) {
62    fn to_json(&self) -> Json {
63        Json::Array(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.0.to_json(), self.1.to_json()]))vec![self.0.to_json(), self.1.to_json()])
64    }
65}
66
67impl<A: ToJson> ToJson for Vec<A> {
68    fn to_json(&self) -> Json {
69        Json::Array(self.iter().map(|elt| elt.to_json()).collect())
70    }
71}
72
73impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
74where
75    [A]: ToOwned,
76{
77    fn to_json(&self) -> Json {
78        Json::Array(self.iter().map(|elt| elt.to_json()).collect())
79    }
80}
81
82impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
83    fn to_json(&self) -> Json {
84        let mut d = Map::new();
85        for (key, value) in self {
86            d.insert(key.to_string(), value.to_json());
87        }
88        Json::Object(d)
89    }
90}
91
92impl<A: ToJson> ToJson for Option<A> {
93    fn to_json(&self) -> Json {
94        match *self {
95            None => Json::Null,
96            Some(ref value) => value.to_json(),
97        }
98    }
99}
100
101impl ToJson for TargetMetadata {
102    fn to_json(&self) -> Json {
103        ::serde_json::Value::Object({
        let mut object = ::serde_json::Map::new();
        let _ =
            object.insert(("description").into(),
                ::serde_json::to_value(&self.description).unwrap());
        let _ =
            object.insert(("tier").into(),
                ::serde_json::to_value(&self.tier).unwrap());
        let _ =
            object.insert(("host_tools").into(),
                ::serde_json::to_value(&self.host_tools).unwrap());
        let _ =
            object.insert(("std").into(),
                ::serde_json::to_value(&self.std).unwrap());
        ;
        object
    })json!({
104            "description": self.description,
105            "tier": self.tier,
106            "host_tools": self.host_tools,
107            "std": self.std,
108        })
109    }
110}
111
112impl ToJson for rustc_abi::Endian {
113    fn to_json(&self) -> Json {
114        self.as_str().to_json()
115    }
116}
117
118impl ToJson for rustc_abi::CanonAbi {
119    fn to_json(&self) -> Json {
120        self.to_string().to_json()
121    }
122}
123
124macro_rules! serde_deserialize_from_str {
125    ($ty:ty) => {
126        impl<'de> serde::Deserialize<'de> for $ty {
127            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
128            where
129                D: serde::Deserializer<'de>,
130            {
131                let s = String::deserialize(deserializer)?;
132                FromStr::from_str(&s).map_err(serde::de::Error::custom)
133            }
134        }
135    };
136}
137pub(crate) use serde_deserialize_from_str;