Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/fortifier-macros-tests/tests/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use trybuild::TestCases;
#[test]
fn validations() {
let t = TestCases::new();
t.pass("tests/validations/*/root_generics_pass.rs");
// t.pass("tests/validations/*/*_pass.rs");
// t.compile_fail("tests/validations/*/*_fail.rs");
t.pass("tests/validations/*/*_pass.rs");
t.compile_fail("tests/validations/*/*_fail.rs");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use fortifier::{Validate, error_code};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Validate)]
struct CustomData<'a> {
#[validate(custom(function = custom, error = CustomError))]
zero_options: &'a str,

#[validate(custom(function = custom, error = CustomError))]
strip_one_option: Option<&'a str>,
#[validate(custom(function = custom, error = CustomError))]
strip_two_options: Option<Option<&'a str>>,
#[validate(custom(function = custom, error = CustomError))]
strip_three_options: Option<Option<Option<&'a str>>>,

#[validate(custom(function = custom_one_option, error = CustomError, options))]
strip_no_options_from_one: Option<&'a str>,
#[validate(custom(function = custom_two_options, error = CustomError, options))]
strip_no_options_from_two: Option<Option<&'a str>>,
#[validate(custom(function = custom_three_options, error = CustomError, options))]
strip_no_options_from_three: Option<Option<Option<&'a str>>>,

#[validate(custom(function = custom_one_option, error = CustomError, options = 1))]
strip_to_one_option_from_one: Option<&'a str>,
#[validate(custom(function = custom_one_option, error = CustomError, options = 1))]
strip_to_one_option_from_two: Option<Option<&'a str>>,
#[validate(custom(function = custom_one_option, error = CustomError, options = 1))]
strip_to_one_option_from_three: Option<Option<Option<&'a str>>>,

#[validate(custom(function = custom_one_option, error = CustomError, options = 2))]
strip_to_two_options_from_one: Option<&'a str>,
#[validate(custom(function = custom_two_options, error = CustomError, options = 2))]
strip_to_two_options_from_two: Option<Option<&'a str>>,
#[validate(custom(function = custom_two_options, error = CustomError, options = 2))]
strip_to_two_options_from_three: Option<Option<Option<&'a str>>>,
}

error_code!(CustomErrorCode, "custom");

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
struct CustomError {
code: CustomErrorCode,
}

fn custom(value: &str) -> Result<(), CustomError> {
if value == "" {
Ok(())
} else {
Err(CustomError {
code: CustomErrorCode,
})
}
}

fn custom_one_option(value: &Option<&str>) -> Result<(), CustomError> {
if let Some(value) = value
&& *value == ""
{
Ok(())
} else {
Err(CustomError {
code: CustomErrorCode,
})
}
}

fn custom_two_options(value: &Option<Option<&str>>) -> Result<(), CustomError> {
if let Some(Some(value)) = value
&& *value == ""
{
Ok(())
} else {
Err(CustomError {
code: CustomErrorCode,
})
}
}

fn custom_three_options(value: &Option<Option<Option<&str>>>) -> Result<(), CustomError> {
if let Some(Some(Some(value))) = value
&& *value == ""
{
Ok(())
} else {
Err(CustomError {
code: CustomErrorCode,
})
}
}

fn main() {
let data = CustomData {
zero_options: "",

strip_one_option: Some(""),
strip_two_options: Some(Some("")),
strip_three_options: Some(Some(Some(""))),

strip_no_options_from_one: Some(""),
strip_no_options_from_two: Some(Some("")),
strip_no_options_from_three: Some(Some(Some(""))),

strip_to_one_option_from_one: Some(""),
strip_to_one_option_from_two: Some(Some("")),
strip_to_one_option_from_three: Some(Some(Some(""))),

strip_to_two_options_from_one: Some(""),
strip_to_two_options_from_two: Some(Some("")),
strip_to_two_options_from_three: Some(Some(Some(""))),
};

assert_eq!(data.validate_sync(), Ok(()));
}
29 changes: 28 additions & 1 deletion packages/fortifier-macros/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use convert_case::{Case, Casing};
use quote::format_ident;
use syn::Ident;
use syn::{GenericArgument, Ident, Path, PathArguments, Type};

pub fn upper_camel_ident(ident: &Ident) -> Ident {
let s = ident.to_string();
Expand All @@ -11,3 +11,30 @@ pub fn upper_camel_ident(ident: &Ident) -> Ident {
format_ident!("{}", s.to_case(Case::UpperCamel))
}
}

pub fn path_to_string(path: &Path) -> String {
path.segments
.iter()
.map(|segment| segment.ident.to_string())
.collect::<Vec<_>>()
.join("::")
}

pub fn is_option_path(path: &Path) -> bool {
let path_string = path_to_string(path);
path_string == "Option" || path_string == "std::option::Option"
}

pub fn count_options(r#type: &Type) -> usize {
if let Type::Path(r#type) = r#type
&& let Some(segment) = r#type.path.segments.last()
&& let PathArguments::AngleBracketed(arguments) = &segment.arguments
&& arguments.args.len() == 1
&& is_option_path(&r#type.path)
&& let Some(GenericArgument::Type(argument_type)) = arguments.args.first()
{
1 + count_options(argument_type)
} else {
0
}
}
12 changes: 3 additions & 9 deletions packages/fortifier-macros/src/validate/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use syn::{
TypeParamBound, WherePredicate, punctuated::Punctuated, token::PathSep,
};

use crate::{integrations::where_predicate, validate::error::format_error_ident};
use crate::{
integrations::where_predicate, util::path_to_string, validate::error::format_error_ident,
};

/// Primitive and built-in types.
///
Expand Down Expand Up @@ -244,14 +246,6 @@ impl ValidateResult {
}
}

fn path_to_string(path: &Path) -> String {
path.segments
.iter()
.map(|segment| segment.ident.to_string())
.collect::<Vec<_>>()
.join("::")
}

fn is_validate_path(path: &Path) -> bool {
let path_string = path_to_string(path);
path_string == "Validate"
Expand Down
85 changes: 64 additions & 21 deletions packages/fortifier-macros/src/validations/custom.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote};
use syn::{Ident, LitBool, Path, Result, Type, TypePath, meta::ParseNestedMeta};
use syn::{Ident, LitBool, LitInt, Path, Result, Type, TypePath, meta::ParseNestedMeta};

use crate::{
generics::{Generic, generic_arguments},
util::upper_camel_ident,
util::{count_options, upper_camel_ident},
validation::{Execution, Validation},
};

pub struct Custom {
r#type: Type,
name: Ident,
execution: Execution,
error_type: TypePath,
function_path: Path,
execution: Execution,
context: bool,
max_options: usize,
}

impl Validation for Custom {
fn parse(_type: &Type, meta: &ParseNestedMeta<'_>) -> Result<Self> {
fn parse(r#type: &Type, meta: &ParseNestedMeta<'_>) -> Result<Self> {
let mut name = None;
let mut execution = Execution::Sync;
let mut error_type: Option<TypePath> = None;
let mut function_path: Option<Path> = None;
let mut execution = Execution::Sync;
let mut context = false;
let mut max_options = 0;

meta.parse_nested_meta(|meta| {
if meta.path.is_ident("async") {
Expand Down Expand Up @@ -54,6 +57,15 @@ impl Validation for Custom {
} else if meta.path.is_ident("function") {
function_path = Some(meta.value()?.parse()?);

Ok(())
} else if meta.path.is_ident("options") {
if let Ok(value) = meta.value() {
let lit: LitInt = value.parse()?;
max_options = lit.base10_parse::<usize>()?;
} else {
max_options = usize::MAX;
}

Ok(())
} else if meta.path.is_ident("name") {
let ident = meta.value()?.parse()?;
Expand All @@ -78,11 +90,13 @@ impl Validation for Custom {
});

Ok(Custom {
r#type: r#type.clone(),
name,
execution,
error_type,
function_path,
execution,
context,
max_options,
})
}

Expand All @@ -103,24 +117,53 @@ impl Validation for Custom {
}

fn expr(&self, execution: Execution, expr: &TokenStream) -> Option<TokenStream> {
let context_expr = self.context.then(|| quote!(, &context));

match (execution, self.execution) {
(Execution::Sync, Execution::Sync) => {
let function_path = &self.function_path;
(Execution::Sync, Execution::Sync) => Some(wrapper(
&self.r#type,
&self.function_path,
expr,
self.context,
self.max_options,
None,
)),
(Execution::Async, Execution::Async) => Some(wrapper(
&self.r#type,
&self.function_path,
expr,
self.context,
self.max_options,
Some(quote!(.await)),
)),
_ => None,
}
}
}

Some(quote! {
#function_path(&#expr #context_expr)
})
}
(Execution::Async, Execution::Async) => {
let function_path = &self.function_path;
fn wrapper(
r#type: &Type,
function_path: &Path,
expr: &TokenStream,
context: bool,
max_options: usize,
suffix: Option<TokenStream>,
) -> TokenStream {
let context_expr = context.then(|| quote!(, &context));

let count = count_options(r#type);
let remove_count = count.saturating_sub(max_options);

if remove_count > 0 {
let mut wrapper = quote!(value);
for _ in 0..remove_count {
wrapper = quote!(Some(#wrapper));
}

Some(quote! {
#function_path(&#expr #context_expr).await
})
}
_ => None,
quote! {
{ if let #wrapper = &#expr { #function_path(value #context_expr) #suffix} else { Ok(())} }
}
} else {
quote! {
#function_path(&#expr #context_expr) #suffix
}
}
}
Loading