From a3ccc2713bb5315123814cadd6c50275eee38e1c Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 3 Aug 2023 15:09:46 +0200 Subject: feat: add constructor name flag to injectable macro --- macros/src/factory/declare_default_args.rs | 32 ++++++++++++++++++++---------- macros/src/factory/macro_args.rs | 32 ++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 22 deletions(-) (limited to 'macros/src/factory') diff --git a/macros/src/factory/declare_default_args.rs b/macros/src/factory/declare_default_args.rs index 269ef8f..f93d29d 100644 --- a/macros/src/factory/declare_default_args.rs +++ b/macros/src/factory/declare_default_args.rs @@ -31,12 +31,11 @@ impl Parse for DeclareDefaultFactoryMacroArgs let flags = Punctuated::::parse_terminated(input)?; for flag in &flags { - let flag_str = flag.flag.to_string(); + let name = flag.name().to_string(); - if !FACTORY_MACRO_FLAGS.contains(&flag_str.as_str()) { + if !FACTORY_MACRO_FLAGS.contains(&name.as_str()) { return Err(input.error(format!( - "Unknown flag '{}'. Expected one of [ {} ]", - flag_str, + "Unknown flag '{name}'. Expected one of [ {} ]", FACTORY_MACRO_FLAGS.join(",") ))); } @@ -44,7 +43,7 @@ impl Parse for DeclareDefaultFactoryMacroArgs let flag_names = flags .iter() - .map(|flag| flag.flag.to_string()) + .map(|flag| flag.name().to_string()) .collect::>(); if let Some((dupe_flag_name, _)) = flag_names.iter().find_duplicate() { @@ -65,6 +64,7 @@ mod tests use syn::token::Dyn; use syn::{ parse2, + Lit, LitBool, Path, PathArguments, @@ -77,6 +77,7 @@ mod tests }; use super::*; + use crate::macro_flag::MacroFlagValue; #[test] fn can_parse_with_interface_only() -> Result<(), Box> @@ -142,8 +143,11 @@ mod tests assert_eq!( dec_def_fac_args.flags, Punctuated::from_iter(vec![MacroFlag { - flag: format_ident!("threadsafe"), - is_on: LitBool::new(true, Span::call_site()) + name: format_ident!("threadsafe"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + true, + Span::call_site() + ))) }]) ); @@ -182,12 +186,18 @@ mod tests dec_def_fac_args.flags, Punctuated::from_iter(vec![ MacroFlag { - flag: format_ident!("threadsafe"), - is_on: LitBool::new(true, Span::call_site()) + name: format_ident!("threadsafe"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + true, + Span::call_site() + ))) }, MacroFlag { - flag: format_ident!("async"), - is_on: LitBool::new(false, Span::call_site()) + name: format_ident!("async"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + false, + Span::call_site() + ))) } ]) ); diff --git a/macros/src/factory/macro_args.rs b/macros/src/factory/macro_args.rs index 8acbdb6..cb2cbc9 100644 --- a/macros/src/factory/macro_args.rs +++ b/macros/src/factory/macro_args.rs @@ -19,12 +19,12 @@ impl Parse for FactoryMacroArgs let flags = Punctuated::::parse_terminated(input)?; for flag in &flags { - let flag_str = flag.flag.to_string(); + let name = flag.name().to_string(); - if !FACTORY_MACRO_FLAGS.contains(&flag_str.as_str()) { + if !FACTORY_MACRO_FLAGS.contains(&name.as_str()) { return Err(input.error(format!( "Unknown flag '{}'. Expected one of [ {} ]", - flag_str, + name, FACTORY_MACRO_FLAGS.join(",") ))); } @@ -32,7 +32,7 @@ impl Parse for FactoryMacroArgs let flag_names = flags .iter() - .map(|flag| flag.flag.to_string()) + .map(|flag| flag.name().to_string()) .collect::>(); if let Some((dupe_flag_name, _)) = flag_names.iter().find_duplicate() { @@ -50,9 +50,10 @@ mod tests use proc_macro2::Span; use quote::{format_ident, quote}; - use syn::{parse2, LitBool}; + use syn::{parse2, Lit, LitBool}; use super::*; + use crate::macro_flag::MacroFlagValue; #[test] fn can_parse_with_single_flag() -> Result<(), Box> @@ -66,8 +67,11 @@ mod tests assert_eq!( factory_macro_args.flags, Punctuated::from_iter(vec![MacroFlag { - flag: format_ident!("async"), - is_on: LitBool::new(true, Span::call_site()) + name: format_ident!("async"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + true, + Span::call_site() + ))) }]) ); @@ -87,12 +91,18 @@ mod tests factory_macro_args.flags, Punctuated::from_iter(vec![ MacroFlag { - flag: format_ident!("async"), - is_on: LitBool::new(true, Span::call_site()) + name: format_ident!("async"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + true, + Span::call_site() + ))) }, MacroFlag { - flag: format_ident!("threadsafe"), - is_on: LitBool::new(false, Span::call_site()) + name: format_ident!("threadsafe"), + value: MacroFlagValue::Literal(Lit::Bool(LitBool::new( + false, + Span::call_site() + ))) } ]) ); -- cgit v1.2.3-18-g5258