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(&self, buf: &mut Vec<SpgArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
27        buf.push(SpgArgumentValue {
28            value: EngineValue::Bool(*self),
29            type_info: Some(<bool as Type<Spg>>::type_info()),
30            _phantom: core::marker::PhantomData,
31        });
32        Ok(IsNull::No)
33    }
34}
35
36impl<'r> Decode<'r, Spg> for bool {
37    fn decode(value: SpgValueRef<'r>) -> Result<Self, BoxDynError> {
38        match value.engine() {
39            EngineValue::Bool(b) => Ok(*b),
40            other => Err(format!("cannot decode {other:?} as bool / BOOLEAN").into()),
41        }
42    }
43}