/
germanubis
/
sqlx
Обзор
Документация
Войти
/
germanubis
/
sqlx
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
sqlx-postgres/src/types/bool.rs
42 строки
1 KB
Max Bruckner
Make Encode return a result (#3126)
31 май 2024, 22:42
Не верифицирован
31 май 2024, 22:42
c57b46c
Код
Авторство
О чём код?
use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; impl Type<Postgres> for bool { fn type_info() -> PgTypeInfo { PgTypeInfo::BOOL } } impl PgHasArrayType for bool { fn array_type_info() -> PgTypeInfo { PgTypeInfo::BOOL_ARRAY } } impl Encode<'_, Postgres> for bool { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { buf.push(*self as u8); Ok(IsNull::No) } } impl Decode<'_, Postgres> for bool { fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> { Ok(match value.format() { PgValueFormat::Binary => value.as_bytes()?[0] != 0, PgValueFormat::Text => match value.as_str()? { "t" => true, "f" => false, s => { return Err(format!("unexpected value {s:?} for boolean").into()); } }, }) } }