sqlx_postgres/types/
str.rs1use crate::decode::Decode;
2use crate::encode::{Encode, IsNull};
3use crate::error::BoxDynError;
4use crate::types::array_compatible;
5use crate::types::Type;
6use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres};
7use std::borrow::Cow;
8
9impl 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
27impl 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
37impl 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
47impl 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
57impl 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
67impl 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
77impl 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
87impl 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
97impl 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
105impl 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
114impl 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
120impl 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
126impl<'r> Decode<'r, Postgres> for &'r str {
127 fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
128 value.as_str()
129 }
130}
131
132impl<'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
138impl<'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
144impl Decode<'_, Postgres> for String {
145 fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
146 Ok(value.as_str()?.to_owned())
147 }
148}