sqlx_postgres/types/
uuid.rs

1use uuid::Uuid;
2
3use crate::decode::Decode;
4use crate::encode::{Encode, IsNull};
5use crate::error::BoxDynError;
6use crate::types::Type;
7use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
8
9impl Type<Postgres> for Uuid {
10    fn type_info() -> PgTypeInfo {
11        PgTypeInfo::UUID
12    }
13}
14
15impl PgHasArrayType for Uuid {
16    fn array_type_info() -> PgTypeInfo {
17        PgTypeInfo::UUID_ARRAY
18    }
19}
20
21impl Encode<'_, Postgres> for Uuid {
22    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
23        buf.extend_from_slice(self.as_bytes());
24
25        Ok(IsNull::No)
26    }
27}
28
29impl Decode<'_, Postgres> for Uuid {
30    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
31        match value.format() {
32            PgValueFormat::Binary => Uuid::from_slice(value.as_bytes()?),
33            PgValueFormat::Text => value.as_str()?.parse(),
34        }
35        .map_err(Into::into)
36    }
37}