|
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"] |
2 | 9 | pub enum EndpointScope {
|
3 | 10 | PublishNew,
|
4 | 11 | PublishUpdate,
|
5 | 12 | Yank,
|
6 | 13 | ChangeOwners,
|
7 | 14 | }
|
| 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