postgres_types_extra/
pg_line.rs1use bytes::{Buf, BufMut};
2use postgres_types::{FromSql, IsNull, ToSql, Type, accepts, to_sql_checked};
3use std::{error::Error, fmt};
4
5#[derive(Debug, Clone)]
6pub struct PgLine {
7 pub a: f64,
8 pub b: f64,
9 pub c: f64,
10}
11
12impl fmt::Display for PgLine {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 write!(f, "{{{},{},{}}}", self.a, self.b, self.c)
15 }
16}
17
18impl FromSql<'_> for PgLine {
19 fn from_sql(ty: &Type, mut raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
20 if ty.name() != "line" {
21 return Err("Unexpected type".into());
22 }
23 let a = raw.get_f64();
24 let b = raw.get_f64();
25 let c = raw.get_f64();
26 Ok(PgLine { a, b, c })
27 }
28
29 accepts!(LINE);
30}
31
32impl ToSql for PgLine {
33 fn to_sql(
34 &self,
35 ty: &Type,
36 out: &mut bytes::BytesMut,
37 ) -> Result<postgres_types::IsNull, Box<dyn Error + Sync + Send>>
38 where
39 Self: Sized,
40 {
41 if ty.name() != "line" {
42 return Err("Unexpected type".into());
43 }
44
45 out.put_f64(self.a);
46 out.put_f64(self.b);
47 out.put_f64(self.c);
48
49 Ok(IsNull::No)
50 }
51
52 accepts!(LINE);
53
54 to_sql_checked!();
55}