diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2c4e297a3b..ec8863bb6b 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -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 { + 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] diff --git a/src/db/models/auth_request.rs b/src/db/models/auth_request.rs index 6a04c57da6..dd4f098a90 100644 --- a/src/db/models/auth_request.rs +++ b/src/db/models/auth_request.rs @@ -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)] @@ -175,6 +175,6 @@ impl AuthRequest { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct AuthRequestId(String); diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index af34262ca3..3b8d238448 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -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; @@ -1071,6 +1071,6 @@ impl Cipher { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct CipherId(String); diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index d0e8a46565..2302b49391 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -6,7 +6,7 @@ use super::{ User, UserId, }; use crate::CONFIG; -use macros::IdFromParam; +use macros::UuidFromParam; db_object! { #[derive(Identifiable, Queryable, Insertable, AsChangeset)] @@ -828,6 +828,6 @@ impl From for CollectionMembership { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct CollectionId(String); diff --git a/src/db/models/emergency_access.rs b/src/db/models/emergency_access.rs index 7a62bdb4bc..a82801f687 100644 --- a/src/db/models/emergency_access.rs +++ b/src/db/models/emergency_access.rs @@ -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)] @@ -377,6 +377,6 @@ impl EmergencyAccess { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct EmergencyAccessId(String); diff --git a/src/db/models/folder.rs b/src/db/models/folder.rs index deb8819eb0..654ccd6dcf 100644 --- a/src/db/models/folder.rs +++ b/src/db/models/folder.rs @@ -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)] @@ -248,6 +248,6 @@ impl FolderCipher { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct FolderId(String); diff --git a/src/db/models/group.rs b/src/db/models/group.rs index ca3a9acb3b..5a72418d04 100644 --- a/src/db/models/group.rs +++ b/src/db/models/group.rs @@ -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! { @@ -618,6 +618,6 @@ impl GroupUser { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct GroupId(String); diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index ea50e56750..0885dd48a7 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -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)] @@ -1134,7 +1134,7 @@ impl OrganizationApiKey { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] #[deref(forward)] #[from(forward)] @@ -1153,7 +1153,7 @@ pub struct OrganizationId(String); Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct MembershipId(String); diff --git a/src/db/models/send.rs b/src/db/models/send.rs index ee7685a340..c0bb0b3391 100644 --- a/src/db/models/send.rs +++ b/src/db/models/send.rs @@ -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; @@ -371,7 +371,7 @@ pub mod id { Eq, Serialize, Deserialize, - IdFromParam, + UuidFromParam, )] pub struct SendId(String); diff --git a/src/db/models/user.rs b/src/db/models/user.rs index 302b2c9d86..24c86a435e 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -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)] @@ -473,7 +473,7 @@ impl Invitation { Deref, Display, From, - IdFromParam, + UuidFromParam, )] #[deref(forward)] #[from(forward)]