sqlx_postgres/types/
text.rs

1use crate::{PgArgumentBuffer, PgTypeInfo, PgValueRef, Postgres};
2use sqlx_core::decode::Decode;
3use sqlx_core::encode::{Encode, IsNull};
4use sqlx_core::error::BoxDynError;
5use sqlx_core::types::{Text, Type};
6use std::fmt::Display;
7use std::str::FromStr;
8
9use std::io::Write;
10
11impl<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
21impl<'q, T> Encode<'q, Postgres> for Text<T>
22where
23    T: Display,
24{
25    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
26        // Unfortunately, our API design doesn't give us a way to bubble up the error here.
27        //
28        // Fortunately, writing to `Vec<u8>` is infallible so the only possible source of
29        // errors is from the implementation of `Display::fmt()` itself,
30        // where the onus is on the user.
31        //
32        // The blanket impl of `ToString` also panics if there's an error, so this is not
33        // unprecedented.
34        //
35        // However, the panic should be documented anyway.
36        write!(**buf, "{}", self.0).expect("unexpected error from `Display::fmt()`");
37        IsNull::No
38    }
39}
40
41impl<'r, T> Decode<'r, Postgres> for Text<T>
42where
43    T: FromStr,
44    BoxDynError: From<<T as FromStr>::Err>,
45{
46    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
47        let s: &str = Decode::<Postgres>::decode(value)?;
48        Ok(Self(s.parse()?))
49    }
50}