0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-04-08 02:55:11 -05:00

add UuidFromParam macro for UUIDs

This commit is contained in:
Stefan Melmuk 2025-01-05 14:14:35 +01:00
parent d0fb15e97a
commit 0fcb4d5170
No known key found for this signature in database
GPG key ID: 817020C608FE9C09
10 changed files with 50 additions and 23 deletions

View file

@ -3,14 +3,41 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(IdFromParam)]
pub fn derive_from_param(input: TokenStream) -> TokenStream {
#[proc_macro_derive(UuidFromParam)]
pub fn derive_uuid_from_param(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_derive_macro(&ast)
impl_derive_uuid_macro(&ast)
}
fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
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_id_from_param(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_derive_safestring_macro(&ast)
}
fn impl_derive_safestring_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
#[automatically_derived]

View file

@ -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);

View file

@ -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);

View file

@ -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<CollectionUser> for CollectionMembership {
Eq,
Serialize,
Deserialize,
IdFromParam,
UuidFromParam,
)]
pub struct CollectionId(String);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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)]