pbcodec/
types.rs

1use std;
2use byteorder::{ByteOrder, LittleEndian};
3use trackable::error::ErrorKindExt;
4
5use {ErrorKind, Result};
6use traits::{FieldType, MapKey, Message, Packable, TryFrom};
7use wire::WireType;
8use wire::types::{Bit32, Bit64, LengthDelimited, Varint};
9
10macro_rules! impl_scalar_type {
11    ($t:ident, $base:ty, $wire:ident) => {
12        impl FieldType for $t {
13            fn wire_type() -> WireType {
14                WireType::$wire
15            }
16        }
17        impl From<$base> for $t {
18            fn from(f: $base) -> Self {
19                $t(f)
20            }
21        }
22        impl From<$t> for $base {
23            fn from(f: $t) -> Self {
24                f.0
25            }
26        }
27    }
28}
29
30#[derive(Debug, Default)]
31pub struct Bool(pub bool);
32impl_scalar_type!(Bool, bool, Varint);
33impl Packable for Bool {}
34impl TryFrom<Varint> for Bool {
35    fn try_from(f: Varint) -> Result<Self> {
36        track_assert!(
37            f.0 <= 1,
38            ErrorKind::Invalid,
39            "Tool large `bool` value: {}",
40            f.0
41        );
42        Ok(Bool(f.0 != 0))
43    }
44}
45impl MapKey for Bool {}
46
47#[derive(Debug, Default)]
48pub struct Int32(pub i32);
49impl_scalar_type!(Int32, i32, Varint);
50impl Packable for Int32 {}
51impl TryFrom<Varint> for Int32 {
52    fn try_from(f: Varint) -> Result<Self> {
53        let n = f.0 as i64;
54        track_assert!(
55            n >= i64::from(std::i32::MIN),
56            ErrorKind::Invalid,
57            "Tool small `int32` value: {}",
58            n
59        );
60        track_assert!(
61            n <= i64::from(std::i32::MAX),
62            ErrorKind::Invalid,
63            "Tool large `int32` value: {}",
64            n
65        );
66        Ok(Int32(f.0 as i32))
67    }
68}
69impl MapKey for Int32 {}
70
71#[derive(Debug, Default)]
72pub struct Int64(pub i64);
73impl_scalar_type!(Int64, i64, Varint);
74impl Packable for Int64 {}
75impl From<Varint> for Int64 {
76    fn from(f: Varint) -> Self {
77        Int64(f.0 as i64)
78    }
79}
80impl MapKey for Int64 {}
81
82#[derive(Debug, Default)]
83pub struct Uint32(pub u32);
84impl_scalar_type!(Uint32, u32, Varint);
85impl Packable for Uint32 {}
86impl TryFrom<Varint> for Uint32 {
87    fn try_from(f: Varint) -> Result<Self> {
88        track_assert!(
89            f.0 <= u64::from(std::u32::MAX),
90            ErrorKind::Invalid,
91            "Tool large `uint32` value: {}",
92            f.0
93        );
94        Ok(Uint32(f.0 as u32))
95    }
96}
97impl MapKey for Uint32 {}
98
99#[derive(Debug, Default)]
100pub struct Uint64(pub u64);
101impl_scalar_type!(Uint64, u64, Varint);
102impl Packable for Uint64 {}
103impl From<Varint> for Uint64 {
104    fn from(f: Varint) -> Self {
105        Uint64(f.0)
106    }
107}
108impl MapKey for Uint64 {}
109
110#[derive(Debug, Default)]
111pub struct Sint32(pub i32);
112impl_scalar_type!(Sint32, i32, Varint);
113impl Packable for Sint32 {}
114impl TryFrom<Varint> for Sint32 {
115    fn try_from(f: Varint) -> Result<Self> {
116        let n = ((f.0 << 63) | (f.0 >> 1)) as i64;
117        track_assert!(
118            n >= i64::from(std::i32::MIN),
119            ErrorKind::Invalid,
120            "Tool small `int32` value: {}",
121            n
122        );
123        track_assert!(
124            n <= i64::from(std::i32::MAX),
125            ErrorKind::Invalid,
126            "Tool large `int32` value: {}",
127            n
128        );
129        Ok(Sint32(n as i32))
130    }
131}
132impl MapKey for Sint32 {}
133
134#[derive(Debug, Default)]
135pub struct Sint64(pub i64);
136impl_scalar_type!(Sint64, i64, Varint);
137impl Packable for Sint64 {}
138impl From<Varint> for Sint64 {
139    fn from(f: Varint) -> Self {
140        let n = ((f.0 << 63) | (f.0 >> 1)) as i64;
141        Sint64(n)
142    }
143}
144impl MapKey for Sint64 {}
145
146#[derive(Debug, Default)]
147pub struct Fixed32(pub u32);
148impl_scalar_type!(Fixed32, u32, Bit32);
149impl Packable for Fixed32 {}
150impl From<Bit32> for Fixed32 {
151    fn from(f: Bit32) -> Self {
152        Fixed32(LittleEndian::read_u32(&f.0[..]))
153    }
154}
155impl MapKey for Fixed32 {}
156
157#[derive(Debug, Default)]
158pub struct Fixed64(pub u64);
159impl_scalar_type!(Fixed64, u64, Bit64);
160impl Packable for Fixed64 {}
161impl From<Bit64> for Fixed64 {
162    fn from(f: Bit64) -> Self {
163        Fixed64(LittleEndian::read_u64(&f.0[..]))
164    }
165}
166impl MapKey for Fixed64 {}
167
168#[derive(Debug, Default)]
169pub struct Sfixed32(pub i32);
170impl_scalar_type!(Sfixed32, i32, Bit32);
171impl Packable for Sfixed32 {}
172impl From<Bit32> for Sfixed32 {
173    fn from(f: Bit32) -> Self {
174        Sfixed32(LittleEndian::read_i32(&f.0[..]))
175    }
176}
177impl MapKey for Sfixed32 {}
178
179#[derive(Debug, Default)]
180pub struct Sfixed64(pub i64);
181impl_scalar_type!(Sfixed64, i64, Bit64);
182impl Packable for Sfixed64 {}
183impl From<Bit64> for Sfixed64 {
184    fn from(f: Bit64) -> Self {
185        Sfixed64(LittleEndian::read_i64(&f.0[..]))
186    }
187}
188impl MapKey for Sfixed64 {}
189
190#[derive(Debug, Default)]
191pub struct Float(pub f32);
192impl_scalar_type!(Float, f32, Bit32);
193impl Packable for Float {}
194impl From<Bit32> for Float {
195    fn from(f: Bit32) -> Self {
196        Float(LittleEndian::read_f32(&f.0[..]))
197    }
198}
199
200#[derive(Debug, Default)]
201pub struct Double(pub f64);
202impl_scalar_type!(Double, f64, Bit64);
203impl Packable for Double {}
204impl From<Bit64> for Double {
205    fn from(f: Bit64) -> Self {
206        Double(LittleEndian::read_f64(&f.0[..]))
207    }
208}
209
210#[derive(Debug, Default)]
211pub struct Bytes(pub Vec<u8>);
212impl_scalar_type!(Bytes, Vec<u8>, LengthDelimited);
213impl From<LengthDelimited<Vec<u8>>> for Bytes {
214    fn from(f: LengthDelimited<Vec<u8>>) -> Self {
215        Bytes(f.0)
216    }
217}
218
219#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
220pub struct Str(pub String);
221impl_scalar_type!(Str, String, LengthDelimited);
222impl TryFrom<Bytes> for Str {
223    fn try_from(f: Bytes) -> Result<Str> {
224        String::from_utf8(f.0)
225            .map(Str)
226            .map_err(|e| ErrorKind::Invalid.cause(e))
227    }
228}
229impl MapKey for Str {}
230
231#[derive(Debug, Default)]
232pub struct Embedded<T: Message>(pub(crate) T::Base);
233impl<T: Message> Embedded<T> {
234    pub fn new(message: T) -> Self {
235        Embedded(message.into_base())
236    }
237    pub fn unwrap(self) -> Result<T> {
238        T::from_base(self.0)
239    }
240}
241impl<T: Message> FieldType for Embedded<T> {
242    fn wire_type() -> WireType {
243        WireType::LengthDelimited
244    }
245}
246impl<T: Message> From<T> for Embedded<T> {
247    fn from(f: T) -> Self {
248        Embedded(f.into_base())
249    }
250}