1 use protobuf::descriptor::EnumOptions;
2 use protobuf::descriptor::FieldOptions;
3 use protobuf::descriptor::FileOptions;
4 use protobuf::descriptor::MessageOptions;
5 use protobuf::rustproto;
6
7 /// Specifies style of generated code.
8 #[derive(Default, Debug, Clone)]
9 pub struct Customize {
10 /// Make oneof enum public.
11 pub expose_oneof: Option<bool>,
12 /// When true all fields are public, and accessors are not generated
13 pub expose_fields: Option<bool>,
14 /// When false, `get_`, `set_`, `mut_` etc. accessors are not generated
15 pub generate_accessors: Option<bool>,
16 /// Use `bytes::Bytes` for `bytes` fields
17 pub carllerche_bytes_for_bytes: Option<bool>,
18 /// Use `bytes::Bytes` for `string` fields
19 pub carllerche_bytes_for_string: Option<bool>,
20 /// Implement serde_derive for messages
21 pub serde_derive: Option<bool>,
22 /// When `serde_derive` is set, serde annotations will be guarded with `#[cfg(cfg, ...)]`.
23 pub serde_derive_cfg: Option<String>,
24 /// When `serde_derive` is set, use attribute rename_all
25 pub serde_rename_all: Option<String>,
26 /// Enable lite runtime
27 pub lite_runtime: Option<bool>,
28 /// Generate `mod.rs` in the output directory.
29 ///
30 /// This option allows inclusion of generated files from cargo output directory.
31 ///
32 /// This option will likely be on by default in rust-protobuf version 3.
33 pub gen_mod_rs: Option<bool>,
34 /// Used internally to generate protos bundled in protobuf crate
35 /// like `descriptor.proto`
36 pub inside_protobuf: Option<bool>,
37
38 // When adding more options please keep in sync with `parse_from_parameter` below.
39 /// Make sure `Customize` is always used with `..Default::default()`
40 /// for future compatibility.
41 pub _future_options: (),
42 }
43
44 #[derive(Debug)]
45 pub enum CustomizeParseParameterError {
46 EqNotFound,
47 CannotParseBool,
48 UnknownOptionName(String),
49 }
50
51 pub type CustomizeParseParameterResult<T> = Result<T, CustomizeParseParameterError>;
52
53 impl Customize {
54 /// Update fields of self with fields defined in other customize
update_with(&mut self, that: &Customize)55 pub fn update_with(&mut self, that: &Customize) {
56 if let Some(v) = that.expose_oneof {
57 self.expose_oneof = Some(v);
58 }
59 if let Some(v) = that.expose_fields {
60 self.expose_fields = Some(v);
61 }
62 if let Some(v) = that.generate_accessors {
63 self.generate_accessors = Some(v);
64 }
65 if let Some(v) = that.carllerche_bytes_for_bytes {
66 self.carllerche_bytes_for_bytes = Some(v);
67 }
68 if let Some(v) = that.carllerche_bytes_for_string {
69 self.carllerche_bytes_for_string = Some(v);
70 }
71 if let Some(v) = that.serde_derive {
72 self.serde_derive = Some(v);
73 }
74 if let Some(ref v) = that.serde_derive_cfg {
75 self.serde_derive_cfg = Some(v.clone());
76 }
77 if let Some(ref v) = that.serde_rename_all {
78 self.serde_rename_all = Some(v.clone());
79 }
80 if let Some(v) = that.lite_runtime {
81 self.lite_runtime = Some(v);
82 }
83 if let Some(v) = that.gen_mod_rs {
84 self.gen_mod_rs = Some(v);
85 }
86 if let Some(v) = that.inside_protobuf {
87 self.inside_protobuf = Some(v);
88 }
89 }
90
91 /// Update unset fields of self with fields from other customize
set_defaults_from(&mut self, other: &Customize)92 pub fn set_defaults_from(&mut self, other: &Customize) {
93 let mut tmp = other.clone();
94 tmp.update_with(self);
95 *self = tmp;
96 }
97
98 /// Parse customize options from a string passed via protoc flag.
parse_from_parameter(parameter: &str) -> CustomizeParseParameterResult<Customize>99 pub fn parse_from_parameter(parameter: &str) -> CustomizeParseParameterResult<Customize> {
100 fn parse_bool(v: &str) -> CustomizeParseParameterResult<bool> {
101 v.parse()
102 .map_err(|_| CustomizeParseParameterError::CannotParseBool)
103 }
104
105 let mut r = Customize::default();
106 for nv in parameter.split_whitespace() {
107 let eq = match nv.find('=') {
108 Some(eq) => eq,
109 None => return Err(CustomizeParseParameterError::EqNotFound),
110 };
111
112 let n = &nv[..eq];
113 let v = &nv[eq + 1..];
114
115 if n == "expose_oneof" {
116 r.expose_oneof = Some(parse_bool(v)?);
117 } else if n == "expose_fields" {
118 r.expose_fields = Some(parse_bool(v)?);
119 } else if n == "generate_accessors" {
120 r.generate_accessors = Some(parse_bool(v)?);
121 } else if n == "carllerche_bytes_for_bytes" {
122 r.carllerche_bytes_for_bytes = Some(parse_bool(v)?);
123 } else if n == "carllerche_bytes_for_string" {
124 r.carllerche_bytes_for_string = Some(parse_bool(v)?);
125 } else if n == "serde_derive" {
126 r.serde_derive = Some(parse_bool(v)?);
127 } else if n == "serde_derive_cfg" {
128 r.serde_derive_cfg = Some(v.to_owned());
129 } else if n == "serde_rename_all" {
130 r.serde_rename_all = Some(v.to_owned());
131 } else if n == "lite_runtime" {
132 r.lite_runtime = Some(parse_bool(v)?);
133 } else if n == "gen_mod_rs" {
134 r.gen_mod_rs = Some(parse_bool(v)?);
135 } else if n == "inside_protobuf" {
136 r.inside_protobuf = Some(parse_bool(v)?);
137 } else {
138 return Err(CustomizeParseParameterError::UnknownOptionName(
139 n.to_owned(),
140 ));
141 }
142 }
143 Ok(r)
144 }
145 }
146
customize_from_rustproto_for_message(source: &MessageOptions) -> Customize147 pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customize {
148 let expose_oneof = rustproto::exts::expose_oneof.get(source);
149 let expose_fields = rustproto::exts::expose_fields.get(source);
150 let generate_accessors = rustproto::exts::generate_accessors.get(source);
151 let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes.get(source);
152 let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string.get(source);
153 let serde_derive = rustproto::exts::serde_derive.get(source);
154 let serde_derive_cfg = rustproto::exts::serde_derive_cfg.get(source);
155 let lite_runtime = None;
156 let gen_mod_rs = None;
157 let inside_protobuf = None;
158 let serde_rename_all = None;
159 Customize {
160 expose_oneof,
161 expose_fields,
162 generate_accessors,
163 carllerche_bytes_for_bytes,
164 carllerche_bytes_for_string,
165 serde_derive,
166 serde_derive_cfg,
167 serde_rename_all,
168 lite_runtime,
169 gen_mod_rs,
170 inside_protobuf,
171 _future_options: (),
172 }
173 }
174
customize_from_rustproto_for_enum(source: &EnumOptions) -> Customize175 pub fn customize_from_rustproto_for_enum(source: &EnumOptions) -> Customize {
176 let serde_rename_all = rustproto::exts::serde_rename_all.get(source);
177 let mut r = Customize::default();
178 r.serde_rename_all = serde_rename_all;
179 return r;
180 }
181
customize_from_rustproto_for_field(source: &FieldOptions) -> Customize182 pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
183 let expose_oneof = None;
184 let expose_fields = rustproto::exts::expose_fields_field.get(source);
185 let generate_accessors = rustproto::exts::generate_accessors_field.get(source);
186 let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_field.get(source);
187 let carllerche_bytes_for_string =
188 rustproto::exts::carllerche_bytes_for_string_field.get(source);
189 let serde_rename_all = None;
190 let serde_derive = None;
191 let serde_derive_cfg = None;
192 let lite_runtime = None;
193 let gen_mod_rs = None;
194 let inside_protobuf = None;
195 Customize {
196 expose_oneof,
197 expose_fields,
198 generate_accessors,
199 carllerche_bytes_for_bytes,
200 carllerche_bytes_for_string,
201 serde_derive,
202 serde_derive_cfg,
203 serde_rename_all,
204 lite_runtime,
205 gen_mod_rs,
206 inside_protobuf,
207 _future_options: (),
208 }
209 }
210
customize_from_rustproto_for_file(source: &FileOptions) -> Customize211 pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
212 let expose_oneof = rustproto::exts::expose_oneof_all.get(source);
213 let expose_fields = rustproto::exts::expose_fields_all.get(source);
214 let generate_accessors = rustproto::exts::generate_accessors_all.get(source);
215 let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_all.get(source);
216 let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_all.get(source);
217 let serde_derive = rustproto::exts::serde_derive_all.get(source);
218 let serde_derive_cfg = rustproto::exts::serde_derive_cfg_all.get(source);
219 let lite_runtime = rustproto::exts::lite_runtime_all.get(source);
220 let gen_mod_rs = None;
221 let inside_protobuf = None;
222 let serde_rename_all = None;
223 Customize {
224 expose_oneof,
225 expose_fields,
226 generate_accessors,
227 carllerche_bytes_for_bytes,
228 carllerche_bytes_for_string,
229 serde_derive,
230 serde_derive_cfg,
231 serde_rename_all,
232 lite_runtime,
233 inside_protobuf,
234 gen_mod_rs,
235 _future_options: (),
236 }
237 }
238