aboutsummaryrefslogtreecommitdiff
path: root/src/gen/field/option_kind.rs
blob: 0fde11a94062ee8aaaa92519fd46da6670b3f2ca (plain)
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
use crate::gen::inside::protobuf_crate_path;
use crate::gen::rust_types_values::RustType;
use crate::Customize;

/// Optional fields can be stored are `Option<T>` or `SingularPtrField<T>`.
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum OptionKind {
    /// Field is `Option<T>`
    Option,
    /// Field is `SingularPtrField<T>`
    MessageField,
}

impl OptionKind {
    pub(crate) fn wrap_element(&self, element_type: RustType) -> RustType {
        let element_type = Box::new(element_type);
        match self {
            OptionKind::Option => RustType::Option(element_type),
            OptionKind::MessageField => RustType::MessageField(element_type),
        }
    }

    // Type of `as_ref()` operation
    pub(crate) fn as_ref_type(&self, element_type: RustType) -> RustType {
        match self {
            OptionKind::Option => RustType::Option(Box::new(element_type.ref_type())),
            OptionKind::MessageField => RustType::MessageField(Box::new(element_type.ref_type())),
        }
    }

    fn _as_option_ref(&self, v: &str) -> String {
        match self {
            OptionKind::Option | OptionKind::MessageField => format!("{}.as_ref()", v),
        }
    }

    pub(crate) fn unwrap_or_else(&self, what: &str, default_value: &str) -> String {
        match self {
            _ => format!("{}.unwrap_or_else(|| {})", what, default_value),
        }
    }

    pub(crate) fn unwrap_ref_or_else(&self, what: &str, default_value: &str) -> String {
        match self {
            _ => format!("{}.unwrap_or_else(|| {})", what, default_value),
        }
    }

    pub(crate) fn wrap_value(&self, value: &str, customize: &Customize) -> String {
        match self {
            OptionKind::Option => format!("::std::option::Option::Some({})", value),
            OptionKind::MessageField => format!(
                "{}::MessageField::some({})",
                protobuf_crate_path(customize),
                value
            ),
        }
    }
}