|
| 1 | +//! Support for the [`diesel`](https://crates.io/crates/diesel) crate. |
| 2 | +//! |
| 3 | +//! Currently only encodes to/from a big-endian byte array. |
| 4 | +
|
| 5 | +#![cfg(feature = "diesel")] |
| 6 | +#![cfg_attr(docsrs, doc(cfg(feature = "diesel")))] |
| 7 | + |
| 8 | +use diesel::{ |
| 9 | + backend::Backend, |
| 10 | + deserialize::{FromSql, Result as DeserResult}, |
| 11 | + expression::AsExpression, |
| 12 | + internal::derives::as_expression::Bound, |
| 13 | + query_builder::bind_collector::RawBytesBindCollector, |
| 14 | + serialize::{IsNull, Output, Result as SerResult, ToSql}, |
| 15 | + sql_types::{Binary, Nullable, SingleValue}, |
| 16 | + Queryable, |
| 17 | +}; |
| 18 | +use std::io::Write; |
| 19 | +use thiserror::Error; |
| 20 | + |
| 21 | +use crate::Uint; |
| 22 | + |
| 23 | +#[derive(Error, Debug)] |
| 24 | +pub enum DecodeError { |
| 25 | + #[error("Value too large for target type")] |
| 26 | + Overflow, |
| 27 | +} |
| 28 | + |
| 29 | +impl<const BITS: usize, const LIMBS: usize, Db> ToSql<Binary, Db> for Uint<BITS, LIMBS> |
| 30 | +where |
| 31 | + for<'c> Db: Backend<BindCollector<'c> = RawBytesBindCollector<Db>>, |
| 32 | +{ |
| 33 | + fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Db>) -> SerResult { |
| 34 | + out.write_all(&self.to_be_bytes_vec())?; |
| 35 | + Ok(IsNull::No) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<const BITS: usize, const LIMBS: usize, Db: Backend> FromSql<Binary, Db> for Uint<BITS, LIMBS> |
| 40 | +where |
| 41 | + *const [u8]: FromSql<Binary, Db>, |
| 42 | +{ |
| 43 | + fn from_sql(bytes: Db::RawValue<'_>) -> DeserResult<Self> { |
| 44 | + let bytes: *const [u8] = FromSql::<Binary, Db>::from_sql(bytes)?; |
| 45 | + let bytes: &[u8] = unsafe { &*bytes }; |
| 46 | + Self::try_from_be_slice(bytes).ok_or_else(|| DecodeError::Overflow.into()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// NB: the following code is expanded derive macros. They were produced by |
| 51 | +// expanding the the following code: |
| 52 | +// ``` |
| 53 | +// #[derive(diesel::AsExpression, diesel::FromSqlRow)] |
| 54 | +// #[diesel(sql_type = diesel::sql_types::Binary)] |
| 55 | +// pub struct Uint<const BITS: usize, const LIMBS: usize> { .. } |
| 56 | +// ``` |
| 57 | + |
| 58 | +impl<'a, const BITS: usize, const LIMBS: usize> AsExpression<Binary> for &'a Uint<BITS, LIMBS> { |
| 59 | + type Expression = Bound<Binary, Self>; |
| 60 | + fn as_expression(self) -> Self::Expression { |
| 61 | + Bound::new(self) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a, const BITS: usize, const LIMBS: usize> AsExpression<Nullable<Binary>> |
| 66 | + for &'a Uint<BITS, LIMBS> |
| 67 | +{ |
| 68 | + type Expression = Bound<Nullable<Binary>, Self>; |
| 69 | + fn as_expression(self) -> Self::Expression { |
| 70 | + Bound::new(self) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a, 'b, const BITS: usize, const LIMBS: usize> AsExpression<Binary> |
| 75 | + for &'b &'a Uint<BITS, LIMBS> |
| 76 | +{ |
| 77 | + type Expression = Bound<Binary, Self>; |
| 78 | + fn as_expression(self) -> Self::Expression { |
| 79 | + Bound::new(self) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<'a, 'b, const BITS: usize, const LIMBS: usize> AsExpression<Nullable<Binary>> |
| 84 | + for &'b &'a Uint<BITS, LIMBS> |
| 85 | +{ |
| 86 | + type Expression = Bound<Nullable<Binary>, Self>; |
| 87 | + fn as_expression(self) -> Self::Expression { |
| 88 | + Bound::new(self) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<const BITS: usize, const LIMBS: usize, Db> ToSql<Nullable<Binary>, Db> for Uint<BITS, LIMBS> |
| 93 | +where |
| 94 | + Db: Backend, |
| 95 | + Self: ToSql<Binary, Db>, |
| 96 | +{ |
| 97 | + fn to_sql<'a>(&'a self, out: &mut Output<'a, '_, Db>) -> SerResult { |
| 98 | + ToSql::<Binary, Db>::to_sql(self, out) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<const BITS: usize, const LIMBS: usize> AsExpression<Binary> for Uint<BITS, LIMBS> { |
| 103 | + type Expression = Bound<Binary, Self>; |
| 104 | + fn as_expression(self) -> Self::Expression { |
| 105 | + Bound::new(self) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<const BITS: usize, const LIMBS: usize> AsExpression<Nullable<Binary>> for Uint<BITS, LIMBS> { |
| 110 | + type Expression = Bound<Nullable<Binary>, Self>; |
| 111 | + fn as_expression(self) -> Self::Expression { |
| 112 | + Bound::new(self) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<const BITS: usize, const LIMBS: usize, Db, St> Queryable<St, Db> for Uint<BITS, LIMBS> |
| 117 | +where |
| 118 | + Db: Backend, |
| 119 | + St: SingleValue, |
| 120 | + Self: FromSql<St, Db>, |
| 121 | +{ |
| 122 | + type Row = Self; |
| 123 | + fn build(row: Self::Row) -> DeserResult<Self> { |
| 124 | + Ok(row) |
| 125 | + } |
| 126 | +} |
0 commit comments