Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix most tests + bitflags
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Oct 10, 2023
1 parent 1dc2557 commit 87a7db3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 49 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sha1 = { version = "0.6.1", features = ["std"] }
sha2 = "0.9.9"
hmac = "0.11.0"
argon2 = { version = "0.5.0", features = ["std"] }
bitflags = { version = "2.4.0", features = ["serde"] }
bitflags = "2.4.0"
bitflags-serde-legacy = "0.1.1"
hex = "0.4.3"
zxcvbn = "2.2.2"
Expand Down
15 changes: 3 additions & 12 deletions src/models/pats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ids::Base62Id;
use crate::bitflags_serde_impl;
use crate::models::ids::UserId;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -106,6 +107,8 @@ bitflags::bitflags! {
}
}

bitflags_serde_impl!(Scopes, u64);

impl Scopes {
// these scopes cannot be specified in a personal access token
pub fn restricted() -> Scopes {
Expand All @@ -125,18 +128,6 @@ impl Scopes {
}
}

impl serde::Serialize for Scopes {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}

impl<'de> serde::Deserialize<'de> for Scopes {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}

#[derive(Serialize, Deserialize)]
pub struct PersonalAccessToken {
pub id: PatId,
Expand Down
25 changes: 3 additions & 22 deletions src/models/teams.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ids::Base62Id;
use crate::bitflags_serde_impl;
use crate::models::users::User;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -39,17 +40,7 @@ bitflags::bitflags! {
}
}

impl serde::Serialize for ProjectPermissions {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}

impl<'de> serde::Deserialize<'de> for ProjectPermissions {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}
bitflags_serde_impl!(ProjectPermissions, u64);

impl Default for ProjectPermissions {
fn default() -> ProjectPermissions {
Expand Down Expand Up @@ -104,17 +95,7 @@ bitflags::bitflags! {
}
}

impl serde::Serialize for OrganizationPermissions {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}

impl<'de> serde::Deserialize<'de> for OrganizationPermissions {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}
bitflags_serde_impl!(OrganizationPermissions, u64);

impl Default for OrganizationPermissions {
fn default() -> OrganizationPermissions {
Expand Down
13 changes: 2 additions & 11 deletions src/models/users.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ids::Base62Id;
use crate::auth::flows::AuthProvider;
use crate::bitflags_serde_impl;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -28,17 +29,7 @@ bitflags::bitflags! {
}
}

impl serde::Serialize for Badges {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}

impl<'de> serde::Deserialize<'de> for Badges {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}
bitflags_serde_impl!(Badges, u64);

impl Default for Badges {
fn default() -> Badges {
Expand Down
19 changes: 19 additions & 0 deletions src/util/bitflag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[macro_export]
macro_rules! bitflags_serde_impl {
($type:ident, $int_type:ident) => {
impl serde::Serialize for $type {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_i64(self.bits() as i64)
}
}

impl<'de> serde::Deserialize<'de> for $type {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let v: i64 = Deserialize::deserialize(deserializer)?;

println!("{}", v);
Ok($type::from_bits_truncate(v as $int_type))
}
}
};
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bitflag;
pub mod captcha;
pub mod cors;
pub mod env;
Expand Down

0 comments on commit 87a7db3

Please sign in to comment.