Skip to content

Commit eb89121

Browse files
committed
wrap uuid so we can bridge diesel and rockets uuid types manually
1 parent a0a44b9 commit eb89121

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ edition = "2018"
77
[dependencies]
88
diesel = { version = "1.3.2", features = ["postgres", "chrono", "uuid"] }
99
postgres_resource_derive = { path = "./postgres_resource_derive" }
10+
uuid = { version = "0.7", features = ["serde", "v4"] }
11+
serde_derive = "1.0"
12+
serde = "1.0"
1013

1114
[dev-dependencies]
1215
diesel_migrations = { version = "1.3.0", features = ["postgres"] }

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
#![feature(custom_attribute)]
12
pub use postgres_resource_derive::resource;
3+
pub mod uuid;
4+
5+
#[macro_use]
6+
extern crate diesel;
27

38
use diesel::{
4-
self,
59
expression::BoxableExpression,
610
pg::{Pg, PgConnection},
711
prelude::*,

src/uuid.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use diesel::{
2+
deserialize::{self, FromSql},
3+
pg::Pg,
4+
serialize::{self, IsNull, Output, ToSql},
5+
sql_types::Uuid as UuidDiesel,
6+
not_none,
7+
expression::Expression,
8+
};
9+
use serde_derive::{Serialize, Deserialize};
10+
use std::io::Write;
11+
use uuid;
12+
13+
#[derive(Clone, Debug, AsExpression, PartialEq, FromSqlRow, Serialize, Deserialize, Hash, Eq)]
14+
#[sql_type = "UuidDiesel"]
15+
pub struct Uuid(pub uuid::Uuid);
16+
17+
impl ToSql<UuidDiesel, Pg> for Uuid {
18+
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
19+
out.write_all(self.0.as_bytes())?;
20+
Ok(IsNull::No)
21+
}
22+
}
23+
24+
impl FromSql<UuidDiesel, Pg> for Uuid {
25+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
26+
let bytes = not_none!(bytes);
27+
Ok(Uuid(uuid::Uuid::from_slice(bytes)?))
28+
}
29+
}
30+
31+
impl From<uuid::Uuid> for Uuid {
32+
fn from(uuid: uuid::Uuid) -> Self {
33+
Uuid(uuid)
34+
}
35+
}

0 commit comments

Comments
 (0)