mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2025-12-10 10:24:07 +01:00
* rename membership rename UserOrganization to Membership to clarify the relation and prevent confusion whether something refers to a member(ship) or user * use newtype pattern * implement custom derive macro IdFromParam * add UuidFromParam macro for UUIDs * add macros to Docker build Co-authored-by: dfunkt <dfunkt@users.noreply.github.com> --------- Co-authored-by: dfunkt <dfunkt@users.noreply.github.com>
58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
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_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]
|
|
impl<'r> rocket::request::FromParam<'r> for #name {
|
|
type Error = ();
|
|
|
|
#[inline(always)]
|
|
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
|
|
if param.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' |'0'..='9' | '-')) {
|
|
Ok(Self(param.to_string()))
|
|
} else {
|
|
Err(())
|
|
}
|
|
}
|
|
}
|
|
};
|
|
gen.into()
|
|
}
|