sqlx_postgres/types/
bytes.rs

1use std::borrow::Cow;
2use std::rc::Rc;
3use std::sync::Arc;
4
5use crate::decode::Decode;
6use crate::encode::{Encode, IsNull};
7use crate::error::BoxDynError;
8use crate::types::Type;
9use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
10
11impl PgHasArrayType for u8 {
12    fn array_type_info() -> PgTypeInfo {
13        PgTypeInfo::BYTEA
14    }
15}
16
17impl PgHasArrayType for &'_ [u8] {
18    fn array_type_info() -> PgTypeInfo {
19        PgTypeInfo::BYTEA_ARRAY
20    }
21}
22
23impl PgHasArrayType for Box<[u8]> {
24    fn array_type_info() -> PgTypeInfo {
25        <[&[u8]] as Type<Postgres>>::type_info()
26    }
27}
28
29impl PgHasArrayType for Vec<u8> {
30    fn array_type_info() -> PgTypeInfo {
31        <[&[u8]] as Type<Postgres>>::type_info()
32    }
33}
34
35impl<const N: usize> PgHasArrayType for [u8; N] {
36    fn array_type_info() -> PgTypeInfo {
37        <[&[u8]] as Type<Postgres>>::type_info()
38    }
39}
40
41impl Encode<'_, Postgres> for &'_ [u8] {
42    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
43        buf.extend_from_slice(self);
44
45        Ok(IsNull::No)
46    }
47}
48
49impl Encode<'_, Postgres> for Vec<u8> {
50    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
51        <&[u8] as Encode<Postgres>>::encode(self, buf)
52    }
53}
54
55impl<const N: usize> Encode<'_, Postgres> for [u8; N] {
56    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
57        <&[u8] as Encode<Postgres>>::encode(self.as_slice(), buf)
58    }
59}
60
61impl<'r> Decode<'r, Postgres> for &'r [u8] {
62    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
63        match value.format() {
64            PgValueFormat::Binary => value.as_bytes(),
65            PgValueFormat::Text => {
66                Err("unsupported decode to `&[u8]` of BYTEA in a simple query; use a prepared query or decode to `Vec<u8>`".into())
67            }
68        }
69    }
70}
71
72fn text_hex_decode_input(value: PgValueRef<'_>) -> Result<&[u8], BoxDynError> {
73    // BYTEA is formatted as \x followed by hex characters
74    value
75        .as_bytes()?
76        .strip_prefix(b"\\x")
77        .ok_or("text does not start with \\x")
78        .map_err(Into::into)
79}
80
81impl Decode<'_, Postgres> for Vec<u8> {
82    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
83        Ok(match value.format() {
84            PgValueFormat::Binary => value.as_bytes()?.to_owned(),
85            PgValueFormat::Text => hex::decode(text_hex_decode_input(value)?)?,
86        })
87    }
88}
89
90impl<const N: usize> Decode<'_, Postgres> for [u8; N] {
91    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
92        let mut bytes = [0u8; N];
93        match value.format() {
94            PgValueFormat::Binary => {
95                bytes = value.as_bytes()?.try_into()?;
96            }
97            PgValueFormat::Text => hex::decode_to_slice(text_hex_decode_input(value)?, &mut bytes)?,
98        };
99        Ok(bytes)
100    }
101}
102
103forward_encode_impl!(Arc<[u8]>, &[u8], Postgres);
104forward_encode_impl!(Rc<[u8]>, &[u8], Postgres);
105forward_encode_impl!(Box<[u8]>, &[u8], Postgres);
106forward_encode_impl!(Cow<'_, [u8]>, &[u8], Postgres);