1 use protobuf::descriptor::field_descriptor_proto::Type; 2 3 use crate::gen::rust_types_values::PrimitiveTypeVariant; 4 use crate::gen::rust_types_values::RustType; 5 6 pub(crate) trait TypeExt { read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String7 fn read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String; is_s_varint(&self) -> bool8 fn is_s_varint(&self) -> bool; is_copy(&self) -> bool9 fn is_copy(&self) -> bool; protobuf_name(&self) -> &'static str10 fn protobuf_name(&self) -> &'static str; rust_type(&self) -> RustType11 fn rust_type(&self) -> RustType; os_write_fn_param_type(&self) -> RustType12 fn os_write_fn_param_type(&self) -> RustType; encoded_size(&self) -> Option<u32>13 fn encoded_size(&self) -> Option<u32>; 14 } 15 16 impl TypeExt for Type { read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String17 fn read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String { 18 match (self, primitive_type_variant) { 19 (Type::TYPE_ENUM, _) => format!("{}.read_enum_or_unknown()", is), 20 (Type::TYPE_BYTES, PrimitiveTypeVariant::TokioBytes) => { 21 format!("{}.read_tokio_bytes()", is) 22 } 23 (Type::TYPE_STRING, PrimitiveTypeVariant::TokioBytes) => { 24 format!("{}.read_tokio_chars()", is) 25 } 26 _ => format!("{}.read_{}()", is, self.protobuf_name()), 27 } 28 } 29 30 /// True if self is signed integer with zigzag encoding is_s_varint(&self) -> bool31 fn is_s_varint(&self) -> bool { 32 match *self { 33 Type::TYPE_SINT32 | Type::TYPE_SINT64 => true, 34 _ => false, 35 } 36 } 37 is_copy(&self) -> bool38 fn is_copy(&self) -> bool { 39 match self { 40 Type::TYPE_MESSAGE | Type::TYPE_STRING | Type::TYPE_BYTES => false, 41 _ => true, 42 } 43 } 44 protobuf_name(&self) -> &'static str45 fn protobuf_name(&self) -> &'static str { 46 match self { 47 Type::TYPE_DOUBLE => "double", 48 Type::TYPE_FLOAT => "float", 49 Type::TYPE_INT32 => "int32", 50 Type::TYPE_INT64 => "int64", 51 Type::TYPE_UINT32 => "uint32", 52 Type::TYPE_UINT64 => "uint64", 53 Type::TYPE_SINT32 => "sint32", 54 Type::TYPE_SINT64 => "sint64", 55 Type::TYPE_FIXED32 => "fixed32", 56 Type::TYPE_FIXED64 => "fixed64", 57 Type::TYPE_SFIXED32 => "sfixed32", 58 Type::TYPE_SFIXED64 => "sfixed64", 59 Type::TYPE_BOOL => "bool", 60 Type::TYPE_STRING => "string", 61 Type::TYPE_BYTES => "bytes", 62 Type::TYPE_ENUM => "enum", 63 Type::TYPE_MESSAGE => "message", 64 Type::TYPE_GROUP => "group", 65 } 66 } 67 68 /// Rust type for protobuf base type. rust_type(&self) -> RustType69 fn rust_type(&self) -> RustType { 70 match self { 71 Type::TYPE_DOUBLE => RustType::Float(64), 72 Type::TYPE_FLOAT => RustType::Float(32), 73 Type::TYPE_INT32 => RustType::Int(true, 32), 74 Type::TYPE_INT64 => RustType::Int(true, 64), 75 Type::TYPE_UINT32 => RustType::Int(false, 32), 76 Type::TYPE_UINT64 => RustType::Int(false, 64), 77 Type::TYPE_SINT32 => RustType::Int(true, 32), 78 Type::TYPE_SINT64 => RustType::Int(true, 64), 79 Type::TYPE_FIXED32 => RustType::Int(false, 32), 80 Type::TYPE_FIXED64 => RustType::Int(false, 64), 81 Type::TYPE_SFIXED32 => RustType::Int(true, 32), 82 Type::TYPE_SFIXED64 => RustType::Int(true, 64), 83 Type::TYPE_BOOL => RustType::Bool, 84 Type::TYPE_STRING => RustType::String, 85 Type::TYPE_BYTES => RustType::Vec(Box::new(RustType::u8())), 86 Type::TYPE_ENUM | Type::TYPE_GROUP | Type::TYPE_MESSAGE => { 87 panic!("there is no rust name for {:?}", self) 88 } 89 } 90 } 91 92 // type of `v` in `os.write_xxx_no_tag(v)` os_write_fn_param_type(&self) -> RustType93 fn os_write_fn_param_type(&self) -> RustType { 94 match self { 95 Type::TYPE_STRING => RustType::amp_str(), 96 Type::TYPE_BYTES => RustType::amp_slice_of_u8(), 97 Type::TYPE_ENUM => RustType::i32(), 98 t => t.rust_type(), 99 } 100 } 101 102 /// Size of value for type, None if variable. encoded_size(&self) -> Option<u32>103 fn encoded_size(&self) -> Option<u32> { 104 match self { 105 Type::TYPE_BOOL => Some(1), 106 Type::TYPE_FIXED32 => Some(4), 107 Type::TYPE_FIXED64 => Some(8), 108 Type::TYPE_SFIXED32 => Some(4), 109 Type::TYPE_SFIXED64 => Some(8), 110 Type::TYPE_FLOAT => Some(4), 111 Type::TYPE_DOUBLE => Some(8), 112 _ => None, 113 } 114 } 115 } 116