Skip to content

Commit b672127

Browse files
authored
Merge pull request #195 from AdExNetwork/impl-ToSql
Impl ToSql
2 parents 75ab5b0 + d972d8f commit b672127

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

primitives/src/channel.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,16 @@ impl Error for ChannelError {
197197
#[cfg(feature = "postgres")]
198198
pub mod postgres {
199199
use super::ChannelId;
200+
use bytes::BytesMut;
200201
use hex::FromHex;
201-
use postgres_types::{FromSql, Type};
202+
use postgres_types::{FromSql, IsNull, ToSql, Type};
202203
use std::error::Error;
203204

204205
impl<'a> FromSql<'a> for ChannelId {
205206
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
206207
let str_slice = <&str as FromSql>::from_sql(ty, raw)?;
207208

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))
209+
Ok(ChannelId::from_hex(str_slice)?)
212210
}
213211

214212
fn accepts(ty: &Type) -> bool {
@@ -218,4 +216,30 @@ pub mod postgres {
218216
}
219217
}
220218
}
219+
220+
impl ToSql for ChannelId {
221+
fn to_sql(
222+
&self,
223+
ty: &Type,
224+
w: &mut BytesMut,
225+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
226+
let string = format!("0x{}", hex::encode(self));
227+
228+
<String as ToSql>::to_sql(&string, ty, w)
229+
}
230+
231+
fn accepts(ty: &Type) -> bool {
232+
<String as ToSql>::accepts(ty)
233+
}
234+
235+
fn to_sql_checked(
236+
&self,
237+
ty: &Type,
238+
out: &mut BytesMut,
239+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
240+
let string = format!("0x{}", hex::encode(self));
241+
242+
<String as ToSql>::to_sql_checked(&string, ty, out)
243+
}
244+
}
221245
}

primitives/src/validator.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ impl ValidatorId {
3636
}
3737
}
3838

39+
impl AsRef<[u8]> for ValidatorId {
40+
fn as_ref(&self) -> &[u8] {
41+
&self.0
42+
}
43+
}
44+
3945
impl TryFrom<&str> for ValidatorId {
4046
type Error = DomainError;
4147
fn try_from(value: &str) -> Result<Self, Self::Error> {
@@ -147,3 +153,54 @@ pub enum MessageTypes {
147153
Heartbeat(Heartbeat),
148154
Accounting(Accounting),
149155
}
156+
157+
#[cfg(feature = "postgres")]
158+
pub mod postgres {
159+
use super::ValidatorId;
160+
use bytes::BytesMut;
161+
use postgres_types::{FromSql, IsNull, ToSql, Type};
162+
use std::convert::TryFrom;
163+
use std::error::Error;
164+
165+
impl<'a> FromSql<'a> for ValidatorId {
166+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
167+
let str_slice = <&str as FromSql>::from_sql(ty, raw)?;
168+
169+
// FromHex::from_hex for fixed-sized arrays will guard against the length of the string!
170+
Ok(ValidatorId::try_from(str_slice)?)
171+
}
172+
173+
fn accepts(ty: &Type) -> bool {
174+
match *ty {
175+
Type::TEXT | Type::VARCHAR => true,
176+
_ => false,
177+
}
178+
}
179+
}
180+
181+
impl ToSql for ValidatorId {
182+
fn to_sql(
183+
&self,
184+
ty: &Type,
185+
w: &mut BytesMut,
186+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
187+
let string = format!("0x{}", self.to_hex_non_prefix_string());
188+
189+
<String as ToSql>::to_sql(&string, ty, w)
190+
}
191+
192+
fn accepts(ty: &Type) -> bool {
193+
<String as ToSql>::accepts(ty)
194+
}
195+
196+
fn to_sql_checked(
197+
&self,
198+
ty: &Type,
199+
out: &mut BytesMut,
200+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
201+
let string = format!("0x{}", self.to_hex_non_prefix_string());
202+
203+
<String as ToSql>::to_sql_checked(&string, ty, out)
204+
}
205+
}
206+
}

sentry/src/middleware/channel.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ pub async fn get_channel(
2323
pool: &DbPool,
2424
id: &ChannelId,
2525
) -> Result<Option<Channel>, RunError<bb8_postgres::tokio_postgres::Error>> {
26-
let id = hex::encode(id);
27-
2826
pool
2927
.run(move |connection| {
3028
async move {

0 commit comments

Comments
 (0)