Skip to main content

gin_tonic_core/
traits.rs

1use crate::{Tag, encoder::SizeHint, error::ProtoError};
2
3pub trait Scalar<ProtobufType> {
4    const WIRE_TYPE: u8;
5
6    fn size_hint(&self) -> usize {
7        let mut hint = SizeHint::default();
8        self.encode(&mut hint);
9        hint.size()
10    }
11
12    fn encode(&self, encoder: &mut impl Encode);
13    fn decode(decoder: &mut impl Decode) -> Result<Self, ProtoError>
14    where
15        Self: Sized;
16
17    /// helper to serialize type with field number
18    #[inline]
19    fn encode_field(&self, field_number: u32, encoder: &mut impl Encode) {
20        encoder.encode_tag(Tag::from_parts(field_number, Self::WIRE_TYPE));
21        self.encode(encoder);
22    }
23}
24
25pub trait Encode {
26    #[inline]
27    fn encode_tag(&mut self, tag: Tag) {
28        self.encode_uint32(tag.into());
29    }
30
31    #[inline]
32    fn encode_int32(&mut self, n: i32) {
33        self.encode_uint64(n as u64);
34    }
35    #[inline]
36    fn encode_int64(&mut self, n: i64) {
37        self.encode_uint64(n as u64);
38    }
39
40    fn encode_sint32(&mut self, n: i32);
41    fn encode_sint64(&mut self, n: i64);
42
43    fn encode_uint32(&mut self, n: u32);
44    fn encode_uint64(&mut self, n: u64);
45
46    fn encode_sfixed32(&mut self, n: i32);
47    fn encode_sfixed64(&mut self, n: i64);
48
49    fn encode_fixed32(&mut self, n: u32);
50    fn encode_fixed64(&mut self, n: u64);
51
52    fn encode_float(&mut self, n: f32);
53    fn encode_double(&mut self, n: f64);
54
55    #[inline]
56    fn encode_bool(&mut self, b: bool) {
57        self.encode_uint32(if b { 1 } else { 0 });
58    }
59
60    fn encode_bytes(&mut self, b: &[u8]);
61
62    #[inline]
63    fn encode_str(&mut self, s: &str) {
64        self.encode_bytes(s.as_bytes())
65    }
66    #[inline]
67    fn encode_string(&mut self, s: String) {
68        self.encode_bytes(s.as_bytes())
69    }
70}
71
72#[allow(clippy::len_without_is_empty)]
73pub trait Decode {
74    fn buffer(&self) -> &[u8];
75    fn len(&self) -> usize;
76    fn position(&self) -> usize;
77    fn advance(&mut self, size: usize);
78    fn eof(&self) -> bool;
79    fn sub_decoder(&mut self, size: usize) -> impl Decode;
80
81    #[inline]
82    fn decode_int32(&mut self) -> Result<i32, ProtoError> {
83        let v = self.decode_uint64()?;
84        #[allow(clippy::cast_possible_truncation)]
85        Ok(v as _)
86    }
87    #[inline]
88    fn decode_int64(&mut self) -> Result<i64, ProtoError> {
89        let v = self.decode_uint64()?;
90        Ok(v as _)
91    }
92
93    fn decode_sint32(&mut self) -> Result<i32, ProtoError>;
94    fn decode_sint64(&mut self) -> Result<i64, ProtoError>;
95
96    fn decode_uint32(&mut self) -> Result<u32, ProtoError>;
97    fn decode_uint64(&mut self) -> Result<u64, ProtoError>;
98
99    fn decode_sfixed32(&mut self) -> Result<i32, ProtoError>;
100    fn decode_sfixed64(&mut self) -> Result<i64, ProtoError>;
101
102    fn decode_fixed32(&mut self) -> Result<u32, ProtoError>;
103    fn decode_fixed64(&mut self) -> Result<u64, ProtoError>;
104
105    fn decode_float(&mut self) -> Result<f32, ProtoError>;
106    fn decode_double(&mut self) -> Result<f64, ProtoError>;
107
108    fn decode_bytes(&mut self) -> Result<Vec<u8>, ProtoError>;
109    fn decode_string(&mut self) -> Result<String, ProtoError>;
110
111    #[inline]
112    fn decode_bool(&mut self) -> Result<bool, ProtoError> {
113        Ok(self.decode_uint32()? != 0)
114    }
115
116    #[inline]
117    fn decode_tag(&mut self) -> Result<Tag, ProtoError> {
118        Ok(Tag::from(self.decode_uint32()?))
119    }
120}
121
122pub trait Message {
123    fn message_size_hint(&self) -> usize {
124        let mut hint = SizeHint::default();
125        self.encode_message(&mut hint);
126        hint.size()
127    }
128
129    fn encode_message(&self, encoder: &mut impl Encode);
130
131    fn decode_message(decoder: &mut impl Decode) -> Result<Self, ProtoError>
132    where
133        Self: Sized;
134}
135
136pub trait PackableMarker<ProtobufType> {}
137
138pub trait Packed<ProtobufType> {
139    type Rust;
140
141    fn size_hint(&self, field_number: u32) -> usize {
142        let mut hint = SizeHint::default();
143        self.encode(field_number, &mut hint);
144        hint.size()
145    }
146
147    fn encode(&self, field_number: u32, encoder: &mut impl Encode);
148
149    fn decode(decoder: &mut impl Decode, v: &mut Self) -> Result<(), ProtoError>
150    where
151        Self: Sized;
152}
153
154pub trait Unpacked<ProtobufType> {
155    type Rust;
156
157    fn size_hint(&self, tag: Tag) -> usize {
158        let mut hint = SizeHint::default();
159        self.encode(tag, &mut hint);
160        hint.size()
161    }
162
163    fn encode(&self, tag: Tag, encoder: &mut impl Encode);
164}
165
166pub trait Map<ProtobufKey, ProtobufValue> {
167    fn size_hint(&self, field_number: u32) -> usize {
168        let mut hint = SizeHint::default();
169        self.encode(field_number, &mut hint);
170        hint.size()
171    }
172
173    fn encode(&self, field_number: u32, encoder: &mut impl Encode);
174
175    fn decode(decoder: &mut impl Decode, m: &mut Self) -> Result<(), ProtoError>
176    where
177        Self: Sized;
178}