2022-12-14 05:13:23 +03:00
|
|
|
//! sea-orm types don't always serialize how we want. this helps that, though it won't help every case.
|
|
|
|
use ethers::prelude::Address;
|
|
|
|
use sea_orm::prelude::Uuid;
|
2023-07-12 10:35:07 +03:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2022-12-14 05:13:23 +03:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use ulid::Ulid;
|
|
|
|
|
|
|
|
pub fn to_fixed_length<T, const N: usize>(v: Vec<T>) -> [T; N] {
|
|
|
|
v.try_into()
|
|
|
|
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vec_as_address<S>(x: &[u8], s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let x = Address::from_slice(x);
|
|
|
|
|
|
|
|
x.serialize(s)
|
|
|
|
}
|
|
|
|
|
2023-07-12 10:35:07 +03:00
|
|
|
pub fn address_to_vec<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
|
|
|
|
let address = Address::deserialize(deserializer)?;
|
|
|
|
|
|
|
|
Ok(address.to_fixed_bytes().into())
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:13:23 +03:00
|
|
|
pub fn uuid_as_ulid<S>(x: &Uuid, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let x = Ulid::from(x.as_u128());
|
|
|
|
|
|
|
|
// TODO: to_string shouldn't be needed, but i'm still seeing Uuid length
|
|
|
|
x.to_string().serialize(s)
|
|
|
|
}
|
2023-07-12 10:35:07 +03:00
|
|
|
|
|
|
|
pub fn ulid_to_uuid<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Uuid, D::Error> {
|
|
|
|
let ulid = Ulid::deserialize(deserializer)?;
|
|
|
|
|
|
|
|
Ok(ulid.into())
|
|
|
|
}
|