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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::ops::Deref;

use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(feature = "parse")] {
        #[macro_use]
        extern crate log;

        mod parse;

        pub use parse::parse;
    }
}

#[cfg(feature = "display")]
mod display;

cfg_if! {
    if #[cfg(feature = "to_json")] {
        use serde::Serialize;

        mod ser;
    }
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Description<'a>(Vec<&'a str>);

impl<'a> Deref for Description<'a> {
    type Target = [&'a str];

    fn deref(&self) -> &Self::Target {
        self.0.as_slice()
    }
}

impl<'a> From<Vec<&'a str>> for Description<'a> {
    fn from(comments: Vec<&'a str>) -> Description<'a> {
        Description(comments)
    }
}

impl<'a> From<&'a str> for Description<'a> {
    fn from(comment: &'a str) -> Description<'a> {
        Description(vec![comment])
    }
}

impl Description<'_> {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Protocol<'a> {
    #[cfg_attr(feature = "to_json", serde(skip_serializing))]
    pub description: Description<'a>,
    pub version: Version,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub domains: Vec<Domain<'a>>,
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Version {
    #[cfg_attr(feature = "to_json", serde(serialize_with = "ser::serialize_usize"))]
    pub major: usize,
    #[cfg_attr(feature = "to_json", serde(serialize_with = "ser::serialize_usize"))]
    pub minor: usize,
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Domain<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub experimental: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub deprecated: bool,
    #[cfg_attr(feature = "to_json", serde(rename = "domain"))]
    pub name: &'a str,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub dependencies: Vec<&'a str>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub types: Vec<TypeDef<'a>>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub commands: Vec<Command<'a>>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub events: Vec<Event<'a>>,
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeDef<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub experimental: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub deprecated: bool,
    pub id: &'a str,
    #[cfg_attr(feature = "to_json", serde(flatten))]
    pub extends: Type<'a>,
    #[cfg_attr(feature = "to_json", serde(flatten))]
    pub item: Option<Item<'a>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Type<'a> {
    Integer,
    Number,
    Boolean,
    String,
    Object,
    Any,
    Binary,
    Enum(Vec<Variant<'a>>),
    ArrayOf(Box<Type<'a>>),
    Ref(&'a str),
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[cfg_attr(feature = "to_json", serde(rename_all = "lowercase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Item<'a> {
    #[cfg_attr(feature = "to_json", serde(serialize_with = "ser::serialize_enum"))]
    Enum(Vec<Variant<'a>>),
    Properties(Vec<Param<'a>>),
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Variant<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    pub name: &'a str,
}

impl<'a> Variant<'a> {
    pub fn new(name: &str) -> Variant {
        Variant {
            description: Default::default(),
            name,
        }
    }
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Param<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub experimental: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub deprecated: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub optional: bool,
    #[cfg_attr(feature = "to_json", serde(flatten))]
    pub ty: Type<'a>,
    pub name: &'a str,
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Command<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub experimental: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub deprecated: bool,
    pub name: &'a str,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Option::is_none"))]
    #[cfg_attr(feature = "to_json", serde(serialize_with = "ser::serialize_redirect"))]
    pub redirect: Option<Redirect<'a>>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub parameters: Vec<Param<'a>>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub returns: Vec<Param<'a>>,
}

#[cfg_attr(feature = "to_json", derive(Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Event<'a> {
    #[cfg_attr(
        feature = "to_json",
        serde(skip_serializing_if = "Description::is_empty")
    )]
    #[cfg_attr(
        feature = "to_json",
        serde(serialize_with = "ser::serialize_description")
    )]
    pub description: Description<'a>,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub experimental: bool,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "ser::is_false"))]
    pub deprecated: bool,
    pub name: &'a str,
    #[cfg_attr(feature = "to_json", serde(skip_serializing_if = "Vec::is_empty"))]
    pub parameters: Vec<Param<'a>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Redirect<'a> {
    pub description: Description<'a>,
    pub to: &'a str,
}