1use std::borrow::Cow;
2
3use crate::decode::Decode;
4use crate::encode::{Encode, IsNull};
5use crate::error::BoxDynError;
6use crate::type_info::DataType;
7use crate::types::Type;
8use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
9
10impl Type<Sqlite> for str {
11 fn type_info() -> SqliteTypeInfo {
12 SqliteTypeInfo(DataType::Text)
13 }
14}
15
16impl<'q> Encode<'q, Sqlite> for &'q str {
17 fn encode_by_ref(
18 &self,
19 args: &mut Vec<SqliteArgumentValue<'q>>,
20 ) -> Result<IsNull, BoxDynError> {
21 args.push(SqliteArgumentValue::Text(Cow::Borrowed(*self)));
22
23 Ok(IsNull::No)
24 }
25}
26
27impl<'r> Decode<'r, Sqlite> for &'r str {
28 fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
29 value.text()
30 }
31}
32
33impl Type<Sqlite> for Box<str> {
34 fn type_info() -> SqliteTypeInfo {
35 <&str as Type<Sqlite>>::type_info()
36 }
37}
38
39impl Encode<'_, Sqlite> for Box<str> {
40 fn encode(self, args: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
41 args.push(SqliteArgumentValue::Text(Cow::Owned(self.into_string())));
42
43 Ok(IsNull::No)
44 }
45
46 fn encode_by_ref(
47 &self,
48 args: &mut Vec<SqliteArgumentValue<'_>>,
49 ) -> Result<IsNull, BoxDynError> {
50 args.push(SqliteArgumentValue::Text(Cow::Owned(
51 self.clone().into_string(),
52 )));
53
54 Ok(IsNull::No)
55 }
56}
57
58impl Decode<'_, Sqlite> for Box<str> {
59 fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
60 value.text().map(Box::from)
61 }
62}
63
64impl Type<Sqlite> for String {
65 fn type_info() -> SqliteTypeInfo {
66 <&str as Type<Sqlite>>::type_info()
67 }
68}
69
70impl<'q> Encode<'q, Sqlite> for String {
71 fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
72 args.push(SqliteArgumentValue::Text(Cow::Owned(self)));
73
74 Ok(IsNull::No)
75 }
76
77 fn encode_by_ref(
78 &self,
79 args: &mut Vec<SqliteArgumentValue<'q>>,
80 ) -> Result<IsNull, BoxDynError> {
81 args.push(SqliteArgumentValue::Text(Cow::Owned(self.clone())));
82
83 Ok(IsNull::No)
84 }
85}
86
87impl<'r> Decode<'r, Sqlite> for String {
88 fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
89 value.text().map(ToOwned::to_owned)
90 }
91}
92
93impl Type<Sqlite> for Cow<'_, str> {
94 fn type_info() -> SqliteTypeInfo {
95 <&str as Type<Sqlite>>::type_info()
96 }
97
98 fn compatible(ty: &SqliteTypeInfo) -> bool {
99 <&str as Type<Sqlite>>::compatible(ty)
100 }
101}
102
103impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
104 fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
105 args.push(SqliteArgumentValue::Text(self));
106
107 Ok(IsNull::No)
108 }
109
110 fn encode_by_ref(
111 &self,
112 args: &mut Vec<SqliteArgumentValue<'q>>,
113 ) -> Result<IsNull, BoxDynError> {
114 args.push(SqliteArgumentValue::Text(self.clone()));
115
116 Ok(IsNull::No)
117 }
118}
119
120impl<'r> Decode<'r, Sqlite> for Cow<'r, str> {
121 fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
122 value.text().map(Cow::Borrowed)
123 }
124}