1use crate::arguments::PgArgumentBuffer;
2use crate::type_info::PgTypeInfo;
3use crate::types::decode::Decode;
4use crate::types::encode::{Encode, IsNull};
5use crate::types::TypeInfo;
6use crate::value::{PgValue, PgValueFormat};
7use byteorder::{BigEndian, ByteOrder};
8use rbdc::Error;
9
10impl Decode for u64 {
11 fn decode(value: PgValue) -> Result<Self, Error> {
12 Ok(match value.format() {
13 PgValueFormat::Binary => BigEndian::read_u64(value.as_bytes()?),
14 PgValueFormat::Text => value.as_str()?.parse()?,
15 })
16 }
17}
18
19impl Decode for u32 {
20 fn decode(value: PgValue) -> Result<Self, Error> {
21 Ok(match value.format() {
22 PgValueFormat::Binary => {
23 let bytes = value.as_bytes()?;
24 if bytes.len() == 8 {
25 BigEndian::read_u64(bytes) as u32
26 } else if bytes.len() == 4 {
27 BigEndian::read_u32(bytes)
28 } else {
29 return Err(Error::from("error u32 bytes len"));
30 }
31 }
32 PgValueFormat::Text => value.as_str()?.parse()?,
33 })
34 }
35}
36
37impl Decode for u16 {
38 fn decode(value: PgValue) -> Result<Self, Error> {
39 Ok(match value.format() {
40 PgValueFormat::Binary => {
41 let bytes = value.as_bytes()?;
42 if bytes.len() == 8 {
43 BigEndian::read_u64(bytes) as u16
44 } else if bytes.len() == 4 {
45 BigEndian::read_u32(bytes) as u16
46 } else if bytes.len() == 2 {
47 BigEndian::read_u16(bytes)
48 } else {
49 return Err(Error::from("error u16 bytes len"));
50 }
51 }
52 PgValueFormat::Text => value.as_str()?.parse()?,
53 })
54 }
55}
56
57impl Decode for u8 {
58 fn decode(value: PgValue) -> Result<Self, Error> {
59 Ok(value.as_bytes()?.get(0).copied().unwrap_or_default() as u8)
61 }
62}
63
64impl Decode for i64 {
65 fn decode(value: PgValue) -> Result<Self, Error> {
66 Ok(match value.format() {
67 PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?),
68 PgValueFormat::Text => value.as_str()?.parse()?,
69 })
70 }
71}
72
73impl Decode for i32 {
74 fn decode(value: PgValue) -> Result<Self, Error> {
75 Ok(match value.format() {
76 PgValueFormat::Binary => {
77 let bytes = value.as_bytes()?;
78 if bytes.len() == 8 {
79 BigEndian::read_i64(bytes) as i32
80 } else if bytes.len() == 4 {
81 BigEndian::read_i32(bytes)
82 } else {
83 return Err(Error::from("error i32 bytes len"));
84 }
85 }
86 PgValueFormat::Text => value.as_str()?.parse()?,
87 })
88 }
89}
90
91impl Decode for i16 {
92 fn decode(value: PgValue) -> Result<Self, Error> {
93 Ok(match value.format() {
94 PgValueFormat::Binary => {
95 let bytes = value.as_bytes()?;
96 if bytes.len() == 8 {
97 BigEndian::read_i64(bytes) as i16
98 } else if bytes.len() == 4 {
99 BigEndian::read_i32(bytes) as i16
100 } else if bytes.len() == 2 {
101 BigEndian::read_i16(bytes)
102 } else {
103 return Err(Error::from("error i16 bytes len"));
104 }
105 }
106 PgValueFormat::Text => value.as_str()?.parse()?,
107 })
108 }
109}
110
111impl Decode for i8 {
112 fn decode(value: PgValue) -> Result<Self, Error> {
113 Ok(value.as_bytes()?.get(0).copied().unwrap_or_default() as i8)
115 }
116}
117
118impl Encode for u64 {
121 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
122 buf.extend(&self.to_be_bytes());
123 Ok(IsNull::No)
124 }
125}
126
127impl Encode for u32 {
128 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
129 buf.extend(&self.to_be_bytes());
130
131 Ok(IsNull::No)
132 }
133}
134
135impl Encode for u16 {
136 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
137 buf.extend(&self.to_be_bytes());
138
139 Ok(IsNull::No)
140 }
141}
142
143impl Encode for u8 {
144 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
145 buf.extend(&self.to_be_bytes());
146
147 Ok(IsNull::No)
148 }
149}
150
151impl Encode for i64 {
152 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
153 buf.extend(&self.to_be_bytes());
154
155 Ok(IsNull::No)
156 }
157}
158
159impl Encode for i32 {
160 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
161 buf.extend(&self.to_be_bytes());
162
163 Ok(IsNull::No)
164 }
165}
166
167impl Encode for i16 {
168 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
169 buf.extend(&self.to_be_bytes());
170
171 Ok(IsNull::No)
172 }
173}
174
175impl Encode for i8 {
176 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
177 buf.extend(&self.to_be_bytes());
178
179 Ok(IsNull::No)
180 }
181}
182
183impl TypeInfo for i8 {
186 fn type_info(&self) -> PgTypeInfo {
187 PgTypeInfo::BYTEA
188 }
189}
190
191#[cfg(test)]
192mod test {
193 use crate::type_info::PgTypeInfo;
194 use crate::types::decode::Decode;
195 use crate::value::{PgValue, PgValueFormat};
196
197 #[test]
198 fn test_decode_u32() {
199 let bytes: [u8; 4] = 3_u32.to_be_bytes();
200 let r: u32 = Decode::decode(PgValue {
201 value: Some(bytes.to_vec()),
202 type_info: PgTypeInfo::INT8,
203 format: PgValueFormat::Binary,
204 timezone_sec: None,
205 })
206 .unwrap();
207 assert_eq!(r, 3);
208 }
209
210 #[test]
211 fn test_decode_u32_by_u64() {
212 let bytes: [u8; 8] = 3_u64.to_be_bytes();
213 println!("bytes={:?}", bytes);
214 let r: u32 = Decode::decode(PgValue {
215 value: Some(bytes.to_vec()),
216 type_info: PgTypeInfo::INT8,
217 format: PgValueFormat::Binary,
218 timezone_sec: None,
219 })
220 .unwrap();
221 assert_eq!(r, 3);
222 }
223
224 #[test]
225 fn test_decode_u16_by_u64() {
226 let bytes: [u8; 8] = 3_u64.to_be_bytes();
227 println!("bytes={:?}", bytes);
228 let r: u16 = Decode::decode(PgValue {
229 value: Some(bytes.to_vec()),
230 type_info: PgTypeInfo::INT8,
231 format: PgValueFormat::Binary,
232 timezone_sec: None,
233 })
234 .unwrap();
235 assert_eq!(r, 3);
236 }
237
238 #[test]
239 fn test_decode_i32() {
240 let bytes: [u8; 4] = 3_i32.to_be_bytes();
241 let r: i32 = Decode::decode(PgValue {
242 value: Some(bytes.to_vec()),
243 type_info: PgTypeInfo::INT8,
244 format: PgValueFormat::Binary,
245 timezone_sec: None,
246 })
247 .unwrap();
248 assert_eq!(r, 3);
249 }
250
251 #[test]
252 fn test_decode_i32_by_i64() {
253 let bytes: [u8; 8] = 3_i64.to_be_bytes();
254 println!("bytes={:?}", bytes);
255 let r: i32 = Decode::decode(PgValue {
256 value: Some(bytes.to_vec()),
257 type_info: PgTypeInfo::INT8,
258 format: PgValueFormat::Binary,
259 timezone_sec: None,
260 })
261 .unwrap();
262 assert_eq!(r, 3);
263 }
264
265 #[test]
266 fn test_decode_i16_by_i64() {
267 let bytes: [u8; 8] = 3_i64.to_be_bytes();
268 println!("bytes={:?}", bytes);
269 let r: i16 = Decode::decode(PgValue {
270 value: Some(bytes.to_vec()),
271 type_info: PgTypeInfo::INT8,
272 format: PgValueFormat::Binary,
273 timezone_sec: None,
274 })
275 .unwrap();
276 assert_eq!(r, 3);
277 }
278}