use spg_embedded::Value as EngineValue;
#[cfg(feature = "uuid")]
use sqlx_core::decode::Decode;
#[cfg(feature = "uuid")]
use sqlx_core::error::BoxDynError;
#[cfg(feature = "uuid")]
use sqlx_core::types::Type;
#[cfg(feature = "uuid")]
use crate::database::Spg;
#[cfg(feature = "uuid")]
use crate::type_info::{Kind, SpgTypeInfo};
#[cfg(feature = "uuid")]
use crate::value::SpgValueRef;
pub(crate) fn try_uuid_as_string(value: &EngineValue) -> Option<String> {
match value {
EngineValue::Uuid(b) => Some(spg_storage::format_uuid(b)),
_ => None,
}
}
#[cfg(feature = "uuid")]
mod u {
use super::*;
use sqlx_core::encode::{Encode, IsNull};
use uuid::Uuid;
use crate::arguments::SpgArgumentValue;
impl Type<Spg> for Uuid {
fn type_info() -> SpgTypeInfo {
SpgTypeInfo::of(Kind::Uuid)
}
fn compatible(ty: &SpgTypeInfo) -> bool {
matches!(ty.kind(), Kind::Uuid)
}
}
impl<'q> Encode<'q, Spg> for Uuid {
fn encode_by_ref(
&self,
buf: &mut Vec<SpgArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
buf.push(SpgArgumentValue {
value: EngineValue::Uuid(*self.as_bytes()),
type_info: Some(SpgTypeInfo::of(Kind::Uuid)),
_phantom: core::marker::PhantomData,
});
Ok(IsNull::No)
}
}
impl<'r> Decode<'r, Spg> for Uuid {
fn decode(value: SpgValueRef<'r>) -> Result<Self, BoxDynError> {
match value.engine() {
EngineValue::Uuid(b) => Ok(Uuid::from_bytes(*b)),
EngineValue::Text(s) => spg_storage::parse_uuid_str(s)
.map(Uuid::from_bytes)
.ok_or_else(|| format!("cannot parse text {s:?} as uuid::Uuid").into()),
other => Err(format!("cannot decode {other:?} as uuid::Uuid / UUID").into()),
}
}
}
}