1use crate::arguments::PgArgumentBuffer;
2use crate::types::decode::Decode;
3use crate::types::encode::{Encode, IsNull};
4use crate::value::{PgValueFormat, PgValueRef};
5use byteorder::{BigEndian, ByteOrder};
6use rbdc::Error;
7
8impl Decode for f64 {
9 fn decode(value: PgValueRef) -> Result<Self, Error> {
10 Ok(match value.format() {
11 PgValueFormat::Binary => BigEndian::read_f64(value.as_bytes()?),
12 PgValueFormat::Text => value.as_str()?.parse()?,
13 })
14 }
15}
16
17impl Decode for f32 {
18 fn decode(value: PgValueRef) -> Result<Self, Error> {
19 Ok(match value.format() {
20 PgValueFormat::Binary => {
21 let bytes = value.as_bytes()?;
22 if bytes.len() == 8 {
23 BigEndian::read_f64(bytes) as f32
24 } else if bytes.len() == 4 {
25 BigEndian::read_f32(bytes)
26 } else {
27 return Err(Error::from("error f32 bytes len"));
28 }
29 }
30 PgValueFormat::Text => value.as_str()?.parse()?,
31 })
32 }
33}
34
35impl Encode for f64 {
36 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
37 buf.extend(&self.to_be_bytes());
38
39 Ok(IsNull::No)
40 }
41}
42
43impl Encode for f32 {
44 fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
45 buf.extend(&self.to_be_bytes());
46
47 Ok(IsNull::No)
48 }
49}