Skip to content

Commit 536fd56

Browse files
committed
Merge branch 'dev' into issue-151-migrations
2 parents 6c2b745 + b672127 commit 536fd56

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
@@ -199,18 +199,16 @@ pub mod postgres {
199199
use super::ChannelId;
200200
use super::{Channel, ChannelSpec};
201201
use bb8_postgres::tokio_postgres::{types::Json, Row};
202+
use bytes::BytesMut;
202203
use hex::FromHex;
203-
use postgres_types::{FromSql, Type};
204+
use postgres_types::{FromSql, IsNull, ToSql, Type};
204205
use std::error::Error;
205206

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

210-
// FromHex::from_hex for fixed-sized arrays will guard against the length of the string!
211-
let id: [u8; 32] = <[u8; 32] as FromHex>::from_hex(str_slice)?;
212-
213-
Ok(ChannelId(id))
211+
Ok(ChannelId::from_hex(str_slice)?)
214212
}
215213

216214
fn accepts(ty: &Type) -> bool {
@@ -221,6 +219,32 @@ pub mod postgres {
221219
}
222220
}
223221

222+
impl ToSql for ChannelId {
223+
fn to_sql(
224+
&self,
225+
ty: &Type,
226+
w: &mut BytesMut,
227+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
228+
let string = format!("0x{}", hex::encode(self));
229+
230+
<String as ToSql>::to_sql(&string, ty, w)
231+
}
232+
233+
fn accepts(ty: &Type) -> bool {
234+
<String as ToSql>::accepts(ty)
235+
}
236+
237+
fn to_sql_checked(
238+
&self,
239+
ty: &Type,
240+
out: &mut BytesMut,
241+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
242+
let string = format!("0x{}", hex::encode(self));
243+
244+
<String as ToSql>::to_sql_checked(&string, ty, out)
245+
}
246+
}
247+
224248
impl From<&Row> for Channel {
225249
fn from(row: &Row) -> Self {
226250
Self {

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
@@ -22,8 +22,6 @@ pub async fn get_channel(
2222
pool: &DbPool,
2323
id: &ChannelId,
2424
) -> Result<Option<Channel>, RunError<bb8_postgres::tokio_postgres::Error>> {
25-
let id = hex::encode(id);
26-
2725
pool
2826
.run(move |connection| {
2927
async move {

0 commit comments

Comments
 (0)