aboutsummaryrefslogtreecommitdiff
path: root/macros/src/decl_def_factory_args.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-31 21:41:27 +0200
committerHampusM <hampus@hampusmat.com>2022-08-31 21:41:27 +0200
commita9ff2f16812b56107604400a64a7f482d017eca1 (patch)
tree8b562fbe95c0356ab4ff85121df4720fcc00783a /macros/src/decl_def_factory_args.rs
parent576111c38dbb80ff535636e6120db610842d81f1 (diff)
feat: add a threadsafe flag to the declare_default_factory macro
Diffstat (limited to 'macros/src/decl_def_factory_args.rs')
-rw-r--r--macros/src/decl_def_factory_args.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/macros/src/decl_def_factory_args.rs b/macros/src/decl_def_factory_args.rs
new file mode 100644
index 0000000..6450583
--- /dev/null
+++ b/macros/src/decl_def_factory_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<MacroFlag, Token![,]>,
+}
+
+impl Parse for DeclareDefaultFactoryMacroArgs
+{
+ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self>
+ {
+ let interface = input.parse().unwrap();
+
+ if !input.peek(Token![,]) {
+ return Ok(Self {
+ interface,
+ flags: Punctuated::new(),
+ });
+ }
+
+ input.parse::<Token![,]>().unwrap();
+
+ let flags = Punctuated::<MacroFlag, Token![,]>::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::<Vec<_>>();
+
+ 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 })
+ }
+}