Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/models/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod scopes;

use chrono::NaiveDateTime;
use diesel::prelude::*;

Expand Down Expand Up @@ -28,7 +30,7 @@ pub struct ApiToken {
pub crate_scopes: Option<Vec<String>>,
/// A list of endpoint scopes or `None` for the `legacy` endpoint scope (see RFC #2947)
#[serde(skip)]
pub endpoint_scopes: Option<Vec<String>>,
pub endpoint_scopes: Option<Vec<scopes::EndpointScope>>,
}

impl ApiToken {
Expand Down
52 changes: 52 additions & 0 deletions src/models/token/scopes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::serialize::{self, IsNull, Output, ToSql};
use diesel::sql_types::Text;
use std::io::Write;

#[derive(Clone, Copy, Debug, PartialEq, Eq, AsExpression)]
#[sql_type = "Text"]
pub enum EndpointScope {
PublishNew,
PublishUpdate,
Yank,
ChangeOwners,
}

impl From<&EndpointScope> for &[u8] {
fn from(scope: &EndpointScope) -> Self {
match scope {
EndpointScope::PublishNew => b"publish-new",
EndpointScope::PublishUpdate => b"publish-update",
EndpointScope::Yank => b"yank",
EndpointScope::ChangeOwners => b"change-owners",
}
}
}

impl ToSql<Text, Pg> for EndpointScope {
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
out.write_all(self.into())?;
Ok(IsNull::No)
}
}

impl TryFrom<&[u8]> for EndpointScope {
type Error = String;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
match bytes {
b"publish-new" => Ok(EndpointScope::PublishNew),
b"publish-update" => Ok(EndpointScope::PublishUpdate),
b"yank" => Ok(EndpointScope::Yank),
b"change-owners" => Ok(EndpointScope::ChangeOwners),
_ => Err("Unrecognized enum variant".to_string()),
}
}
}

impl FromSql<Text, Pg> for EndpointScope {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
Ok(EndpointScope::try_from(not_none!(bytes))?)
}
}