From 87a7db32f277977443e6cc67c4bf1dead2123cd7 Mon Sep 17 00:00:00 2001 From: Jai A Date: Tue, 10 Oct 2023 00:45:35 -0700 Subject: [PATCH] Fix most tests + bitflags --- Cargo.lock | 3 --- Cargo.toml | 2 +- src/models/pats.rs | 15 +++------------ src/models/teams.rs | 25 +++---------------------- src/models/users.rs | 13 ++----------- src/util/bitflag.rs | 19 +++++++++++++++++++ src/util/mod.rs | 1 + 7 files changed, 29 insertions(+), 49 deletions(-) create mode 100644 src/util/bitflag.rs diff --git a/Cargo.lock b/Cargo.lock index 976be2f1..9e5ada17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -601,9 +601,6 @@ name = "bitflags" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" -dependencies = [ - "serde", -] [[package]] name = "bitflags-serde-legacy" diff --git a/Cargo.toml b/Cargo.toml index 7bc84550..ae6e7df8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/models/pats.rs b/src/models/pats.rs index 47c0a025..6ac2afc8 100644 --- a/src/models/pats.rs +++ b/src/models/pats.rs @@ -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}; @@ -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 { @@ -125,18 +128,6 @@ impl Scopes { } } -impl serde::Serialize for Scopes { - fn serialize(&self, serializer: S) -> Result { - bitflags_serde_legacy::serialize(self, "Flags", serializer) - } -} - -impl<'de> serde::Deserialize<'de> for Scopes { - fn deserialize>(deserializer: D) -> Result { - bitflags_serde_legacy::deserialize("Flags", deserializer) - } -} - #[derive(Serialize, Deserialize)] pub struct PersonalAccessToken { pub id: PatId, diff --git a/src/models/teams.rs b/src/models/teams.rs index ccb8aab3..9182474d 100644 --- a/src/models/teams.rs +++ b/src/models/teams.rs @@ -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}; @@ -39,17 +40,7 @@ bitflags::bitflags! { } } -impl serde::Serialize for ProjectPermissions { - fn serialize(&self, serializer: S) -> Result { - bitflags_serde_legacy::serialize(self, "Flags", serializer) - } -} - -impl<'de> serde::Deserialize<'de> for ProjectPermissions { - fn deserialize>(deserializer: D) -> Result { - bitflags_serde_legacy::deserialize("Flags", deserializer) - } -} +bitflags_serde_impl!(ProjectPermissions, u64); impl Default for ProjectPermissions { fn default() -> ProjectPermissions { @@ -104,17 +95,7 @@ bitflags::bitflags! { } } -impl serde::Serialize for OrganizationPermissions { - fn serialize(&self, serializer: S) -> Result { - bitflags_serde_legacy::serialize(self, "Flags", serializer) - } -} - -impl<'de> serde::Deserialize<'de> for OrganizationPermissions { - fn deserialize>(deserializer: D) -> Result { - bitflags_serde_legacy::deserialize("Flags", deserializer) - } -} +bitflags_serde_impl!(OrganizationPermissions, u64); impl Default for OrganizationPermissions { fn default() -> OrganizationPermissions { diff --git a/src/models/users.rs b/src/models/users.rs index c1779b7b..8a012bf1 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -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}; @@ -28,17 +29,7 @@ bitflags::bitflags! { } } -impl serde::Serialize for Badges { - fn serialize(&self, serializer: S) -> Result { - bitflags_serde_legacy::serialize(self, "Flags", serializer) - } -} - -impl<'de> serde::Deserialize<'de> for Badges { - fn deserialize>(deserializer: D) -> Result { - bitflags_serde_legacy::deserialize("Flags", deserializer) - } -} +bitflags_serde_impl!(Badges, u64); impl Default for Badges { fn default() -> Badges { diff --git a/src/util/bitflag.rs b/src/util/bitflag.rs new file mode 100644 index 00000000..4b51521d --- /dev/null +++ b/src/util/bitflag.rs @@ -0,0 +1,19 @@ +#[macro_export] +macro_rules! bitflags_serde_impl { + ($type:ident, $int_type:ident) => { + impl serde::Serialize for $type { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_i64(self.bits() as i64) + } + } + + impl<'de> serde::Deserialize<'de> for $type { + fn deserialize>(deserializer: D) -> Result { + let v: i64 = Deserialize::deserialize(deserializer)?; + + println!("{}", v); + Ok($type::from_bits_truncate(v as $int_type)) + } + } + }; +} diff --git a/src/util/mod.rs b/src/util/mod.rs index fa514c59..83ef106f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,4 @@ +pub mod bitflag; pub mod captcha; pub mod cors; pub mod env;