1use cdbc::decode::Decode;
2use cdbc::encode::{Encode, IsNull};
3use cdbc::error::BoxDynError;
4use crate::{
5 PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres,
6};
7use cdbc::types::Type;
8
9impl Type<Postgres> for bool {
10 fn type_info() -> PgTypeInfo {
11 PgTypeInfo::BOOL
12 }
13}
14
15impl PgHasArrayType for bool {
16 fn array_type_info() -> PgTypeInfo {
17 PgTypeInfo::BOOL_ARRAY
18 }
19}
20
21impl Encode<'_, Postgres> for bool {
22 fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
23 buf.push(*self as u8);
24
25 IsNull::No
26 }
27}
28
29impl Decode<'_, Postgres> for bool {
30 fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
31 Ok(match value.format() {
32 PgValueFormat::Binary => value.as_bytes()?[0] != 0,
33
34 PgValueFormat::Text => match value.as_str()? {
35 "t" => true,
36 "f" => false,
37
38 s => {
39 return Err(format!("unexpected value {:?} for boolean", s).into());
40 }
41 },
42 })
43 }
44}