-
Notifications
You must be signed in to change notification settings - Fork 382
/
customize.rs
94 lines (86 loc) · 3.55 KB
/
customize.rs
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
use protobuf::rustproto;
use protobuf::descriptor::FieldOptions;
use protobuf::descriptor::MessageOptions;
use protobuf::descriptor::FileOptions;
/// Specifies style of generated code.
#[derive(Default, Debug, Clone)]
pub struct Customize {
/// Make oneof enum public.
pub expose_oneof: Option<bool>,
/// When true all fields are public, and accessors are not generated
pub expose_fields: Option<bool>,
/// When false, `get_`, `set_`, `mut_` etc. accessors are not generated
pub generate_accessors: Option<bool>,
/// Use `bytes::Bytes` for `bytes` fields
pub carllerche_bytes_for_bytes: Option<bool>,
/// Use `bytes::Bytes` for `string` fields
pub carllerche_bytes_for_string: Option<bool>,
}
impl Customize {
/// Update fields of self with fields defined in other 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);
}
}
/// Update unset fields of self with fields from other customize
pub fn set_defaults_from(&mut self, other: &Customize) {
let mut tmp = other.clone();
tmp.update_with(self);
*self = tmp;
}
}
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);
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
}
}
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);
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
}
}
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);
Customize {
expose_oneof,
expose_fields,
generate_accessors,
carllerche_bytes_for_bytes,
carllerche_bytes_for_string,
}
}