Skip to content

Commit d332df7

Browse files
committed
primitives - impl ToSql for ChannelSpec
1 parent c00ec37 commit d332df7

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

primitives/src/channel.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ pub mod postgres {
200200
use super::{Channel, ChannelSpec};
201201
use bytes::BytesMut;
202202
use hex::FromHex;
203-
use postgres_types::{FromSql, IsNull, ToSql, Type};
203+
use postgres_types::{accepts, to_sql_checked, FromSql, IsNull, Json, ToSql, Type};
204204
use std::error::Error;
205-
use tokio_postgres::{types::Json, Row};
205+
use tokio_postgres::Row;
206206

207207
impl From<&Row> for Channel {
208208
fn from(row: &Row) -> Self {
@@ -224,12 +224,7 @@ pub mod postgres {
224224
Ok(ChannelId::from_hex(&str_slice[2..])?)
225225
}
226226

227-
fn accepts(ty: &Type) -> bool {
228-
match *ty {
229-
Type::TEXT | Type::VARCHAR => true,
230-
_ => false,
231-
}
232-
}
227+
accepts!(TEXT, VARCHAR);
233228
}
234229

235230
impl ToSql for ChannelId {
@@ -247,14 +242,19 @@ pub mod postgres {
247242
<String as ToSql>::accepts(ty)
248243
}
249244

250-
fn to_sql_checked(
245+
to_sql_checked!();
246+
}
247+
248+
impl ToSql for ChannelSpec {
249+
fn to_sql(
251250
&self,
252251
ty: &Type,
253-
out: &mut BytesMut,
252+
w: &mut BytesMut,
254253
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
255-
let string = format!("0x{}", hex::encode(self));
256-
257-
<String as ToSql>::to_sql_checked(&string, ty, out)
254+
Json(self).to_sql(ty, w)
258255
}
256+
257+
accepts!(JSONB);
258+
to_sql_checked!();
259259
}
260260
}

0 commit comments

Comments
 (0)