-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextras_generator.rs
More file actions
246 lines (223 loc) · 7.15 KB
/
extras_generator.rs
File metadata and controls
246 lines (223 loc) · 7.15 KB
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
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
use std::{
fs::File,
path::{Path, PathBuf},
};
use convert_case::{Case, Casing};
use serde_json::Value;
pub fn generate(path: &Path) -> String {
let message_schemas = read_message_schemas(path);
generate_extras(&message_schemas)
}
struct MessageSchema {
path: PathBuf,
schema: Value,
}
impl MessageSchema {
pub fn new(path: PathBuf) -> Self {
let schema = Self::schema(&path);
Self { path, schema }
}
fn schema(path: &Path) -> Value {
serde_json::from_reader(
File::open(path).expect(&format!("Open schema file {}", path.display())),
)
.expect(&format!("Parse JSON schema {}", path.display()))
}
pub fn name(&self) -> String {
self.path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_case(Case::Pascal)
}
pub fn id(&self) -> i64 {
self.schema
.as_object()
.expect("Schema root is object")
.get("properties")
.expect("Schema has properties")
.as_object()
.expect("Schema properties is object")
.get("messageType")
.expect("Schema has messageType")
.as_object()
.expect("Schema messageType is object")
.get("const")
.expect("Schema messageType is const")
.as_i64()
.expect("Schema messageType is integer")
}
}
fn read_message_schemas(path: &Path) -> Vec<MessageSchema> {
let schema: Value = serde_json::from_reader(
File::open(path).expect(&format!("Open schema file {}", path.display())),
)
.expect(&format!("Parse JSON schema {}", path.display()));
schema
.as_object()
.expect("Schema root is object")
.get("properties")
.expect("Schema has properties")
.as_object()
.expect("Schema properties is object")
.get("message")
.expect("Schema has message")
.as_object()
.expect("Schema message is object")
.get("oneOf")
.expect("Schema has oneOf")
.as_array()
.expect("Schema oneOf is array")
.into_iter()
.map(|obj| {
let file = obj
.as_object()
.expect("Schema oneOf entry is object")
.get("$ref")
.expect("Schema has $ref")
.as_str()
.expect("Schema $ref is string");
MessageSchema::new(path.parent().expect("Path parent").join(file))
})
.collect()
}
macro_rules! iterate {
($fmt:expr, $files:expr) => {{
let mut code = "".to_owned();
for file in $files {
code += &format!(
concat!($fmt, "{name:.0}{id:.0}"),
name = file.name(),
id = file.id().to_string(),
);
}
code
}};
}
fn generate_extras(schemas: &Vec<MessageSchema>) -> String {
format!(
"
use std::{{fmt::{{self, Display, Formatter}}, str::FromStr}};
use serde::{{ser::{{self, Serializer}}, Serialize, Deserialize}};
use crate::types::*;
pub trait MessageType {{
const MESSAGE_ID: i64;
}}
pub trait MessageBase {{
fn plugin_id(&self) -> &str;
fn message_id(&self) -> i64;
}}
#[derive(Serialize, Deserialize, Debug)]
pub struct GenericMessage {{
#[serde(rename = \"messageType\")]
message_type: i64
}}
#[derive(Debug)]
pub struct Error {{
message: String,
}}
impl Display for Error {{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {{
write!(f, \"Cannot parse Message: {{}}\", &self.message)
}}
}}
{schemafy_impl}
#[derive(Debug)]
pub enum Message {{
{message_enum}
}}
impl MessageBase for Message {{
fn message_id(&self) -> i64 {{
match self {{
{message_message_id}
}}
}}
fn plugin_id(&self) -> &str {{
match self {{
{message_plugin_id}
}}
}}
}}
impl FromStr for Message {{
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {{
let msg: GenericMessage = serde_json::from_str(s)
.map_err(|e|
Error {{ message: format!(\"Invalid message: {{}}\", e.to_string()).to_owned() }}
)?;
let code = msg.message_type;
match code {{
{message_from_str}
_ => Err(Error {{ message: \"Unknown message type\".to_owned() }}),
}}
}}
}}
impl ser::Serialize for Message {{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{{
match self {{
{message_serialize}
}}
}}
}}
",
message_enum = iterate!("{name}({name}),", schemas),
message_plugin_id = iterate!("Message::{name}(msg) => msg.plugin_id(),", schemas),
message_message_id = iterate!("Message::{name}(_) => {name}::MESSAGE_ID,", schemas),
message_serialize = iterate!("Message::{name}(msg) => msg.serialize(serializer),", schemas),
message_from_str = iterate!(
"
{name}::MESSAGE_ID =>
Ok(Message::{name}(
serde_json::from_str(s).map_err(|e|
Error {{ message: format!(\"Invalid JSON: {{}}\", e.to_string()).to_owned() }}
)?
)),
",
schemas
),
schemafy_impl = iterate!(
"
impl MessageType for {name} {{
const MESSAGE_ID: i64 = {id};
}}
impl MessageBase for {name} {{
fn plugin_id(&self) -> &str {{
&self.data.plugin_id
}}
fn message_id(&self) -> i64 {{
Self::MESSAGE_ID
}}
}}
impl Into<{name}> for {name}MessageData {{
fn into(self) -> {} {{
{name} {{
data: self,
message_type: {name}::MESSAGE_ID,
}}
}}
}}
impl Into<Message> for {name} {{
fn into(self) -> Message {{
Message::{name}(self)
}}
}}
impl Into<Message> for {name}MessageData {{
fn into(self) -> Message {{
let msg: {name} = self.into();
msg.into()
}}
}}
",
schemas
),
)
}