| 1 | use crate::{PgArgumentBuffer, PgTypeInfo, PgValueRef, Postgres}; |
| 2 | use sqlx_core::decode::Decode; |
| 3 | use sqlx_core::encode::{Encode, IsNull}; |
| 4 | use sqlx_core::error::BoxDynError; |
| 5 | use sqlx_core::types::{Text, Type}; |
| 6 | use std::fmt::Display; |
| 7 | use std::str::FromStr; |
| 8 | |
| 9 | use std::io::Write; |
| 10 | |
| 11 | impl<T> Type<Postgres> for Text<T> { |
| 12 | fn type_info() -> PgTypeInfo { |
| 13 | <String as Type<Postgres>>::type_info() |
| 14 | } |
| 15 | |
| 16 | fn compatible(ty: &PgTypeInfo) -> bool { |
| 17 | <String as Type<Postgres>>::compatible(ty) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<'q, T> Encode<'q, Postgres> for Text<T> |
| 22 | where |
| 23 | T: Display, |
| 24 | { |
| 25 | fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 26 | write!(**buf, "{}", self.0)?; |
| 27 | Ok(IsNull::No) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<'r, T> Decode<'r, Postgres> for Text<T> |
| 32 | where |
| 33 | T: FromStr, |
| 34 | BoxDynError: From<<T as FromStr>::Err>, |
| 35 | { |
| 36 | fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> { |
| 37 | let s: &str = Decode::<Postgres>::decode(value)?; |
| 38 | Ok(Self(s.parse()?)) |
| 39 | } |
| 40 | } |