Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to generate associated consts #194

Closed
Closed
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
10 changes: 8 additions & 2 deletions nutype_macros/src/any/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use syn::{parse_quote, Generics};

use crate::common::{
gen::{
tests::gen_test_should_have_valid_default_value, traits::GeneratedTraits, GenerateNewtype,
tests::{gen_associated_consts_should_be_valid, gen_test_should_have_valid_default_value},
traits::GeneratedTraits,
GenerateNewtype,
},
models::{ErrorTypePath, Guard, TypeName, TypedCustomFunction},
models::{ConstAssign, ErrorTypePath, Guard, TypeName, TypedCustomFunction},
};

use self::error::gen_validation_error_type;
Expand Down Expand Up @@ -138,16 +140,20 @@ impl GenerateNewtype for AnyNewtype {
maybe_default_value: &Option<syn::Expr>,
guard: &Guard<Self::Sanitizer, Self::Validator>,
_traits: &HashSet<Self::TypedTrait>,
associated_consts: &[ConstAssign],
) -> TokenStream {
let test_valid_default_value = gen_test_should_have_valid_default_value(
type_name,
generics,
maybe_default_value,
guard.has_validation(),
);
let test_associated_consts =
gen_associated_consts_should_be_valid(type_name, associated_consts);

quote! {
#test_valid_default_value
#test_associated_consts
}
}
}
2 changes: 2 additions & 0 deletions nutype_macros/src/any/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn parse_attributes(
new_unchecked,
default,
derive_traits,
associated_consts,
} = attrs;
let raw_guard = AnyRawGuard {
sanitizers,
Expand All @@ -39,6 +40,7 @@ pub fn parse_attributes(
guard,
default,
derive_traits,
associated_consts,
})
}

Expand Down
33 changes: 33 additions & 0 deletions nutype_macros/src/common/gen/associated_consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::Generics;

use crate::common::{
gen::strip_trait_bounds_on_generics,
models::{ConstAssign, TypeName},
};

pub fn gen_associated_consts(
type_name: &TypeName,
generics: &Generics,
associated_consts: &[ConstAssign],
) -> TokenStream {
let generics_without_bounds = strip_trait_bounds_on_generics(generics);
let consts = associated_consts.iter().map(
|ConstAssign {
const_name,
const_value,
..
}| {
quote! {
pub const #const_name: Self = Self(#const_value);
}
},
);

quote! {
impl #generics #type_name #generics_without_bounds {
#(#consts)*
}
}
}
26 changes: 21 additions & 5 deletions nutype_macros/src/common/gen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod associated_consts;
pub mod error;
pub mod new_unchecked;
pub mod parse_error;
Expand All @@ -10,11 +11,14 @@ use std::collections::HashSet;
use self::traits::GeneratedTraits;

use super::models::{
CustomFunction, ErrorTypePath, GenerateParams, Guard, NewUnchecked, ParseErrorTypeName,
TypeName, TypeTrait,
ConstAssign, CustomFunction, ErrorTypePath, GenerateParams, Guard, NewUnchecked,
ParseErrorTypeName, TypeName, TypeTrait,
};
use crate::common::{
gen::{new_unchecked::gen_new_unchecked, parse_error::gen_parse_error_name},
gen::{
associated_consts::gen_associated_consts, new_unchecked::gen_new_unchecked,
parse_error::gen_parse_error_name,
},
models::{ModuleName, Validation},
};
use proc_macro2::{Punct, Spacing, TokenStream, TokenTree};
Expand Down Expand Up @@ -351,6 +355,7 @@ pub trait GenerateNewtype {
inner_type: &Self::InnerType,
guard: &Guard<Self::Sanitizer, Self::Validator>,
new_unchecked: NewUnchecked,
associated_consts: &[ConstAssign],
) -> TokenStream {
let impl_new = match guard {
Guard::WithoutValidation { sanitizers } => {
Expand All @@ -363,11 +368,13 @@ pub trait GenerateNewtype {
};
let impl_into_inner = gen_impl_into_inner(type_name, generics, inner_type);
let impl_new_unchecked = gen_new_unchecked(type_name, inner_type, new_unchecked);
let impl_associated_consts = gen_associated_consts(type_name, generics, associated_consts);

quote! {
#impl_new
#impl_into_inner
#impl_new_unchecked
#impl_associated_consts
}
}

Expand All @@ -389,11 +396,18 @@ pub trait GenerateNewtype {
maybe_default_value,
inner_type,
generics,
associated_consts,
} = params;

let module_name = gen_module_name_for_type(&type_name);
let implementation =
Self::gen_implementation(&type_name, &generics, &inner_type, &guard, new_unchecked);
let implementation = Self::gen_implementation(
&type_name,
&generics,
&inner_type,
&guard,
new_unchecked,
&associated_consts,
);

let has_from_str_trait = traits.iter().any(|t| t.is_from_str());
let maybe_parse_error_type_path = if has_from_str_trait && Self::HAS_DEDICATED_PARSE_ERROR {
Expand All @@ -409,6 +423,7 @@ pub trait GenerateNewtype {
&maybe_default_value,
&guard,
&traits,
&associated_consts,
);

let maybe_reimported_error_type_path = match &guard {
Expand Down Expand Up @@ -472,6 +487,7 @@ pub trait GenerateNewtype {
maybe_default_value: &Option<syn::Expr>,
guard: &Guard<Self::Sanitizer, Self::Validator>,
traits: &HashSet<Self::TypedTrait>,
associated_consts: &[ConstAssign],
) -> TokenStream;
}

Expand Down
37 changes: 35 additions & 2 deletions nutype_macros/src/common/gen/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use quote::{format_ident, quote, ToTokens};
use syn::Generics;

use crate::common::models::{NumericBound, TypeName};
use crate::common::models::{ConstAssign, NumericBound, TypeName};

pub fn gen_test_should_have_consistent_lower_and_upper_boundaries<Validator>(
type_name: &TypeName,
Expand Down Expand Up @@ -73,3 +73,36 @@ Note: the test is generated automatically by #[nutype] macro
}
))
}

pub fn gen_associated_consts_should_be_valid(
type_name: &TypeName,
associated_consts: &[ConstAssign],
) -> TokenStream {
// quote! {}
let tests_consts = associated_consts.iter().map(
|ConstAssign {
const_name,
const_value,
..
}| {
let err_msg = format!(
"
Type `{type_name}` has invalid associated const `{const_name}` of value `{const_value}`.
Note: the test is generated automatically by #[nutype] macro
"
);

let test_name = format_ident!("associated_const_{const_name}_should_have_valid_value");
quote! {
#[test]
fn #test_name () {
let inner_value = #type_name::#const_name.into_inner();

#type_name::try_new(inner_value).expect(#err_msg);
}
}
},
);

quote! { #(#tests_consts)* }
}
Loading
Loading