cdbc_sqlite/types/
str.rs

1use std::borrow::Cow;
2
3use cdbc::decode::Decode;
4use cdbc::encode::{Encode, IsNull};
5use cdbc::error::BoxDynError;
6use crate::type_info::DataType;
7use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
8use cdbc::types::Type;
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(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
18        args.push(SqliteArgumentValue::Text(Cow::Borrowed(*self)));
19
20        IsNull::No
21    }
22}
23
24impl<'r> Decode<'r, Sqlite> for &'r str {
25    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
26        value.text()
27    }
28}
29
30impl Type<Sqlite> for String {
31    fn type_info() -> SqliteTypeInfo {
32        <&str as Type<Sqlite>>::type_info()
33    }
34}
35
36impl<'q> Encode<'q, Sqlite> for String {
37    fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
38        args.push(SqliteArgumentValue::Text(Cow::Owned(self)));
39
40        IsNull::No
41    }
42
43    fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
44        args.push(SqliteArgumentValue::Text(Cow::Owned(self.clone())));
45
46        IsNull::No
47    }
48}
49
50impl<'r> Decode<'r, Sqlite> for String {
51    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
52        value.text().map(ToOwned::to_owned)
53    }
54}
55
56impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
57    fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
58        args.push(SqliteArgumentValue::Text(self));
59
60        IsNull::No
61    }
62
63    fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
64        args.push(SqliteArgumentValue::Text(self.clone()));
65
66        IsNull::No
67    }
68}
69
70impl<'r> Decode<'r, Sqlite> for Cow<'r, str> {
71    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
72        value.text().map(Cow::Borrowed)
73    }
74}