spiff the proc macro for user reflection in templates

This commit is contained in:
Joe Ardent 2023-09-24 12:29:26 -07:00
parent cc7afb3533
commit a09ec898f9
2 changed files with 16 additions and 2 deletions

View file

@ -19,7 +19,7 @@ pub fn derive_optional_optional_user(input: TokenStream) -> TokenStream {
})
.is_some();
let (use_any, user_is_option_user) = if has_user {
let (use_any, user_is_option_user, user_is_mandatory) = if has_user {
(
quote!(
use ::std::any::Any;
@ -28,9 +28,10 @@ pub fn derive_optional_optional_user(input: TokenStream) -> TokenStream {
::std::any::TypeId::of::<::std::option::Option<crate::User>>()
== self.user.type_id()
),
quote!(::std::any::TypeId::of::<crate::User>() == self.user.type_id()),
)
} else {
(quote!(), quote!(false))
(quote!(), quote!(false), quote!(false))
};
let output = quote!(
@ -39,6 +40,15 @@ pub fn derive_optional_optional_user(input: TokenStream) -> TokenStream {
#use_any
#user_is_option_user
}
pub fn has_mandatory_user(&self) -> bool {
#use_any
#user_is_mandatory
}
pub fn has_user(&self) -> bool {
self.has_optional_user() || self.has_mandatory_user()
}
}
);

View file

@ -97,6 +97,7 @@ mod test {
#[test]
fn main_page_has_optional_user() {
assert!(MainPage::default().has_optional_user());
assert!(MainPage::default().has_user());
}
#[test]
@ -111,6 +112,8 @@ mod test {
user: User,
}
assert!(!TestThing::default().has_optional_user());
assert!(TestThing::default().has_mandatory_user());
assert!(TestThing::default().has_user());
}
#[test]
@ -120,5 +123,6 @@ mod test {
user: Option<bool>,
}
assert!(!TestThing::default().has_optional_user());
assert!(!TestThing::default().has_user());
}
}