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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::fmt;

use crate::descriptor::field_descriptor_proto;
use crate::reflect::EnumDescriptor;
use crate::reflect::MessageDescriptor;
use crate::reflect::MessageRef;
use crate::reflect::ReflectValueBox;
use crate::reflect::ReflectValueRef;
use crate::text_format;
use crate::text_format::lexer::float::parse_protobuf_float;

/// Runtime representation of elementary protobuf type.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum RuntimeTypeBox {
    /// `i32`
    I32,
    /// `i64`
    I64,
    /// `u32`
    U32,
    /// `u64`
    U64,
    /// `f32`
    F32,
    /// `f64`
    F64,
    /// `bool`
    Bool,
    /// [`String`](std::string::String)
    String,
    /// [`Vec<u8>`](std::vec::Vec)
    VecU8,
    /// `enum`
    Enum(EnumDescriptor),
    /// `message`
    Message(MessageDescriptor),
}

impl RuntimeTypeBox {
    pub(crate) fn default_value_ref(&self) -> ReflectValueRef<'static> {
        match self {
            RuntimeTypeBox::I32 => ReflectValueRef::I32(0),
            RuntimeTypeBox::I64 => ReflectValueRef::I64(0),
            RuntimeTypeBox::U32 => ReflectValueRef::U32(0),
            RuntimeTypeBox::U64 => ReflectValueRef::U64(0),
            RuntimeTypeBox::F32 => ReflectValueRef::F32(0.0),
            RuntimeTypeBox::F64 => ReflectValueRef::F64(0.0),
            RuntimeTypeBox::Bool => ReflectValueRef::Bool(false),
            RuntimeTypeBox::String => ReflectValueRef::String(""),
            RuntimeTypeBox::VecU8 => ReflectValueRef::Bytes(b""),
            RuntimeTypeBox::Enum(e) => {
                ReflectValueRef::Enum(e.clone(), e.get_default_value().value())
            }
            RuntimeTypeBox::Message(m) => ReflectValueRef::Message(MessageRef::default_instance(m)),
        }
    }

    /// Rust type from protobuf type.
    ///
    /// # Panics
    ///
    /// Panics for message or enum types (because they can't be resolved without context).
    pub(crate) fn from_proto_type(t: field_descriptor_proto::Type) -> RuntimeTypeBox {
        match t {
            field_descriptor_proto::Type::TYPE_UINT32 => RuntimeTypeBox::U32,
            field_descriptor_proto::Type::TYPE_UINT64 => RuntimeTypeBox::U64,
            field_descriptor_proto::Type::TYPE_INT32 => RuntimeTypeBox::I32,
            field_descriptor_proto::Type::TYPE_INT64 => RuntimeTypeBox::I64,
            field_descriptor_proto::Type::TYPE_SINT32 => RuntimeTypeBox::I32,
            field_descriptor_proto::Type::TYPE_SINT64 => RuntimeTypeBox::I64,
            field_descriptor_proto::Type::TYPE_FIXED32 => RuntimeTypeBox::U32,
            field_descriptor_proto::Type::TYPE_FIXED64 => RuntimeTypeBox::U64,
            field_descriptor_proto::Type::TYPE_SFIXED64 => RuntimeTypeBox::I64,
            field_descriptor_proto::Type::TYPE_SFIXED32 => RuntimeTypeBox::I32,
            field_descriptor_proto::Type::TYPE_BOOL => RuntimeTypeBox::Bool,
            field_descriptor_proto::Type::TYPE_STRING => RuntimeTypeBox::String,
            field_descriptor_proto::Type::TYPE_BYTES => RuntimeTypeBox::VecU8,
            field_descriptor_proto::Type::TYPE_FLOAT => RuntimeTypeBox::F32,
            field_descriptor_proto::Type::TYPE_DOUBLE => RuntimeTypeBox::F64,
            field_descriptor_proto::Type::TYPE_ENUM
            | field_descriptor_proto::Type::TYPE_MESSAGE
            | field_descriptor_proto::Type::TYPE_GROUP => panic!(
                "{:?} cannot be converted to runtime type without context",
                t
            ),
        }
    }

    pub(crate) fn parse_proto_default_value(&self, value: &str) -> ReflectValueBox {
        match self {
            // For booleans, "true" or "false"
            RuntimeTypeBox::Bool => ReflectValueBox::Bool(if value == "true" {
                true
            } else if value == "false" {
                false
            } else {
                panic!("cannot parse bool default value: {}", value)
            }),
            RuntimeTypeBox::I32 => ReflectValueBox::I32(value.parse().unwrap()),
            RuntimeTypeBox::I64 => ReflectValueBox::I64(value.parse().unwrap()),
            RuntimeTypeBox::U32 => ReflectValueBox::U32(value.parse().unwrap()),
            RuntimeTypeBox::U64 => ReflectValueBox::U64(value.parse().unwrap()),
            RuntimeTypeBox::F32 => {
                ReflectValueBox::F32(parse_protobuf_float(value).unwrap() as f32)
            }
            RuntimeTypeBox::F64 => ReflectValueBox::F64(parse_protobuf_float(value).unwrap()),
            // For strings, contains the default text contents (not escaped in any way)
            RuntimeTypeBox::String => ReflectValueBox::String(value.to_owned()),
            // For bytes, contains the C escaped value.  All bytes >= 128 are escaped
            RuntimeTypeBox::VecU8 => ReflectValueBox::Bytes(
                text_format::lexer::StrLit {
                    escaped: value.to_owned(),
                }
                .decode_bytes()
                .expect("decoded bytes default value"),
            ),
            t => unimplemented!("not implemented for {:?}", t),
        }
    }
}

impl fmt::Display for RuntimeTypeBox {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RuntimeTypeBox::I32 => write!(f, "i32"),
            RuntimeTypeBox::I64 => write!(f, "i64"),
            RuntimeTypeBox::U32 => write!(f, "u32"),
            RuntimeTypeBox::U64 => write!(f, "u64"),
            RuntimeTypeBox::F32 => write!(f, "f32"),
            RuntimeTypeBox::F64 => write!(f, "f64"),
            RuntimeTypeBox::Bool => write!(f, "bool"),
            RuntimeTypeBox::String => write!(f, "String"),
            RuntimeTypeBox::VecU8 => write!(f, "Vec<u8>"),
            RuntimeTypeBox::Enum(e) => write!(f, "{}", e.full_name()),
            RuntimeTypeBox::Message(m) => write!(f, "{}", m.full_name()),
        }
    }
}