1 use crate::descriptor::field_descriptor_proto::Type; 2 use crate::error::ProtobufError; 3 use crate::error::WireError; 4 use crate::reflect::EnumDescriptor; 5 use crate::reflect::MessageDescriptor; 6 use crate::reflect::ProtobufValue; 7 use crate::reflect::ReflectRepeatedMut; 8 use crate::reflect::ReflectValueBox; 9 use crate::reflect::RuntimeType; 10 use crate::wire_format::WireType; 11 use crate::CodedInputStream; 12 13 /// Runtime type and protobuf type. 14 #[derive(Debug, Clone)] 15 pub(crate) struct ProtobufType { 16 /// Runtime type. 17 runtime: RuntimeType, 18 /// Wire type. 19 t: Type, 20 } 21 22 impl ProtobufType { runtime(&self) -> &RuntimeType23 pub(crate) fn runtime(&self) -> &RuntimeType { 24 &self.runtime 25 } 26 t(&self) -> Type27 pub(crate) fn t(&self) -> Type { 28 self.t 29 } 30 _into_runtime(self) -> RuntimeType31 pub(crate) fn _into_runtime(self) -> RuntimeType { 32 self.runtime 33 } 34 message(message: MessageDescriptor) -> ProtobufType35 pub(crate) fn message(message: MessageDescriptor) -> ProtobufType { 36 ProtobufType::new(RuntimeType::Message(message), Type::TYPE_MESSAGE).unwrap() 37 } 38 enumeration(enumeration: EnumDescriptor) -> ProtobufType39 pub(crate) fn enumeration(enumeration: EnumDescriptor) -> ProtobufType { 40 ProtobufType::new(RuntimeType::Enum(enumeration), Type::TYPE_ENUM).unwrap() 41 } 42 from_proto_type(t: Type) -> ProtobufType43 pub(crate) fn from_proto_type(t: Type) -> ProtobufType { 44 ProtobufType::new(RuntimeType::from_proto_type(t), t).unwrap() 45 } 46 new(runtime: RuntimeType, t: Type) -> crate::Result<ProtobufType>47 pub(crate) fn new(runtime: RuntimeType, t: Type) -> crate::Result<ProtobufType> { 48 match (t, &runtime) { 49 (Type::TYPE_INT32, RuntimeType::I32) => {} 50 (Type::TYPE_INT64, RuntimeType::I64) => {} 51 (Type::TYPE_UINT32, RuntimeType::U32) => {} 52 (Type::TYPE_UINT64, RuntimeType::U64) => {} 53 (Type::TYPE_SINT32, RuntimeType::I32) => {} 54 (Type::TYPE_SINT64, RuntimeType::I64) => {} 55 (Type::TYPE_FIXED32, RuntimeType::U32) => {} 56 (Type::TYPE_FIXED64, RuntimeType::U64) => {} 57 (Type::TYPE_SFIXED32, RuntimeType::I32) => {} 58 (Type::TYPE_SFIXED64, RuntimeType::I64) => {} 59 (Type::TYPE_FLOAT, RuntimeType::F32) => {} 60 (Type::TYPE_DOUBLE, RuntimeType::F64) => {} 61 (Type::TYPE_BOOL, RuntimeType::Bool) => {} 62 (Type::TYPE_STRING, RuntimeType::String) => {} 63 (Type::TYPE_BYTES, RuntimeType::VecU8) => {} 64 (Type::TYPE_MESSAGE, RuntimeType::Message(..)) => {} 65 (Type::TYPE_ENUM, RuntimeType::Enum(..)) => {} 66 (Type::TYPE_GROUP, ..) => return Err(ProtobufError::GroupIsNotImplemented.into()), 67 _ => return Err(ProtobufError::IncompatibleProtobufTypeAndRuntimeType.into()), 68 } 69 Ok(ProtobufType { runtime, t }) 70 } 71 read( &self, is: &mut CodedInputStream, wire_type: WireType, ) -> crate::Result<ReflectValueBox>72 pub(crate) fn read( 73 &self, 74 is: &mut CodedInputStream, 75 wire_type: WireType, 76 ) -> crate::Result<ReflectValueBox> { 77 if wire_type != WireType::for_type(self.t) { 78 return Err(WireError::UnexpectedWireType(wire_type).into()); 79 } 80 Ok(match self.t { 81 Type::TYPE_DOUBLE => ReflectValueBox::F64(is.read_double()?), 82 Type::TYPE_FLOAT => ReflectValueBox::F32(is.read_float()?), 83 Type::TYPE_INT64 => ReflectValueBox::I64(is.read_int64()?), 84 Type::TYPE_UINT64 => ReflectValueBox::U64(is.read_uint64()?), 85 Type::TYPE_INT32 => ReflectValueBox::I32(is.read_int32()?), 86 Type::TYPE_FIXED64 => ReflectValueBox::U64(is.read_fixed64()?), 87 Type::TYPE_FIXED32 => ReflectValueBox::U32(is.read_fixed32()?), 88 Type::TYPE_BOOL => ReflectValueBox::Bool(is.read_bool()?), 89 Type::TYPE_UINT32 => ReflectValueBox::U32(is.read_uint32()?), 90 Type::TYPE_SFIXED32 => ReflectValueBox::I32(is.read_sfixed32()?), 91 Type::TYPE_SFIXED64 => ReflectValueBox::I64(is.read_sfixed64()?), 92 Type::TYPE_SINT32 => ReflectValueBox::I32(is.read_sint32()?), 93 Type::TYPE_SINT64 => ReflectValueBox::I64(is.read_sint64()?), 94 Type::TYPE_STRING => ReflectValueBox::String(is.read_string()?), 95 Type::TYPE_BYTES => ReflectValueBox::Bytes(is.read_bytes()?), 96 Type::TYPE_ENUM => match &self.runtime { 97 RuntimeType::Enum(e) => { 98 let v = is.read_enum_value()?; 99 ReflectValueBox::Enum(e.clone(), v) 100 } 101 _ => unreachable!(), 102 }, 103 Type::TYPE_GROUP => return Err(ProtobufError::GroupIsNotImplemented.into()), 104 Type::TYPE_MESSAGE => match &self.runtime { 105 RuntimeType::Message(m) => ReflectValueBox::Message(is.read_message_dyn(m)?), 106 _ => unreachable!(), 107 }, 108 }) 109 } 110 read_repeated_into( &self, is: &mut CodedInputStream, wire_type: WireType, repeated: &mut ReflectRepeatedMut, ) -> crate::Result<()>111 pub(crate) fn read_repeated_into( 112 &self, 113 is: &mut CodedInputStream, 114 wire_type: WireType, 115 repeated: &mut ReflectRepeatedMut, 116 ) -> crate::Result<()> { 117 if wire_type == WireType::for_type(self.t) { 118 let value = self.read(is, wire_type)?; 119 repeated.push(value); 120 Ok(()) 121 } else if wire_type == WireType::LengthDelimited { 122 fn extend<V: ProtobufValue>(repeated: &mut ReflectRepeatedMut, mut v: Vec<V>) { 123 repeated.extend(ReflectRepeatedMut::new(&mut v)); 124 } 125 126 match self.t { 127 Type::TYPE_INT32 => { 128 let mut v = Vec::new(); 129 is.read_repeated_packed_int32_into(&mut v)?; 130 extend(repeated, v); 131 Ok(()) 132 } 133 Type::TYPE_INT64 => { 134 let mut v = Vec::new(); 135 is.read_repeated_packed_int64_into(&mut v)?; 136 extend(repeated, v); 137 Ok(()) 138 } 139 Type::TYPE_UINT32 => { 140 let mut v = Vec::new(); 141 is.read_repeated_packed_uint32_into(&mut v)?; 142 extend(repeated, v); 143 Ok(()) 144 } 145 Type::TYPE_UINT64 => { 146 let mut v = Vec::new(); 147 is.read_repeated_packed_uint64_into(&mut v)?; 148 extend(repeated, v); 149 Ok(()) 150 } 151 Type::TYPE_SINT32 => { 152 let mut v = Vec::new(); 153 is.read_repeated_packed_sint32_into(&mut v)?; 154 extend(repeated, v); 155 Ok(()) 156 } 157 Type::TYPE_SINT64 => { 158 let mut v = Vec::new(); 159 is.read_repeated_packed_sint64_into(&mut v)?; 160 extend(repeated, v); 161 Ok(()) 162 } 163 Type::TYPE_FIXED32 => { 164 let mut v = Vec::new(); 165 is.read_repeated_packed_fixed32_into(&mut v)?; 166 extend(repeated, v); 167 Ok(()) 168 } 169 Type::TYPE_FIXED64 => { 170 let mut v = Vec::new(); 171 is.read_repeated_packed_fixed64_into(&mut v)?; 172 extend(repeated, v); 173 Ok(()) 174 } 175 Type::TYPE_SFIXED32 => { 176 let mut v = Vec::new(); 177 is.read_repeated_packed_sfixed32_into(&mut v)?; 178 extend(repeated, v); 179 Ok(()) 180 } 181 Type::TYPE_SFIXED64 => { 182 let mut v = Vec::new(); 183 is.read_repeated_packed_sfixed64_into(&mut v)?; 184 extend(repeated, v); 185 Ok(()) 186 } 187 Type::TYPE_FLOAT => { 188 let mut v = Vec::new(); 189 is.read_repeated_packed_float_into(&mut v)?; 190 extend(repeated, v); 191 Ok(()) 192 } 193 Type::TYPE_DOUBLE => { 194 let mut v = Vec::new(); 195 is.read_repeated_packed_double_into(&mut v)?; 196 extend(repeated, v); 197 Ok(()) 198 } 199 Type::TYPE_BOOL => { 200 let mut v = Vec::new(); 201 is.read_repeated_packed_bool_into(&mut v)?; 202 extend(repeated, v); 203 Ok(()) 204 } 205 Type::TYPE_ENUM => match &self.runtime { 206 RuntimeType::Enum(e) => { 207 let mut v = Vec::new(); 208 is.read_repeated_packed_enum_values_into(&mut v)?; 209 for e_v in v { 210 repeated.push(ReflectValueBox::Enum(e.clone(), e_v)); 211 } 212 Ok(()) 213 } 214 _ => unreachable!(), 215 }, 216 Type::TYPE_GROUP => Err(ProtobufError::GroupIsNotImplemented.into()), 217 Type::TYPE_MESSAGE | Type::TYPE_STRING | Type::TYPE_BYTES => { 218 Err(WireError::UnexpectedWireType(wire_type).into()) 219 } 220 } 221 } else { 222 Err(WireError::UnexpectedWireType(wire_type).into()) 223 } 224 } 225 } 226