rbdc_pg/types/
bool.rs

1use crate::arguments::PgArgumentBuffer;
2use crate::types::decode::Decode;
3use crate::types::encode::{Encode, IsNull};
4use crate::value::{PgValue, PgValueFormat};
5use rbdc::Error;
6
7impl Encode for bool {
8    fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, Error> {
9        buf.push(self as u8);
10        Ok(IsNull::No)
11    }
12}
13
14impl Decode for bool {
15    fn decode(value: PgValue) -> Result<Self, Error> {
16        Ok(match value.format() {
17            PgValueFormat::Binary => value.as_bytes()?[0] != 0,
18
19            PgValueFormat::Text => match value.as_str()? {
20                "t" => true,
21                "f" => false,
22
23                s => {
24                    return Err(format!("unexpected value {:?} for boolean", s).into());
25                }
26            },
27        })
28    }
29}