Skip to content

Commit

Permalink
add UuidFromParam macro for UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Jan 5, 2025
1 parent 06026e3 commit 39d8446
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 22 deletions.
33 changes: 30 additions & 3 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,41 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_derive(UuidFromParam)]
pub fn derive_uuid_from_param(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();

impl_derive_uuid_macro(&ast)
}

fn impl_derive_uuid_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
#[automatically_derived]
impl<'r> rocket::request::FromParam<'r> for #name {
type Error = ();

#[inline(always)]
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
if uuid::Uuid::parse_str(param).is_ok() {
Ok(Self(param.to_string()))
} else {
Err(())
}
}
}
};
gen.into()
}

#[proc_macro_derive(IdFromParam)]
pub fn derive_from_param(input: TokenStream) -> TokenStream {
pub fn derive_id_from_param(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();

impl_derive_macro(&ast)
impl_derive_safestring_macro(&ast)
}

fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
fn impl_derive_safestring_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
#[automatically_derived]
Expand Down
4 changes: 2 additions & 2 deletions src/db/models/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{DeviceId, OrganizationId, UserId};
use crate::crypto::ct_eq;
use chrono::{NaiveDateTime, Utc};
use derive_more::{AsRef, Deref, Display, From};
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Debug, Identifiable, Queryable, Insertable, AsChangeset, Deserialize, Serialize)]
Expand Down Expand Up @@ -175,6 +175,6 @@ impl AuthRequest {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct AuthRequestId(String);
4 changes: 2 additions & 2 deletions src/db/models/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
MembershipType, OrganizationId, User, UserId,
};
use crate::api::core::{CipherData, CipherSyncData, CipherSyncType};
use macros::IdFromParam;
use macros::UuidFromParam;

use std::borrow::Cow;

Expand Down Expand Up @@ -1071,6 +1071,6 @@ impl Cipher {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct CipherId(String);
4 changes: 2 additions & 2 deletions src/db/models/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
User, UserId,
};
use crate::CONFIG;
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -828,6 +828,6 @@ impl From<CollectionUser> for CollectionMembership {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct CollectionId(String);
4 changes: 2 additions & 2 deletions src/db/models/emergency_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::Value;

use super::{User, UserId};
use crate::{api::EmptyResult, db::DbConn, error::MapResult};
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -377,6 +377,6 @@ impl EmergencyAccess {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct EmergencyAccessId(String);
4 changes: 2 additions & 2 deletions src/db/models/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use derive_more::{AsRef, Deref, Display, From};
use serde_json::Value;

use super::{CipherId, User, UserId};
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -248,6 +248,6 @@ impl FolderCipher {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct FolderId(String);
4 changes: 2 additions & 2 deletions src/db/models/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::db::DbConn;
use crate::error::MapResult;
use chrono::{NaiveDateTime, Utc};
use derive_more::{AsRef, Deref, Display, From};
use macros::IdFromParam;
use macros::UuidFromParam;
use serde_json::Value;

db_object! {
Expand Down Expand Up @@ -618,6 +618,6 @@ impl GroupUser {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct GroupId(String);
6 changes: 3 additions & 3 deletions src/db/models/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
OrgPolicyType, TwoFactor, User, UserId,
};
use crate::CONFIG;
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -1134,7 +1134,7 @@ impl OrganizationApiKey {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
#[deref(forward)]
#[from(forward)]
Expand All @@ -1153,7 +1153,7 @@ pub struct OrganizationId(String);
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct MembershipId(String);

Expand Down
4 changes: 2 additions & 2 deletions src/db/models/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl Send {
// separate namespace to avoid name collision with std::marker::Send
pub mod id {
use derive_more::{AsRef, Deref, Display, From};
use macros::IdFromParam;
use macros::{IdFromParam, UuidFromParam};
use std::marker::Send;
use std::path::Path;

Expand All @@ -371,7 +371,7 @@ pub mod id {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct SendId(String);

Expand Down
4 changes: 2 additions & 2 deletions src/db/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
util::{format_date, get_uuid, retry},
CONFIG,
};
use macros::IdFromParam;
use macros::UuidFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -473,7 +473,7 @@ impl Invitation {
Deref,
Display,
From,
IdFromParam,
UuidFromParam,
)]
#[deref(forward)]
#[from(forward)]
Expand Down

0 comments on commit 39d8446

Please sign in to comment.