Skip to content

Commit

Permalink
implement custom derive macro IdFromParam
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Jan 5, 2025
1 parent c2a8fbb commit 06026e3
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 203 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
workspace = { members = ["macros"] }

[package]
name = "vaultwarden"
version = "1.0.0"
Expand Down Expand Up @@ -39,6 +41,8 @@ unstable = []
syslog = "7.0.0"

[dependencies]
macros = { path = "./macros" }

# Logging
log = "0.4.22"
fern = { version = "0.7.1", features = ["syslog-7", "reopen-1"] }
Expand Down
13 changes: 13 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "macros"
version = "0.1.0"
edition = "2021"

[lib]
name = "macros"
path = "src/lib.rs"
proc-macro = true

[dependencies]
quote = "1.0.38"
syn = "2.0.94"
31 changes: 31 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

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

impl_derive_macro(&ast)
}

fn impl_derive_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()
}
31 changes: 16 additions & 15 deletions src/db/models/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::io::ErrorKind;

use bigdecimal::{BigDecimal, ToPrimitive};
use derive_more::{AsRef, Deref, Display};
use rocket::request::FromParam;
use serde_json::Value;

use super::{CipherId, OrganizationId, UserId};
use crate::CONFIG;
use macros::IdFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -230,18 +230,19 @@ impl Attachment {
}
}

#[derive(Clone, Debug, AsRef, Deref, DieselNewType, Display, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(
Clone,
Debug,
AsRef,
Deref,
DieselNewType,
Display,
FromForm,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
IdFromParam,
)]
pub struct AttachmentId(pub String);

impl<'r> FromParam<'r> for AttachmentId {
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(())
}
}
}
30 changes: 15 additions & 15 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 rocket::request::FromParam;
use macros::IdFromParam;

db_object! {
#[derive(Debug, Identifiable, Queryable, Insertable, AsChangeset, Deserialize, Serialize)]
Expand Down Expand Up @@ -162,19 +162,19 @@ impl AuthRequest {
}

#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
Clone,
Debug,
AsRef,
Deref,
DieselNewType,
Display,
From,
FromForm,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
IdFromParam,
)]
pub struct AuthRequestId(String);

impl<'r> FromParam<'r> for AuthRequestId {
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(())
}
}
}
37 changes: 20 additions & 17 deletions src/db/models/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use crate::util::LowerCase;
use crate::CONFIG;
use chrono::{NaiveDateTime, TimeDelta, Utc};
use derive_more::{AsRef, Deref, Display, From};
use rocket::request::FromParam;
use serde_json::Value;

use super::{
Attachment, CollectionCipher, CollectionId, Favorite, FolderCipher, FolderId, Group, Membership, MembershipStatus,
MembershipType, OrganizationId, User, UserId,
};

use crate::api::core::{CipherData, CipherSyncData, CipherSyncType};
use macros::IdFromParam;

use std::borrow::Cow;

Expand Down Expand Up @@ -721,7 +720,11 @@ impl Cipher {
}}
}

pub async fn find_by_uuid_and_org(cipher_uuid: &CipherId, org_uuid: &OrganizationId, conn: &mut DbConn) -> Option<Self> {
pub async fn find_by_uuid_and_org(
cipher_uuid: &CipherId,
org_uuid: &OrganizationId,
conn: &mut DbConn,
) -> Option<Self> {
db_run! {conn: {
ciphers::table
.filter(ciphers::uuid.eq(cipher_uuid))
Expand Down Expand Up @@ -1055,19 +1058,19 @@ impl Cipher {
}

#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
Clone,
Debug,
AsRef,
Deref,
DieselNewType,
Display,
From,
FromForm,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
IdFromParam,
)]
pub struct CipherId(String);

impl<'r> FromParam<'r> for CipherId {
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(())
}
}
}
30 changes: 15 additions & 15 deletions src/db/models/collection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use derive_more::{AsRef, Deref, Display, From};
use rocket::request::FromParam;
use serde_json::Value;

use super::{
CipherId, CollectionGroup, GroupUser, Membership, MembershipId, MembershipStatus, MembershipType, OrganizationId,
User, UserId,
};
use crate::CONFIG;
use macros::IdFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -815,19 +815,19 @@ impl From<CollectionUser> for CollectionMembership {
}

#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
Clone,
Debug,
AsRef,
Deref,
DieselNewType,
Display,
From,
FromForm,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
IdFromParam,
)]
pub struct CollectionId(String);

impl<'r> FromParam<'r> for CollectionId {
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(())
}
}
}
25 changes: 4 additions & 21 deletions src/db/models/device.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use chrono::{NaiveDateTime, Utc};
use derive_more::{Display, From};
use rocket::request::FromParam;

use super::UserId;
use crate::{crypto, CONFIG};
use macros::IdFromParam;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -335,24 +335,7 @@ impl DeviceType {
}
}

#[derive(Clone, Debug, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(
Clone, Debug, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize, IdFromParam,
)]
pub struct DeviceId(String);

impl DeviceId {
pub fn empty() -> Self {
Self(String::from("00000000-0000-0000-0000-000000000000"))
}
}

impl<'r> FromParam<'r> for DeviceId {
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(())
}
}
}
33 changes: 16 additions & 17 deletions src/db/models/emergency_access.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use chrono::{NaiveDateTime, Utc};
use derive_more::{AsRef, Deref, Display, From};
use rocket::request::FromParam;
use serde_json::Value;

use crate::{api::EmptyResult, db::DbConn, error::MapResult};

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

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -365,19 +364,19 @@ impl EmergencyAccess {
// endregion

#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
Clone,
Debug,
AsRef,
Deref,
DieselNewType,
Display,
From,
FromForm,
Hash,
PartialEq,
Eq,
Serialize,
Deserialize,
IdFromParam,
)]
pub struct EmergencyAccessId(String);

impl<'r> FromParam<'r> for EmergencyAccessId {
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(())
}
}
}
Loading

0 comments on commit 06026e3

Please sign in to comment.