protobuf_codegen_no_elision/
customize.rs

1use protobuf::rustproto;
2use protobuf::descriptor::FieldOptions;
3use protobuf::descriptor::MessageOptions;
4use protobuf::descriptor::FileOptions;
5
6
7/// Specifies style of generated code.
8#[derive(Default, Debug, Clone)]
9pub 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}
21
22impl Customize {
23    /// Update fields of self with fields defined in other customize
24    pub fn update_with(&mut self, that: &Customize) {
25        if let Some(v) = that.expose_oneof {
26            self.expose_oneof = Some(v);
27        }
28        if let Some(v) = that.expose_fields {
29            self.expose_fields = Some(v);
30        }
31        if let Some(v) = that.generate_accessors {
32            self.generate_accessors = Some(v);
33        }
34        if let Some(v) = that.carllerche_bytes_for_bytes {
35            self.carllerche_bytes_for_bytes = Some(v);
36        }
37        if let Some(v) = that.carllerche_bytes_for_string {
38            self.carllerche_bytes_for_string = Some(v);
39        }
40    }
41
42    /// Update unset fields of self with fields from other customize
43    pub fn set_defaults_from(&mut self, other: &Customize) {
44        let mut tmp = other.clone();
45        tmp.update_with(self);
46        *self = tmp;
47    }
48}
49
50
51pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customize {
52    let expose_oneof = rustproto::exts::expose_oneof.get(source);
53    let expose_fields = rustproto::exts::expose_fields.get(source);
54    let generate_accessors = rustproto::exts::generate_accessors.get(source);
55    let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes.get(source);
56    let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string.get(source);
57    Customize {
58        expose_oneof,
59        expose_fields,
60        generate_accessors,
61        carllerche_bytes_for_bytes,
62        carllerche_bytes_for_string,
63    }
64}
65
66pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
67    let expose_oneof = None;
68    let expose_fields = rustproto::exts::expose_fields_field.get(source);
69    let generate_accessors = rustproto::exts::generate_accessors_field.get(source);
70    let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_field.get(source);
71    let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_field.get(source);
72    Customize {
73        expose_oneof,
74        expose_fields,
75        generate_accessors,
76        carllerche_bytes_for_bytes,
77        carllerche_bytes_for_string,
78    }
79}
80
81pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
82    let expose_oneof = rustproto::exts::expose_oneof_all.get(source);
83    let expose_fields = rustproto::exts::expose_fields_all.get(source);
84    let generate_accessors = rustproto::exts::generate_accessors_all.get(source);
85    let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_all.get(source);
86    let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_all.get(source);
87    Customize {
88        expose_oneof,
89        expose_fields,
90        generate_accessors,
91        carllerche_bytes_for_bytes,
92        carllerche_bytes_for_string,
93    }
94}