| 1 | use crate::decode::Decode; |
| 2 | use crate::encode::{Encode, IsNull}; |
| 3 | use crate::error::BoxDynError; |
| 4 | use crate::types::array_compatible; |
| 5 | use crate::types::Type; |
| 6 | use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres}; |
| 7 | use std::borrow::Cow; |
| 8 | |
| 9 | impl Type<Postgres> for str { |
| 10 | fn type_info() -> PgTypeInfo { |
| 11 | PgTypeInfo::TEXT |
| 12 | } |
| 13 | |
| 14 | fn compatible(ty: &PgTypeInfo) -> bool { |
| 15 | [ |
| 16 | PgTypeInfo::TEXT, |
| 17 | PgTypeInfo::NAME, |
| 18 | PgTypeInfo::BPCHAR, |
| 19 | PgTypeInfo::VARCHAR, |
| 20 | PgTypeInfo::UNKNOWN, |
| 21 | PgTypeInfo::with_name("citext"), |
| 22 | ] |
| 23 | .contains(ty) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl Type<Postgres> for Cow<'_, str> { |
| 28 | fn type_info() -> PgTypeInfo { |
| 29 | <&str as Type<Postgres>>::type_info() |
| 30 | } |
| 31 | |
| 32 | fn compatible(ty: &PgTypeInfo) -> bool { |
| 33 | <&str as Type<Postgres>>::compatible(ty) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Type<Postgres> for Box<str> { |
| 38 | fn type_info() -> PgTypeInfo { |
| 39 | <&str as Type<Postgres>>::type_info() |
| 40 | } |
| 41 | |
| 42 | fn compatible(ty: &PgTypeInfo) -> bool { |
| 43 | <&str as Type<Postgres>>::compatible(ty) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Type<Postgres> for String { |
| 48 | fn type_info() -> PgTypeInfo { |
| 49 | <&str as Type<Postgres>>::type_info() |
| 50 | } |
| 51 | |
| 52 | fn compatible(ty: &PgTypeInfo) -> bool { |
| 53 | <&str as Type<Postgres>>::compatible(ty) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl PgHasArrayType for &'_ str { |
| 58 | fn array_type_info() -> PgTypeInfo { |
| 59 | PgTypeInfo::TEXT_ARRAY |
| 60 | } |
| 61 | |
| 62 | fn array_compatible(ty: &PgTypeInfo) -> bool { |
| 63 | array_compatible::<&str>(ty) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl PgHasArrayType for Cow<'_, str> { |
| 68 | fn array_type_info() -> PgTypeInfo { |
| 69 | <&str as PgHasArrayType>::array_type_info() |
| 70 | } |
| 71 | |
| 72 | fn array_compatible(ty: &PgTypeInfo) -> bool { |
| 73 | <&str as PgHasArrayType>::array_compatible(ty) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl PgHasArrayType for Box<str> { |
| 78 | fn array_type_info() -> PgTypeInfo { |
| 79 | <&str as PgHasArrayType>::array_type_info() |
| 80 | } |
| 81 | |
| 82 | fn array_compatible(ty: &PgTypeInfo) -> bool { |
| 83 | <&str as PgHasArrayType>::array_compatible(ty) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl PgHasArrayType for String { |
| 88 | fn array_type_info() -> PgTypeInfo { |
| 89 | <&str as PgHasArrayType>::array_type_info() |
| 90 | } |
| 91 | |
| 92 | fn array_compatible(ty: &PgTypeInfo) -> bool { |
| 93 | <&str as PgHasArrayType>::array_compatible(ty) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Encode<'_, Postgres> for &'_ str { |
| 98 | fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 99 | buf.extend(self.as_bytes()); |
| 100 | |
| 101 | Ok(IsNull::No) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Encode<'_, Postgres> for Cow<'_, str> { |
| 106 | fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 107 | match self { |
| 108 | Cow::Borrowed(str) => <&str as Encode<Postgres>>::encode(*str, buf), |
| 109 | Cow::Owned(str) => <&str as Encode<Postgres>>::encode(&**str, buf), |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl Encode<'_, Postgres> for Box<str> { |
| 115 | fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 116 | <&str as Encode<Postgres>>::encode(&**self, buf) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl Encode<'_, Postgres> for String { |
| 121 | fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 122 | <&str as Encode<Postgres>>::encode(&**self, buf) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl<'r> Decode<'r, Postgres> for &'r str { |
| 127 | fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> { |
| 128 | value.as_str() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl<'r> Decode<'r, Postgres> for Cow<'r, str> { |
| 133 | fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> { |
| 134 | Ok(Cow::Borrowed(value.as_str()?)) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl<'r> Decode<'r, Postgres> for Box<str> { |
| 139 | fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> { |
| 140 | Ok(Box::from(value.as_str()?)) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl Decode<'_, Postgres> for String { |
| 145 | fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> { |
| 146 | Ok(value.as_str()?.to_owned()) |
| 147 | } |
| 148 | } |