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:
parent
d0fb15e97a
commit
0fcb4d5170
10 changed files with 50 additions and 23 deletions
|
@ -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]
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Add table
Reference in a new issue