Skip to content

Commit 413ae86

Browse files
committed
EndpointScope: Implement To/FromSql traits
1 parent 82ee5c5 commit 413ae86

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/models/token/scopes.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1+
use diesel::deserialize::{self, FromSql};
2+
use diesel::pg::Pg;
3+
use diesel::serialize::{self, IsNull, Output, ToSql};
4+
use diesel::sql_types::Text;
5+
use std::io::Write;
6+
7+
#[derive(Clone, Copy, Debug, PartialEq, Eq, AsExpression)]
8+
#[sql_type = "Text"]
29
pub enum EndpointScope {
310
PublishNew,
411
PublishUpdate,
512
Yank,
613
ChangeOwners,
714
}
15+
16+
impl ToSql<Text, Pg> for EndpointScope {
17+
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
18+
match *self {
19+
EndpointScope::PublishNew => out.write_all(b"publish-new")?,
20+
EndpointScope::PublishUpdate => out.write_all(b"publish-update")?,
21+
EndpointScope::Yank => out.write_all(b"yank")?,
22+
EndpointScope::ChangeOwners => out.write_all(b"change-owners")?,
23+
}
24+
Ok(IsNull::No)
25+
}
26+
}
27+
28+
impl FromSql<Text, Pg> for EndpointScope {
29+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
30+
match not_none!(bytes) {
31+
b"publish-new" => Ok(EndpointScope::PublishNew),
32+
b"publish-update" => Ok(EndpointScope::PublishUpdate),
33+
b"yank" => Ok(EndpointScope::Yank),
34+
b"change-owners" => Ok(EndpointScope::ChangeOwners),
35+
_ => Err("Unrecognized enum variant".into()),
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)