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
use super::{DeviceSize, DeviceType};
use failure::Fail;
use serde::de;
use serde_derive::Deserialize;
use std::fmt;
use std::str::FromStr;
#[derive(Deserialize)]
pub struct RegistrationInfoDevice {
pub id: String,
pub name: Option<String>,
pub size: DeviceSize,
#[serde(rename = "type")]
pub _type: Option<DeviceType>,
}
pub enum Language {
English,
French,
German,
Spanish,
Japanese,
ChineseChina,
Unknown(String),
}
impl<'de> de::Deserialize<'de> for Language {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Language;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}
fn visit_str<E>(self, value: &str) -> Result<Language, E>
where
E: de::Error,
{
Ok(match value {
"en" => Language::English,
"fr" => Language::French,
"de" => Language::German,
"es" => Language::Spanish,
"ja" => Language::Japanese,
"zh_cn" => Language::ChineseChina,
value => Language::Unknown(value.to_string()),
})
}
}
deserializer.deserialize_str(Visitor)
}
}
pub enum Platform {
Mac,
Windows,
Unknown(String),
}
impl<'de: 'a, 'a> de::Deserialize<'de> for Platform {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Platform;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}
fn visit_str<E>(self, value: &str) -> Result<Platform, E>
where
E: de::Error,
{
Ok(match value {
"mac" => Platform::Mac,
"windows" => Platform::Windows,
value => Platform::Unknown(value.to_string()),
})
}
}
deserializer.deserialize_str(Visitor)
}
}
#[derive(Deserialize)]
pub struct RegistrationInfoApplication {
pub language: Language,
pub platform: Platform,
pub version: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegistrationInfo {
pub application: RegistrationInfoApplication,
pub device_pixel_ratio: u8,
pub devices: Vec<RegistrationInfoDevice>,
}
#[derive(Deserialize)]
pub struct RegistrationParams {
pub port: u16,
pub uuid: String,
pub event: String,
pub info: RegistrationInfo,
}
#[derive(Debug, Fail)]
pub enum RegistrationParamsError {
#[fail(display = "port not provided")]
NoPort,
#[fail(display = "port could not be parsed")]
BadPort(#[fail(cause)] std::num::ParseIntError),
#[fail(display = "uuid not provided")]
NoUuid,
#[fail(display = "event not provided")]
NoEvent,
#[fail(display = "info not provided")]
NoInfo,
#[fail(display = "info could not be parsed")]
BadInfo(#[fail(cause)] serde_json::Error),
}
impl RegistrationParams {
pub fn from_args<I: IntoIterator<Item = String>>(
args: I,
) -> Result<RegistrationParams, RegistrationParamsError> {
let mut iter = args.into_iter();
let mut port = None;
let mut uuid = None;
let mut event = None;
let mut info = None;
loop {
match iter.next().as_deref() {
Some("-port") => port = iter.next().map(|a| u16::from_str(&a)),
Some("-pluginUUID") => uuid = iter.next(),
Some("-registerEvent") => event = iter.next(),
Some("-info") => info = iter.next().map(|a| serde_json::from_str(&a)),
Some(_) => {}
None => break,
}
}
let port = port
.ok_or(RegistrationParamsError::NoPort)?
.map_err(RegistrationParamsError::BadPort)?;
let uuid = uuid.ok_or(RegistrationParamsError::NoUuid)?;
let event = event.ok_or(RegistrationParamsError::NoEvent)?;
let info = info
.ok_or(RegistrationParamsError::NoInfo)?
.map_err(RegistrationParamsError::BadInfo)?;
Ok(RegistrationParams {
port,
uuid,
event,
info,
})
}
}