try to decode julids as strings in case bytes fails
This commit is contained in:
parent
f2ade6d85e
commit
6a9e43feee
1 changed files with 13 additions and 4 deletions
15
src/sqlx.rs
15
src/sqlx.rs
|
@ -3,7 +3,7 @@ use std::borrow::Cow;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
encode::IsNull,
|
encode::IsNull,
|
||||||
sqlite::{SqliteArgumentValue, SqliteValueRef},
|
sqlite::{SqliteArgumentValue, SqliteValueRef},
|
||||||
Decode, Encode, Sqlite,
|
Decode, Encode, Sqlite, Value, ValueRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::Julid;
|
use crate::Julid;
|
||||||
|
@ -25,8 +25,17 @@ impl<'q> Encode<'q, Sqlite> for Julid {
|
||||||
|
|
||||||
impl Decode<'_, Sqlite> for Julid {
|
impl Decode<'_, Sqlite> for Julid {
|
||||||
fn decode(value: SqliteValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
|
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();
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue