Skip to content

Commit 2703d3c

Browse files
committed
move FromSql/ToSql impl to primitives behind a flag postgres
1 parent f53468a commit 2703d3c

File tree

6 files changed

+78
-102
lines changed

6 files changed

+78
-102
lines changed

primitives/src/big_num.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,55 @@ where
222222
serializer.serialize_str(&num.to_str_radix(10))
223223
}
224224

225+
#[cfg(feature = "postgres")]
226+
pub mod postgres {
227+
use super::BigNum;
228+
use bytes::BytesMut;
229+
use postgres_types::{FromSql, IsNull, ToSql, Type};
230+
use std::error::Error;
231+
232+
impl<'a> FromSql<'a> for BigNum {
233+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<BigNum, Box<dyn Error + Sync + Send>> {
234+
use std::convert::TryInto;
235+
236+
let str_slice = <&str as FromSql>::from_sql(ty, raw)?;
237+
238+
Ok(str_slice.try_into()?)
239+
}
240+
241+
fn accepts(ty: &Type) -> bool {
242+
match *ty {
243+
Type::TEXT | Type::VARCHAR => true,
244+
_ => false,
245+
}
246+
}
247+
}
248+
249+
impl ToSql for BigNum {
250+
fn to_sql(
251+
&self,
252+
ty: &Type,
253+
w: &mut BytesMut,
254+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
255+
<String as ToSql>::to_sql(&self.0.to_string(), ty, w)
256+
}
257+
258+
fn accepts(ty: &Type) -> bool {
259+
match *ty {
260+
Type::TEXT | Type::VARCHAR => true,
261+
_ => false,
262+
}
263+
}
264+
265+
fn to_sql_checked(
266+
&self,
267+
ty: &Type,
268+
out: &mut BytesMut,
269+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
270+
<String as ToSql>::to_sql_checked(&self.0.to_string(), ty, out)
271+
}
272+
}
273+
}
225274
#[cfg(test)]
226275
mod test {
227276
use super::*;

primitives/src/channel.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,29 @@ impl Error for ChannelError {
193193
None
194194
}
195195
}
196+
197+
#[cfg(feature = "postgres")]
198+
pub mod postgres {
199+
use super::ChannelId;
200+
use hex::FromHex;
201+
use postgres_types::{FromSql, Type};
202+
use std::error::Error;
203+
204+
impl<'a> FromSql<'a> for ChannelId {
205+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
206+
let str_slice = <&str as FromSql>::from_sql(ty, raw)?;
207+
208+
// FromHex::from_hex for fixed-sized arrays will guard against the length of the string!
209+
let id: [u8; 32] = <[u8; 32] as FromHex>::from_hex(str_slice)?;
210+
211+
Ok(ChannelId(id))
212+
}
213+
214+
fn accepts(ty: &Type) -> bool {
215+
match *ty {
216+
Type::TEXT | Type::VARCHAR => true,
217+
_ => false,
218+
}
219+
}
220+
}
221+
}

primitives/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ pub mod util {
2525
pub mod serde;
2626
}
2727
pub mod validator;
28-
#[cfg(feature = "postgres")]
29-
pub mod postgres {
30-
pub mod field {
31-
pub use bignum::BigNumPg;
32-
pub use channel_id::ChannelIdPg;
33-
34-
mod bignum;
35-
mod channel_id;
36-
}
37-
}
3828

3929
pub use self::ad_unit::AdUnit;
4030
pub use self::balances_map::BalancesMap;

primitives/src/postgres/field/bignum.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

primitives/src/postgres/field/channel_id.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

sentry/src/middleware/channel.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::ResponseError;
33
use bb8::RunError;
44
use bb8_postgres::tokio_postgres::{types::Json, Row};
55
use hyper::{Body, Request};
6-
use primitives::channel::ChannelId;
7-
use primitives::postgres::field::{BigNumPg, ChannelIdPg};
8-
use primitives::{Channel, ChannelSpec};
6+
use primitives::{Channel, ChannelId, ChannelSpec};
97

108
pub async fn channel_load(
119
mut req: Request<Body>,
@@ -44,10 +42,10 @@ pub async fn get_channel(
4442

4543
fn channel_map(row: &Row) -> Channel {
4644
Channel {
47-
id: row.get::<_, ChannelIdPg>("channel_id").into(),
45+
id: row.get("channel_id"),
4846
creator: row.get("creator"),
4947
deposit_asset: row.get("deposit_asset"),
50-
deposit_amount: row.get::<_, BigNumPg>("deposit_amount").into(),
48+
deposit_amount: row.get("deposit_amount"),
5149
valid_until: row.get("valid_until"),
5250
spec: row.get::<_, Json<ChannelSpec>>("spec").0,
5351
}

0 commit comments

Comments
 (0)