1 use crate::gen::field::elem::FieldElem; 2 use crate::gen::field::option_kind::OptionKind; 3 use crate::gen::file_and_mod::FileAndMod; 4 use crate::gen::rust_types_values::RustType; 5 use crate::Customize; 6 7 #[derive(Clone, PartialEq, Eq, Copy)] 8 pub enum SingularFieldFlag { 9 // proto2 or proto3 message 10 WithFlag { 11 required: bool, 12 option_kind: OptionKind, 13 }, 14 // proto3 15 WithoutFlag, 16 } 17 18 impl SingularFieldFlag { is_required(&self) -> bool19 pub fn is_required(&self) -> bool { 20 match *self { 21 SingularFieldFlag::WithFlag { required, .. } => required, 22 SingularFieldFlag::WithoutFlag => false, 23 } 24 } 25 } 26 27 #[derive(Clone)] 28 pub(crate) struct SingularField<'a> { 29 pub flag: SingularFieldFlag, 30 pub elem: FieldElem<'a>, 31 } 32 33 impl<'a> SingularField<'a> { rust_storage_type(&self, reference: &FileAndMod) -> RustType34 pub(crate) fn rust_storage_type(&self, reference: &FileAndMod) -> RustType { 35 match self.flag { 36 SingularFieldFlag::WithFlag { option_kind, .. } => { 37 option_kind.wrap_element(self.elem.rust_storage_elem_type(reference)) 38 } 39 SingularFieldFlag::WithoutFlag => self.elem.rust_storage_elem_type(reference), 40 } 41 } 42 default_value( &self, customize: &Customize, reference: &FileAndMod, const_expr: bool, ) -> String43 pub(crate) fn default_value( 44 &self, 45 customize: &Customize, 46 reference: &FileAndMod, 47 const_expr: bool, 48 ) -> String { 49 self.rust_storage_type(reference) 50 .default_value(customize, const_expr) 51 } 52 } 53