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
use protobuf::descriptor::EnumOptions;
use protobuf::descriptor::FieldOptions;
use protobuf::descriptor::FileOptions;
use protobuf::descriptor::MessageOptions;
use protobuf::rustproto;
#[derive(Default, Debug, Clone)]
pub struct Customize {
pub expose_oneof: Option<bool>,
pub expose_fields: Option<bool>,
pub generate_accessors: Option<bool>,
pub carllerche_bytes_for_bytes: Option<bool>,
pub carllerche_bytes_for_string: Option<bool>,
pub serde_derive: Option<bool>,
pub serde_derive_cfg: Option<String>,
pub serde_rename_all: Option<String>,
pub lite_runtime: Option<bool>,
pub gen_mod_rs: Option<bool>,
pub inside_protobuf: Option<bool>,
pub _future_options: (),
}
#[derive(Debug)]
pub enum CustomizeParseParameterError {
EqNotFound,
CannotParseBool,
UnknownOptionName(String),
}
pub type CustomizeParseParameterResult<T> = Result<T, CustomizeParseParameterError>;
impl Customize {
pub fn update_with(&mut self, that: &Customize) {
if let Some(v) = that.expose_oneof {
self.expose_oneof = Some(v);
}
if let Some(v) = that.expose_fields {
self.expose_fields = Some(v);
}
if let Some(v) = that.generate_accessors {
self.generate_accessors = Some(v);
}
if let Some(v) = that.carllerche_bytes_for_bytes {
self.carllerche_bytes_for_bytes = Some(v);
}
if let Some(v) = that.carllerche_bytes_for_string {
self.carllerche_bytes_for_string = Some(v);
}
if let Some(v) = that.serde_derive {
self.serde_derive = Some(v);
}
if let Some(ref v) = that.serde_derive_cfg {
self.serde_derive_cfg = Some(v.clone());
}
if let Some(ref v) = that.serde_rename_all {
self.serde_rename_all = Some(v.clone());
}
if let Some(v) = that.lite_runtime {
self.lite_runtime = Some(v);
}
if let Some(v) = that.gen_mod_rs {
self.gen_mod_rs = Some(v);
}
if let Some(v) = that.inside_protobuf {
self.inside_protobuf = Some(v);
}
}
pub fn set_defaults_from(&mut self, other: &Customize) {
let mut tmp = other.clone();
tmp.update_with(self);
*self = tmp;
}
pub fn parse_from_parameter(parameter: &str) -> CustomizeParseParameterResult<Customize> {
fn parse_bool(v: &str) -> CustomizeParseParameterResult<bool> {
v.parse()
.map_err(|_| CustomizeParseParameterError::CannotParseBool)
}
let mut r = Customize::default();
for nv in parameter.split_whitespace() {
let eq = match nv.find('=') {
Some(eq) => eq,
None => return Err(CustomizeParseParameterError::EqNotFound),
};
let n = &nv[..eq];
let v = &nv[eq + 1..];
if n == "expose_oneof" {
r.expose_oneof = Some(parse_bool(v)?);
} else if n == "expose_fields" {
r.expose_fields = Some(parse_bool(v)?);
} else if n == "generate_accessors" {
r.generate_accessors = Some(parse_bool(v)?);
} else if n == "carllerche_bytes_for_bytes" {
r.carllerche_bytes_for_bytes = Some(parse_bool(v)?);
} else if n == "carllerche_bytes_for_string" {
r.carllerche_bytes_for_string = Some(parse_bool(v)?);
} else if n == "serde_derive" {
r.serde_derive = Some(parse_bool(v)?);
} else if n == "serde_derive_cfg" {
r.serde_derive_cfg = Some(v.to_owned());
} else if n == "serde_rename_all" {
r.serde_rename_all = Some(v.to_owned());
} else if n == "lite_runtime" {
r.lite_runtime = Some(parse_bool(v)?);
} else if n == "gen_mod_rs" {
r.gen_mod_rs = Some(parse_bool(v)?);
} else if n == "inside_protobuf" {
r.inside_protobuf = Some(parse_bool(v)?);
} else {
return Err(CustomizeParseParameterError::UnknownOptionName(
n.to_owned(),
));
}
}
Ok(r)
}
}
pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customize {
let expose_oneof = rustproto::exts::expose_oneof.get(source);
let expose_fields = rustproto::exts::expose_fields.get(source);
let generate_accessors = rustproto::exts::generate_accessors.get(source);
let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes.get(source);
let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string.get(source);
let serde_derive = rustproto::exts::serde_derive.get(source);
let serde_derive_cfg = rustproto::exts::serde_derive_cfg.get(source);
let lite_runtime = None;
let gen_mod_rs = None;
let inside_protobuf = None;
let serde_rename_all = None;
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
serde_derive,
serde_derive_cfg,
serde_rename_all,
lite_runtime,
gen_mod_rs,
inside_protobuf,
_future_options: (),
}
}
pub fn customize_from_rustproto_for_enum(source: &EnumOptions) -> Customize {
let serde_rename_all = rustproto::exts::serde_rename_all.get(source);
let mut r = Customize::default();
r.serde_rename_all = serde_rename_all;
return r;
}
pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
let expose_oneof = None;
let expose_fields = rustproto::exts::expose_fields_field.get(source);
let generate_accessors = rustproto::exts::generate_accessors_field.get(source);
let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_field.get(source);
let carllerche_bytes_for_string =
rustproto::exts::carllerche_bytes_for_string_field.get(source);
let serde_rename_all = None;
let serde_derive = None;
let serde_derive_cfg = None;
let lite_runtime = None;
let gen_mod_rs = None;
let inside_protobuf = None;
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
serde_derive,
serde_derive_cfg,
serde_rename_all,
lite_runtime,
gen_mod_rs,
inside_protobuf,
_future_options: (),
}
}
pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
let expose_oneof = rustproto::exts::expose_oneof_all.get(source);
let expose_fields = rustproto::exts::expose_fields_all.get(source);
let generate_accessors = rustproto::exts::generate_accessors_all.get(source);
let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_all.get(source);
let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_all.get(source);
let serde_derive = rustproto::exts::serde_derive_all.get(source);
let serde_derive_cfg = rustproto::exts::serde_derive_cfg_all.get(source);
let lite_runtime = rustproto::exts::lite_runtime_all.get(source);
let gen_mod_rs = None;
let inside_protobuf = None;
let serde_rename_all = None;
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
serde_derive,
serde_derive_cfg,
serde_rename_all,
lite_runtime,
inside_protobuf,
gen_mod_rs,
_future_options: (),
}
}