#![cfg_attr(docsrs, doc(cfg(feature = "sqlx")))]
use thiserror::Error;
use alloy::primitives::Uint;
use sqlx_core::{
database::Database,
decode::Decode,
encode::{Encode, IsNull},
error::BoxDynError,
types::Type,
};
#[derive(Error, Debug)]
pub enum DecodeError {
#[error("Address decode error: expected 20 bytes, got {0} bytes")]
AddressDecodeError(usize),
#[error("Uint decode error: {0}")]
UintDecodeError(String),
#[error("FixedBytes decode error: expected {expected} bytes, got {actual} bytes")]
FixedBytesDecodeError {
expected: usize,
actual: usize,
},
#[error("Bytes decode error: {0}")]
BytesDecodeError(String),
}
use crate::{SqlAddress, SqlBytes, SqlFixedBytes, SqlUint};
impl<DB: Database> Type<DB> for SqlAddress
where
Vec<u8>: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<Vec<u8> as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<u8> as Type<DB>>::compatible(ty)
}
}
impl<'a, DB: Database> Encode<'a, DB> for SqlAddress
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
self.0.to_vec().encode_by_ref(buf)
}
}
impl<'a, DB: Database> Decode<'a, DB> for SqlAddress
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
let bytes = Vec::<u8>::decode(value)?;
if bytes.len() != 20 {
return Err(DecodeError::AddressDecodeError(bytes.len()).into());
}
let mut arr = [0u8; 20];
arr.copy_from_slice(&bytes);
Ok(SqlAddress::new(arr))
}
}
fn uint_to_be_bytes_vec<const BITS: usize, const LIMBS: usize>(
value: &Uint<BITS, LIMBS>,
) -> Vec<u8> {
let bytes_len = BITS / 8;
let mut bytes = vec![0u8; bytes_len];
let limbs = value.as_limbs();
for i in 0..LIMBS {
if i * 8 >= bytes_len {
break;
}
let src_idx = LIMBS - 1 - i; let limb_bytes = limbs[src_idx].to_be_bytes();
let dst_start = i * 8;
let len = 8.min(bytes_len - dst_start);
bytes[dst_start..dst_start + len].copy_from_slice(&limb_bytes[8 - len..]);
}
bytes
}
impl<const BITS: usize, const LIMBS: usize, DB: Database> Type<DB> for SqlUint<BITS, LIMBS>
where
Vec<u8>: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<Vec<u8> as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<u8> as Type<DB>>::compatible(ty)
}
}
impl<'a, const BITS: usize, const LIMBS: usize, DB: Database> Encode<'a, DB>
for SqlUint<BITS, LIMBS>
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
uint_to_be_bytes_vec(&self.0).encode_by_ref(buf)
}
}
impl<'a, const BITS: usize, const LIMBS: usize, DB: Database> Decode<'a, DB>
for SqlUint<BITS, LIMBS>
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
let bytes = Vec::<u8>::decode(value)?;
let expected = BITS / 8;
if bytes.len() != expected {
return Err(DecodeError::UintDecodeError(format!(
"expected {expected} bytes, got {}",
bytes.len()
))
.into());
}
Ok(SqlUint(Uint::<BITS, LIMBS>::from_be_slice(&bytes)))
}
}
impl<const N: usize, DB: Database> Type<DB> for SqlFixedBytes<N>
where
Vec<u8>: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<Vec<u8> as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<u8> as Type<DB>>::compatible(ty)
}
}
impl<'a, const N: usize, DB: Database> Encode<'a, DB> for SqlFixedBytes<N>
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
self.0.to_vec().encode_by_ref(buf)
}
}
impl<'a, const N: usize, DB: Database> Decode<'a, DB> for SqlFixedBytes<N>
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
let bytes = Vec::<u8>::decode(value)?;
if bytes.len() != N {
return Err(DecodeError::FixedBytesDecodeError {
expected: N,
actual: bytes.len(),
}
.into());
}
let mut arr = [0u8; N];
arr.copy_from_slice(&bytes);
Ok(SqlFixedBytes::new(arr))
}
}
impl<DB: Database> Type<DB> for SqlBytes
where
Vec<u8>: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<Vec<u8> as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<Vec<u8> as Type<DB>>::compatible(ty)
}
}
impl<'a, DB: Database> Encode<'a, DB> for SqlBytes
where
Vec<u8>: Encode<'a, DB>,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'a>,
) -> Result<IsNull, BoxDynError> {
self.0.to_vec().encode_by_ref(buf)
}
}
impl<'a, DB: Database> Decode<'a, DB> for SqlBytes
where
Vec<u8>: Decode<'a, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'a>) -> Result<Self, BoxDynError> {
let bytes = Vec::<u8>::decode(value)?;
Ok(SqlBytes(alloy::primitives::Bytes::from(bytes)))
}
}