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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
use std; use byteorder::{ByteOrder, LittleEndian}; use trackable::error::ErrorKindExt; use {Result, ErrorKind}; use traits::{FieldType, TryFrom, Message, Packable, MapKey}; use wire::WireType; use wire::types::{Varint, Bit32, Bit64, LengthDelimited}; macro_rules! impl_scalar_type { ($t:ident, $base:ty, $wire:ident) => { impl FieldType for $t { fn wire_type() -> WireType { WireType::$wire } } impl From<$base> for $t { fn from(f: $base) -> Self { $t(f) } } impl From<$t> for $base { fn from(f: $t) -> Self { f.0 } } } } #[derive(Debug, Default)] pub struct Bool(pub bool); impl_scalar_type!(Bool, bool, Varint); impl Packable for Bool {} impl TryFrom<Varint> for Bool { fn try_from(f: Varint) -> Result<Self> { track_assert!( f.0 <= 1, ErrorKind::Invalid, "Tool large `bool` value: {}", f.0 ); Ok(Bool(f.0 != 0)) } } impl MapKey for Bool {} #[derive(Debug, Default)] pub struct Int32(pub i32); impl_scalar_type!(Int32, i32, Varint); impl Packable for Int32 {} impl TryFrom<Varint> for Int32 { fn try_from(f: Varint) -> Result<Self> { let n = f.0 as i64; track_assert!( n >= std::i32::MIN as i64, ErrorKind::Invalid, "Tool small `int32` value: {}", n ); track_assert!( n <= std::i32::MAX as i64, ErrorKind::Invalid, "Tool large `int32` value: {}", n ); Ok(Int32(f.0 as i32)) } } impl MapKey for Int32 {} #[derive(Debug, Default)] pub struct Int64(pub i64); impl_scalar_type!(Int64, i64, Varint); impl Packable for Int64 {} impl From<Varint> for Int64 { fn from(f: Varint) -> Self { Int64(f.0 as i64) } } impl MapKey for Int64 {} #[derive(Debug, Default)] pub struct Uint32(pub u32); impl_scalar_type!(Uint32, u32, Varint); impl Packable for Uint32 {} impl TryFrom<Varint> for Uint32 { fn try_from(f: Varint) -> Result<Self> { track_assert!( f.0 <= std::u32::MAX as u64, ErrorKind::Invalid, "Tool large `uint32` value: {}", f.0 ); Ok(Uint32(f.0 as u32)) } } impl MapKey for Uint32 {} #[derive(Debug, Default)] pub struct Uint64(pub u64); impl_scalar_type!(Uint64, u64, Varint); impl Packable for Uint64 {} impl From<Varint> for Uint64 { fn from(f: Varint) -> Self { Uint64(f.0) } } impl MapKey for Uint64 {} #[derive(Debug, Default)] pub struct Sint32(pub i32); impl_scalar_type!(Sint32, i32, Varint); impl Packable for Sint32 {} impl TryFrom<Varint> for Sint32 { fn try_from(f: Varint) -> Result<Self> { let n = ((f.0 << 63) | (f.0 >> 1)) as i64; track_assert!( n >= std::i32::MIN as i64, ErrorKind::Invalid, "Tool small `int32` value: {}", n ); track_assert!( n <= std::i32::MAX as i64, ErrorKind::Invalid, "Tool large `int32` value: {}", n ); Ok(Sint32(n as i32)) } } impl MapKey for Sint32 {} #[derive(Debug, Default)] pub struct Sint64(pub i64); impl_scalar_type!(Sint64, i64, Varint); impl Packable for Sint64 {} impl From<Varint> for Sint64 { fn from(f: Varint) -> Self { let n = ((f.0 << 63) | (f.0 >> 1)) as i64; Sint64(n) } } impl MapKey for Sint64 {} #[derive(Debug, Default)] pub struct Fixed32(pub u32); impl_scalar_type!(Fixed32, u32, Bit32); impl Packable for Fixed32 {} impl From<Bit32> for Fixed32 { fn from(f: Bit32) -> Self { Fixed32(LittleEndian::read_u32(&f.0[..])) } } impl MapKey for Fixed32 {} #[derive(Debug, Default)] pub struct Fixed64(pub u64); impl_scalar_type!(Fixed64, u64, Bit64); impl Packable for Fixed64 {} impl From<Bit64> for Fixed64 { fn from(f: Bit64) -> Self { Fixed64(LittleEndian::read_u64(&f.0[..])) } } impl MapKey for Fixed64 {} #[derive(Debug, Default)] pub struct Sfixed32(pub i32); impl_scalar_type!(Sfixed32, i32, Bit32); impl Packable for Sfixed32 {} impl From<Bit32> for Sfixed32 { fn from(f: Bit32) -> Self { Sfixed32(LittleEndian::read_i32(&f.0[..])) } } impl MapKey for Sfixed32 {} #[derive(Debug, Default)] pub struct Sfixed64(pub i64); impl_scalar_type!(Sfixed64, i64, Bit64); impl Packable for Sfixed64 {} impl From<Bit64> for Sfixed64 { fn from(f: Bit64) -> Self { Sfixed64(LittleEndian::read_i64(&f.0[..])) } } impl MapKey for Sfixed64 {} #[derive(Debug, Default)] pub struct Float(pub f32); impl_scalar_type!(Float, f32, Bit32); impl Packable for Float {} impl From<Bit32> for Float { fn from(f: Bit32) -> Self { Float(LittleEndian::read_f32(&f.0[..])) } } #[derive(Debug, Default)] pub struct Double(pub f64); impl_scalar_type!(Double, f64, Bit64); impl Packable for Double {} impl From<Bit64> for Double { fn from(f: Bit64) -> Self { Double(LittleEndian::read_f64(&f.0[..])) } } #[derive(Debug, Default)] pub struct Bytes(pub Vec<u8>); impl_scalar_type!(Bytes, Vec<u8>, LengthDelimited); impl From<LengthDelimited<Vec<u8>>> for Bytes { fn from(f: LengthDelimited<Vec<u8>>) -> Self { Bytes(f.0) } } #[derive(Debug, Default)] pub struct Str(pub String); impl_scalar_type!(Str, String, LengthDelimited); impl TryFrom<Bytes> for Str { fn try_from(f: Bytes) -> Result<Str> { String::from_utf8(f.0).map(Str).map_err(|e| { ErrorKind::Invalid.cause(e) }) } } impl MapKey for Str {} #[derive(Debug, Default)] pub struct Embedded<T: Message>(pub(crate) T::Base); impl<T: Message> Embedded<T> { pub fn new(message: T) -> Self { Embedded(message.into_base()) } pub fn unwrap(self) -> Result<T> { T::from_base(self.0) } } impl<T: Message> FieldType for Embedded<T> { fn wire_type() -> WireType { WireType::LengthDelimited } } impl<T: Message> From<T> for Embedded<T> { fn from(f: T) -> Self { Embedded(f.into_base()) } }