use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::array_compatible; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres}; use std::borrow::Cow; impl Type for str { fn type_info() -> PgTypeInfo { PgTypeInfo::TEXT } fn compatible(ty: &PgTypeInfo) -> bool { [ PgTypeInfo::TEXT, PgTypeInfo::NAME, PgTypeInfo::BPCHAR, PgTypeInfo::VARCHAR, PgTypeInfo::UNKNOWN, PgTypeInfo::with_name("citext"), ] .contains(ty) } } impl Type for Cow<'_, str> { fn type_info() -> PgTypeInfo { <&str as Type>::type_info() } fn compatible(ty: &PgTypeInfo) -> bool { <&str as Type>::compatible(ty) } } impl Type for Box { fn type_info() -> PgTypeInfo { <&str as Type>::type_info() } fn compatible(ty: &PgTypeInfo) -> bool { <&str as Type>::compatible(ty) } } impl Type for String { fn type_info() -> PgTypeInfo { <&str as Type>::type_info() } fn compatible(ty: &PgTypeInfo) -> bool { <&str as Type>::compatible(ty) } } impl PgHasArrayType for &'_ str { fn array_type_info() -> PgTypeInfo { PgTypeInfo::TEXT_ARRAY } fn array_compatible(ty: &PgTypeInfo) -> bool { array_compatible::<&str>(ty) } } impl PgHasArrayType for Cow<'_, str> { fn array_type_info() -> PgTypeInfo { <&str as PgHasArrayType>::array_type_info() } fn array_compatible(ty: &PgTypeInfo) -> bool { <&str as PgHasArrayType>::array_compatible(ty) } } impl PgHasArrayType for Box { fn array_type_info() -> PgTypeInfo { <&str as PgHasArrayType>::array_type_info() } fn array_compatible(ty: &PgTypeInfo) -> bool { <&str as PgHasArrayType>::array_compatible(ty) } } impl PgHasArrayType for String { fn array_type_info() -> PgTypeInfo { <&str as PgHasArrayType>::array_type_info() } fn array_compatible(ty: &PgTypeInfo) -> bool { <&str as PgHasArrayType>::array_compatible(ty) } } impl Encode<'_, Postgres> for &'_ str { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { buf.extend(self.as_bytes()); Ok(IsNull::No) } } impl Encode<'_, Postgres> for Cow<'_, str> { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { match self { Cow::Borrowed(str) => <&str as Encode>::encode(*str, buf), Cow::Owned(str) => <&str as Encode>::encode(&**str, buf), } } } impl Encode<'_, Postgres> for Box { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { <&str as Encode>::encode(&**self, buf) } } impl Encode<'_, Postgres> for String { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { <&str as Encode>::encode(&**self, buf) } } impl<'r> Decode<'r, Postgres> for &'r str { fn decode(value: PgValueRef<'r>) -> Result { value.as_str() } } impl<'r> Decode<'r, Postgres> for Cow<'r, str> { fn decode(value: PgValueRef<'r>) -> Result { Ok(Cow::Borrowed(value.as_str()?)) } } impl<'r> Decode<'r, Postgres> for Box { fn decode(value: PgValueRef<'r>) -> Result { Ok(Box::from(value.as_str()?)) } } impl Decode<'_, Postgres> for String { fn decode(value: PgValueRef<'_>) -> Result { Ok(value.as_str()?.to_owned()) } }