pub use alloy::primitives::Bytes;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::str::FromStr;
use crate::error::SqlTypeError;
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SqlBytes(pub(crate) Bytes);
impl SqlBytes {
pub fn new() -> Self {
SqlBytes(Bytes::new())
}
pub fn inner(&self) -> &Bytes {
&self.0
}
pub fn to_u256(&self) -> crate::SqlU256 {
crate::SqlU256::from(alloy::primitives::U256::from_be_slice(self.as_ref()))
}
}
impl AsRef<Bytes> for SqlBytes {
fn as_ref(&self) -> &Bytes {
&self.0
}
}
impl Deref for SqlBytes {
type Target = Bytes;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Bytes> for SqlBytes {
fn from(bytes: Bytes) -> Self {
SqlBytes(bytes)
}
}
impl From<SqlBytes> for Bytes {
fn from(sql_bytes: SqlBytes) -> Self {
sql_bytes.0
}
}
impl std::fmt::Display for SqlBytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.to_string().to_lowercase())
}
}
impl FromStr for SqlBytes {
type Err = SqlTypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Bytes::from_str(s)
.map(SqlBytes)
.map_err(|e| SqlTypeError::InvalidFormat(e.to_string()))
}
}
impl Default for SqlBytes {
fn default() -> Self {
SqlBytes::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
let sql_bytes = SqlBytes::from_str("0x1234").unwrap();
assert_eq!(format!("{}", sql_bytes), "0x1234");
}
#[cfg(feature = "serde")]
#[test]
fn test_serde() {
let input = "0xa9059cbb00000000000000000000000042066a09b94e41e0263eee935177c2ecfff7fe11000000000000000000000000000000000000000000000002629f66e0c5300000";
let val = SqlBytes::from_str(input).unwrap();
let json = serde_json::to_string(&val).unwrap();
let de: SqlBytes = serde_json::from_str(&json).unwrap();
assert_eq!(val, de);
}
}