summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_target/src/json.rs
blob: 75bb76a9de0878afe6f0faa1e65f7adf5f73a580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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(),
        }
    }
}

impl ToJson for crate::abi::call::Conv {
    fn to_json(&self) -> Json {
        let s = match self {
            Self::C => "C",
            Self::Rust => "Rust",
            Self::RustCold => "RustCold",
            Self::ArmAapcs => "ArmAapcs",
            Self::CCmseNonSecureCall => "CCmseNonSecureCall",
            Self::Msp430Intr => "Msp430Intr",
            Self::PtxKernel => "PtxKernel",
            Self::X86Fastcall => "X86Fastcall",
            Self::X86Intr => "X86Intr",
            Self::X86Stdcall => "X86Stdcall",
            Self::X86ThisCall => "X86ThisCall",
            Self::X86VectorCall => "X86VectorCall",
            Self::X86_64SysV => "X86_64SysV",
            Self::X86_64Win64 => "X86_64Win64",
            Self::AmdGpuKernel => "AmdGpuKernel",
            Self::AvrInterrupt => "AvrInterrupt",
            Self::AvrNonBlockingInterrupt => "AvrNonBlockingInterrupt",
        };
        Json::String(s.to_owned())
    }
}