From 695f90bf900015df1e2728445f833dabced838a9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 24 Sep 2022 13:13:20 +0200 Subject: refactor: reorganize modules in the macros crate --- macros/src/factory/declare_default_args.rs | 56 ++++++++++++++++++++++++++++++ macros/src/factory/macro_args.rs | 44 +++++++++++++++++++++++ macros/src/factory/mod.rs | 3 ++ macros/src/factory/type_alias.rs | 35 +++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 macros/src/factory/declare_default_args.rs create mode 100644 macros/src/factory/macro_args.rs create mode 100644 macros/src/factory/mod.rs create mode 100644 macros/src/factory/type_alias.rs (limited to 'macros/src/factory') diff --git a/macros/src/factory/declare_default_args.rs b/macros/src/factory/declare_default_args.rs new file mode 100644 index 0000000..6450583 --- /dev/null +++ b/macros/src/factory/declare_default_args.rs @@ -0,0 +1,56 @@ +use syn::parse::Parse; +use syn::punctuated::Punctuated; +use syn::{Token, Type}; + +use crate::macro_flag::MacroFlag; +use crate::util::iterator_ext::IteratorExt; + +pub const FACTORY_MACRO_FLAGS: &[&str] = &["threadsafe"]; + +pub struct DeclareDefaultFactoryMacroArgs +{ + pub interface: Type, + pub flags: Punctuated, +} + +impl Parse for DeclareDefaultFactoryMacroArgs +{ + fn parse(input: syn::parse::ParseStream) -> syn::Result + { + let interface = input.parse().unwrap(); + + if !input.peek(Token![,]) { + return Ok(Self { + interface, + flags: Punctuated::new(), + }); + } + + input.parse::().unwrap(); + + let flags = Punctuated::::parse_terminated(input)?; + + for flag in &flags { + let flag_str = flag.flag.to_string(); + + if !FACTORY_MACRO_FLAGS.contains(&flag_str.as_str()) { + return Err(input.error(format!( + "Unknown flag '{}'. Expected one of [ {} ]", + flag_str, + FACTORY_MACRO_FLAGS.join(",") + ))); + } + } + + let flag_names = flags + .iter() + .map(|flag| flag.flag.to_string()) + .collect::>(); + + if let Some(dupe_flag_name) = flag_names.iter().find_duplicate() { + return Err(input.error(format!("Duplicate flag '{}'", dupe_flag_name))); + } + + Ok(Self { interface, flags }) + } +} diff --git a/macros/src/factory/macro_args.rs b/macros/src/factory/macro_args.rs new file mode 100644 index 0000000..dd80c1c --- /dev/null +++ b/macros/src/factory/macro_args.rs @@ -0,0 +1,44 @@ +use syn::parse::Parse; +use syn::punctuated::Punctuated; +use syn::Token; + +use crate::macro_flag::MacroFlag; +use crate::util::iterator_ext::IteratorExt; + +pub const FACTORY_MACRO_FLAGS: &[&str] = &["threadsafe", "async"]; + +pub struct FactoryMacroArgs +{ + pub flags: Punctuated, +} + +impl Parse for FactoryMacroArgs +{ + fn parse(input: syn::parse::ParseStream) -> syn::Result + { + let flags = Punctuated::::parse_terminated(input)?; + + for flag in &flags { + let flag_str = flag.flag.to_string(); + + if !FACTORY_MACRO_FLAGS.contains(&flag_str.as_str()) { + return Err(input.error(format!( + "Unknown flag '{}'. Expected one of [ {} ]", + flag_str, + FACTORY_MACRO_FLAGS.join(",") + ))); + } + } + + let flag_names = flags + .iter() + .map(|flag| flag.flag.to_string()) + .collect::>(); + + if let Some(dupe_flag_name) = flag_names.iter().find_duplicate() { + return Err(input.error(format!("Duplicate flag '{}'", dupe_flag_name))); + } + + Ok(Self { flags }) + } +} diff --git a/macros/src/factory/mod.rs b/macros/src/factory/mod.rs new file mode 100644 index 0000000..a8947c5 --- /dev/null +++ b/macros/src/factory/mod.rs @@ -0,0 +1,3 @@ +pub mod declare_default_args; +pub mod macro_args; +pub mod type_alias; diff --git a/macros/src/factory/type_alias.rs b/macros/src/factory/type_alias.rs new file mode 100644 index 0000000..64afe57 --- /dev/null +++ b/macros/src/factory/type_alias.rs @@ -0,0 +1,35 @@ +use quote::ToTokens; +use syn::parse::{Parse, ParseStream}; +use syn::punctuated::Punctuated; +use syn::{parse, ItemType, Token, Type}; + +use crate::fn_trait::FnTrait; + +pub struct FactoryTypeAlias +{ + pub type_alias: ItemType, + pub factory_interface: FnTrait, + pub arg_types: Punctuated, + pub return_type: Type, +} + +impl Parse for FactoryTypeAlias +{ + fn parse(input: ParseStream) -> syn::Result + { + let type_alias = match input.parse::() { + Ok(type_alias) => Ok(type_alias), + Err(_) => Err(input.error("Expected a type alias")), + }?; + + let aliased_fn_trait = + parse::(type_alias.ty.as_ref().to_token_stream().into())?; + + Ok(Self { + type_alias, + factory_interface: aliased_fn_trait.clone(), + arg_types: aliased_fn_trait.inputs, + return_type: aliased_fn_trait.output, + }) + } +} -- cgit v1.2.3-18-g5258