| 48 | |
| 49 | impl<const N: usize> Decode<'_, Postgres> for ByteArray<N> { |
| 50 | fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> { |
| 51 | let mut bytes = [0u8; N]; |
| 52 | match value.format() { |
| 53 | // prepared query |
| 54 | PgValueFormat::Binary => { |
| 55 | bytes = value.as_bytes()?.try_into()?; |
| 56 | } |
| 57 | // unprepared raw query |
| 58 | PgValueFormat::Text => { |
| 59 | let text = value |
| 60 | .as_bytes()? |
| 61 | .strip_prefix(b"\\x") |
| 62 | .ok_or("text does not start with \\x")?; |
| 63 | const_hex::decode_to_slice(text, &mut bytes)? |
| 64 | } |
| 65 | }; |
| 66 | Ok(Self(bytes)) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<const N: usize> Encode<'_, Postgres> for ByteArray<N> { |