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
use crate::Money;

use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, BytesMut};
use postgres_types::{FromSql, IsNull, ToSql, Type};
use std::error::Error;

impl<'a> FromSql<'a> for Money {
    fn from_sql(_: &Type, mut buf: &[u8]) -> Result<Money, Box<dyn Error + Sync + Send>> {
        let v = buf.read_i64::<BigEndian>()?;
        if !buf.is_empty() {
            return Err("invalid buffer size".into());
        }
        Ok(Money::from(v))
    }

    postgres_types::accepts!(MONEY);
}

impl ToSql for Money {
    fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
        w.put_i64(self.inner());
        Ok(IsNull::No)
    }

    postgres_types::accepts!(MONEY);
    postgres_types::to_sql_checked!();
}