| 1 | use uuid::Uuid; |
| 2 | |
| 3 | use crate::decode::Decode; |
| 4 | use crate::encode::{Encode, IsNull}; |
| 5 | use crate::error::BoxDynError; |
| 6 | use crate::types::Type; |
| 7 | use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; |
| 8 | |
| 9 | impl Type<Postgres> for Uuid { |
| 10 | fn type_info() -> PgTypeInfo { |
| 11 | PgTypeInfo::UUID |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | impl PgHasArrayType for Uuid { |
| 16 | fn array_type_info() -> PgTypeInfo { |
| 17 | PgTypeInfo::UUID_ARRAY |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl 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 | |
| 29 | impl 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 | } |