rbdc_pg/types/
uuid.rs

1use crate::arguments::PgArgumentBuffer;
2use crate::types::decode::Decode;
3use crate::types::encode::{Encode, IsNull};
4use crate::value::{PgValue, PgValueFormat};
5use rbdc::uuid::Uuid;
6use rbdc::Error;
7use std::str::FromStr;
8
9impl Encode for Uuid {
10    fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
11        let uuid = uuid::Uuid::from_str(&self.0).map_err(|e| Error::from(e.to_string()))?;
12        buf.extend_from_slice(uuid.as_bytes());
13        Ok(IsNull::No)
14    }
15}
16
17impl Decode for Uuid {
18    fn decode(value: PgValue) -> Result<Self, Error> {
19        Ok(Self(match value.format() {
20            PgValueFormat::Binary => uuid::Uuid::from_slice(value.as_bytes()?)
21                .map_err(|e| Error::from(format!("Decode Uuid:{}", e)))?
22                .to_string(),
23            PgValueFormat::Text => value
24                .as_str()?
25                .parse()
26                .map_err(|e| Error::from(format!("Decode Uuid str:{}", e)))?,
27        }))
28    }
29}