Skip to main content

spg_sqlx/types/
bool.rs

1//! v7.16.0 — `Type` / `Encode` / `Decode` for `bool` / BOOLEAN.
2
3use sqlx_core::decode::Decode;
4use sqlx_core::encode::{Encode, IsNull};
5use sqlx_core::error::BoxDynError;
6use sqlx_core::types::Type;
7
8use spg_embedded::Value as EngineValue;
9
10use crate::arguments::SpgArgumentValue;
11use crate::database::Spg;
12use crate::type_info::{Kind, SpgTypeInfo};
13use crate::value::SpgValueRef;
14
15impl Type<Spg> for bool {
16    fn type_info() -> SpgTypeInfo {
17        SpgTypeInfo::of(Kind::Bool)
18    }
19
20    fn compatible(ty: &SpgTypeInfo) -> bool {
21        matches!(ty.kind(), Kind::Bool)
22    }
23}
24
25impl<'q> Encode<'q, Spg> for bool {
26    fn encode_by_ref(
27        &self,
28        buf: &mut Vec<SpgArgumentValue<'q>>,
29    ) -> Result<IsNull, BoxDynError> {
30        buf.push(SpgArgumentValue {
31            value: EngineValue::Bool(*self),
32            type_info: Some(<bool as Type<Spg>>::type_info()),
33            _phantom: core::marker::PhantomData,
34        });
35        Ok(IsNull::No)
36    }
37}
38
39impl<'r> Decode<'r, Spg> for bool {
40    fn decode(value: SpgValueRef<'r>) -> Result<Self, BoxDynError> {
41        match value.engine() {
42            EngineValue::Bool(b) => Ok(*b),
43            other => Err(format!("cannot decode {other:?} as bool / BOOLEAN").into()),
44        }
45    }
46}