use crate::{
codec::{Decode, Encode},
database::{
Typed,
client::postgres::{DecodeWrapper, EncodeWrapper, Postgres, Ty},
},
misc::from_utf8_basic,
};
use alloc::string::String;
impl<'exec, E> Decode<'exec, Postgres<E>> for &'exec [u8]
where
E: From<crate::Error>,
{
#[inline]
fn decode(dw: &mut DecodeWrapper<'exec, '_>) -> Result<Self, E> {
Ok(dw.bytes())
}
}
impl<E> Encode<Postgres<E>> for &[u8]
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, '_>) -> Result<(), E> {
ew.buffer().extend_from_slice(self)?;
Ok(())
}
}
impl<E> Typed<Postgres<E>> for &[u8]
where
E: From<crate::Error>,
{
#[inline]
fn runtime_ty(&self) -> Option<Ty> {
<Self as Typed<Postgres<E>>>::static_ty()
}
#[inline]
fn static_ty() -> Option<Ty> {
Some(Ty::ByteaArray)
}
}
test!(bytes, &[u8], &[1, 2, 3, 4]);
impl<E> Encode<Postgres<E>> for &mut [u8]
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, '_>) -> Result<(), E> {
ew.buffer().extend_from_slice(self)?;
Ok(())
}
}
impl<E> Typed<Postgres<E>> for &mut [u8]
where
E: From<crate::Error>,
{
#[inline]
fn runtime_ty(&self) -> Option<Ty> {
Some(Ty::ByteaArray)
}
#[inline]
fn static_ty() -> Option<Ty> {
Some(Ty::ByteaArray)
}
}
impl<E> Encode<Postgres<E>> for str
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, '_>) -> Result<(), E> {
ew.buffer().extend_from_slice(self.as_bytes())?;
Ok(())
}
}
impl<E> Typed<Postgres<E>> for str
where
E: From<crate::Error>,
{
#[inline]
fn runtime_ty(&self) -> Option<Ty> {
<&Self as Typed<Postgres<E>>>::static_ty()
}
}
impl<'exec, E> Decode<'exec, Postgres<E>> for &'exec str
where
E: From<crate::Error>,
{
#[inline]
fn decode(dw: &mut DecodeWrapper<'exec, '_>) -> Result<Self, E> {
Ok(from_utf8_basic(dw.bytes()).map_err(crate::Error::from)?)
}
}
impl<E> Encode<Postgres<E>> for &str
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, '_>) -> Result<(), E> {
ew.buffer().extend_from_slice(self.as_bytes())?;
Ok(())
}
}
impl<E> Typed<Postgres<E>> for &str
where
E: From<crate::Error>,
{
#[inline]
fn runtime_ty(&self) -> Option<Ty> {
<Self as Typed<Postgres<E>>>::static_ty()
}
#[inline]
fn static_ty() -> Option<Ty> {
Some(Ty::Text)
}
}
test!(str, &str, "1234");
impl<E> Decode<'_, Postgres<E>> for String
where
E: From<crate::Error>,
{
#[inline]
fn decode(dw: &mut DecodeWrapper<'_, '_>) -> Result<Self, E> {
match from_utf8_basic(dw.bytes()).map_err(crate::Error::from) {
Ok(elem) => Ok(elem.into()),
Err(err) => Err(err.into()),
}
}
}
impl<E> Encode<Postgres<E>> for String
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, '_>) -> Result<(), E> {
ew.buffer().extend_from_slice(self.as_bytes())?;
Ok(())
}
}
impl<E> Typed<Postgres<E>> for String
where
E: From<crate::Error>,
{
#[inline]
fn runtime_ty(&self) -> Option<Ty> {
<Self as Typed<Postgres<E>>>::static_ty()
}
#[inline]
fn static_ty() -> Option<Ty> {
Some(Ty::Text)
}
}
kani!(string, String);