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
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ all-features = true
[advisories]
ignore = [
{ id = "RUSTSEC-2024-0436", reason = "No maintained version available for `paste`." },
{ id = "RUSTSEC-2025-0141", reason = "No maintained version available for `bincode`." },
]

[bans]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct RangeData {
exclusive_min_max: usize,
#[validate(range(min = 1, exclusive_max = 7))]
min_exclusive_max: usize,

#[validate(range(min = 1))]
one_option: Option<usize>,
#[validate(range(min = 1))]
two_options: Option<Option<usize>>,
}

fn main() {
Expand All @@ -30,6 +35,9 @@ fn main() {
exclusive_min_exclusive_max: 1,
exclusive_min_max: 2,
min_exclusive_max: 2,

one_option: Some(1),
two_options: Some(Some(1)),
};

assert_eq!(
Expand Down
18 changes: 17 additions & 1 deletion packages/fortifier-macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub fn path_to_string(path: &Path) -> String {

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

pub fn count_options(r#type: &Type) -> usize {
Expand All @@ -38,3 +40,17 @@ pub fn count_options(r#type: &Type) -> usize {
0
}
}

pub fn strip_options(r#type: &Type) -> &Type {
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()
{
strip_options(argument_type)
} else {
r#type
}
}
3 changes: 2 additions & 1 deletion packages/fortifier-macros/src/validations/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::{Expr, Ident, Result, Type, meta::ParseNestedMeta};

use crate::{
generics::Generic,
util::strip_options,
validation::{Execution, Validation},
};

Expand Down Expand Up @@ -66,7 +67,7 @@ impl Validation for Range {
}

fn error_type(&self) -> TokenStream {
let r#type = &self.r#type;
let r#type = strip_options(&self.r#type);

quote!(::fortifier::RangeError<#r#type>)
}
Expand Down