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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{Params, Type};
use id_arena::{Arena, Id};
use indexmap::IndexMap;
use semver::Version;
use serde::ser::{SerializeMap, SerializeSeq, Serializer};
use serde::{de::Error, Deserialize, Serialize};

pub fn serialize_none<S>(serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_none()
}

pub fn serialize_arena<T, S>(arena: &Arena<T>, serializer: S) -> Result<S::Ok, S::Error>
where
    T: Serialize,
    S: Serializer,
{
    let mut seq = serializer.serialize_seq(Some(arena.len()))?;
    for (_, item) in arena.iter() {
        seq.serialize_element(&item)?;
    }
    seq.end()
}

pub fn serialize_id<T, S>(id: &Id<T>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_u64(id.index() as u64)
}

pub fn serialize_optional_id<T, S>(id: &Option<Id<T>>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match id {
        Some(id) => serialize_id(&id, serializer),
        None => serializer.serialize_none(),
    }
}

pub fn serialize_id_map<K, T, S>(map: &IndexMap<K, Id<T>>, serializer: S) -> Result<S::Ok, S::Error>
where
    K: Serialize,
    S: Serializer,
{
    let mut s = serializer.serialize_map(Some(map.len()))?;
    for (key, id) in map.iter() {
        s.serialize_entry(key, &(id.index() as u64))?;
    }
    s.end()
}

impl Serialize for Type {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Type::Bool => serializer.serialize_str("bool"),
            Type::U8 => serializer.serialize_str("u8"),
            Type::U16 => serializer.serialize_str("u16"),
            Type::U32 => serializer.serialize_str("u32"),
            Type::U64 => serializer.serialize_str("u64"),
            Type::S8 => serializer.serialize_str("s8"),
            Type::S16 => serializer.serialize_str("s16"),
            Type::S32 => serializer.serialize_str("s32"),
            Type::S64 => serializer.serialize_str("s64"),
            Type::F32 => serializer.serialize_str("f32"),
            Type::F64 => serializer.serialize_str("f64"),
            Type::Char => serializer.serialize_str("char"),
            Type::String => serializer.serialize_str("string"),
            Type::Id(type_id) => serializer.serialize_u64(type_id.index() as u64),
        }
    }
}

pub fn serialize_anon_result<S>(typ: &Type, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let params: Params = vec![(String::default(), *typ)];
    serialize_params(&params, serializer)
}

pub fn serialize_params<S>(params: &Params, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let mut seq = serializer.serialize_seq(Some(params.len()))?;
    for (name, typ) in params.iter() {
        let param = Param {
            name: name.to_string(),
            typ: *typ,
        };
        seq.serialize_element(&param)?;
    }
    seq.end()
}

#[derive(Debug, Clone, PartialEq, serde_derive::Serialize)]
struct Param {
    #[serde(skip_serializing_if = "String::is_empty")]
    pub name: String,
    #[serde(rename = "type")]
    pub typ: Type,
}

pub fn serialize_version<S>(version: &Version, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    version.to_string().serialize(serializer)
}

pub fn deserialize_version<'de, D>(deserializer: D) -> Result<Version, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    let version: String = String::deserialize(deserializer)?;
    version.parse().map_err(|e| D::Error::custom(e))
}

pub fn serialize_optional_version<S>(
    version: &Option<Version>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    version
        .as_ref()
        .map(|s| s.to_string())
        .serialize(serializer)
}

pub fn deserialize_optional_version<'de, D>(deserializer: D) -> Result<Option<Version>, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    match <Option<String>>::deserialize(deserializer)? {
        Some(version) => Ok(Some(version.parse().map_err(|e| D::Error::custom(e))?)),
        None => Ok(None),
    }
}