try to decode julids as strings in case bytes fails

This commit is contained in:
Joe Ardent 2023-12-28 17:39:08 -08:00
parent f2ade6d85e
commit 6a9e43feee
1 changed files with 13 additions and 4 deletions

View File

@ -3,7 +3,7 @@ use std::borrow::Cow;
use sqlx::{
encode::IsNull,
sqlite::{SqliteArgumentValue, SqliteValueRef},
Decode, Encode, Sqlite,
Decode, Encode, Sqlite, Value, ValueRef,
};
use crate::Julid;
@ -25,8 +25,17 @@ impl<'q> Encode<'q, Sqlite> for Julid {
impl Decode<'_, Sqlite> for Julid {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
let bytes = <&[u8] as Decode<Sqlite>>::decode(value)?;
let julid = match <&[u8] as Decode<Sqlite>>::decode(value.to_owned().as_ref()) {
Ok(bytes) => {
let bytes: [u8; 16] = bytes.try_into().unwrap_or_default();
Ok(bytes.into())
Julid::from_bytes(bytes)
}
_ => {
let string = <&str as Decode<Sqlite>>::decode(value)?;
Julid::from_string(string)?
}
};
Ok(julid)
}
}