1 use crate::gen::inside::protobuf_crate_path; 2 use crate::gen::rust_types_values::RustType; 3 use crate::Customize; 4 5 /// Optional fields can be stored are `Option<T>` or `SingularPtrField<T>`. 6 #[derive(Copy, Clone, Eq, PartialEq)] 7 pub enum OptionKind { 8 /// Field is `Option<T>` 9 Option, 10 /// Field is `SingularPtrField<T>` 11 MessageField, 12 } 13 14 impl OptionKind { wrap_element(&self, element_type: RustType) -> RustType15 pub(crate) fn wrap_element(&self, element_type: RustType) -> RustType { 16 let element_type = Box::new(element_type); 17 match self { 18 OptionKind::Option => RustType::Option(element_type), 19 OptionKind::MessageField => RustType::MessageField(element_type), 20 } 21 } 22 23 // Type of `as_ref()` operation as_ref_type(&self, element_type: RustType) -> RustType24 pub(crate) fn as_ref_type(&self, element_type: RustType) -> RustType { 25 match self { 26 OptionKind::Option => RustType::Option(Box::new(element_type.ref_type())), 27 OptionKind::MessageField => RustType::MessageField(Box::new(element_type.ref_type())), 28 } 29 } 30 _as_option_ref(&self, v: &str) -> String31 fn _as_option_ref(&self, v: &str) -> String { 32 match self { 33 OptionKind::Option | OptionKind::MessageField => format!("{}.as_ref()", v), 34 } 35 } 36 unwrap_or_else(&self, what: &str, default_value: &str) -> String37 pub(crate) fn unwrap_or_else(&self, what: &str, default_value: &str) -> String { 38 match self { 39 _ => format!("{}.unwrap_or_else(|| {})", what, default_value), 40 } 41 } 42 unwrap_ref_or_else(&self, what: &str, default_value: &str) -> String43 pub(crate) fn unwrap_ref_or_else(&self, what: &str, default_value: &str) -> String { 44 match self { 45 _ => format!("{}.unwrap_or_else(|| {})", what, default_value), 46 } 47 } 48 wrap_value(&self, value: &str, customize: &Customize) -> String49 pub(crate) fn wrap_value(&self, value: &str, customize: &Customize) -> String { 50 match self { 51 OptionKind::Option => format!("::std::option::Option::Some({})", value), 52 OptionKind::MessageField => format!( 53 "{}::MessageField::some({})", 54 protobuf_crate_path(customize), 55 value 56 ), 57 } 58 } 59 } 60