From 0940215e9ba991bdd91d67c4d922b39cdc973f9e Mon Sep 17 00:00:00 2001 From: ROMemories Date: Thu, 7 Mar 2024 10:47:01 +0100 Subject: [PATCH] refactor(config-macro): avoid polluting the crate namespace As the `config` macro file is `include!`d into the crate root, we move the required items in a module dedicated to that macro to avoid polluting the crate namespace. --- src/riot-rs-macros/src/config.rs | 77 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/riot-rs-macros/src/config.rs b/src/riot-rs-macros/src/config.rs index 72b0d6f81..7cbfb1d1e 100644 --- a/src/riot-rs-macros/src/config.rs +++ b/src/riot-rs-macros/src/config.rs @@ -37,6 +37,9 @@ #[allow(clippy::missing_panics_doc)] #[proc_macro_attribute] pub fn config(args: TokenStream, item: TokenStream) -> TokenStream { + #[allow(clippy::wildcard_imports)] + use config_macro::*; + use quote::{format_ident, quote}; let mut attrs = ConfigAttributes::default(); @@ -77,51 +80,53 @@ pub fn config(args: TokenStream, item: TokenStream) -> TokenStream { TokenStream::from(expanded) } -#[derive(Default)] -struct ConfigAttributes { - kind: Option, -} +mod config_macro { + #[derive(Default)] + pub struct ConfigAttributes { + pub kind: Option, + } -impl ConfigAttributes { - fn parse(&mut self, meta: &syn::meta::ParseNestedMeta) -> syn::Result<()> { - use enum_iterator::all; + impl ConfigAttributes { + pub fn parse(&mut self, meta: &syn::meta::ParseNestedMeta) -> syn::Result<()> { + use enum_iterator::all; - for (config_name, kind) in all::().map(|c| (c.as_name(), c)) { - if meta.path.is_ident(config_name) { - self.check_only_one_kind(config_name); - self.kind = Some(kind); - return Ok(()); + for (config_name, kind) in all::().map(|c| (c.as_name(), c)) { + if meta.path.is_ident(config_name) { + self.check_only_one_kind(config_name); + self.kind = Some(kind); + return Ok(()); + } } + + let supported_params = all::() + .map(|c| format!("`{}`", c.as_name())) + .collect::>() + .join(", "); + Err(meta.error(format!( + "unsupported parameter ({supported_params} are supported)", + ))) } - let supported_params = all::() - .map(|c| format!("`{}`", c.as_name())) - .collect::>() - .join(", "); - Err(meta.error(format!( - "unsupported parameter ({supported_params} are supported)", - ))) + fn check_only_one_kind(&self, param: &str) { + assert!( + self.kind.is_none(), + "a separate function is required for `{param}` configuration", + ); + } } - fn check_only_one_kind(&self, param: &str) { - assert!( - self.kind.is_none(), - "a separate function is required for `{param}` configuration", - ); + #[derive(Debug, enum_iterator::Sequence)] + pub enum ConfigKind { + Network, + Usb, } -} - -#[derive(Debug, enum_iterator::Sequence)] -enum ConfigKind { - Network, - Usb, -} -impl ConfigKind { - fn as_name(&self) -> &'static str { - match self { - Self::Network => "network", - Self::Usb => "usb", + impl ConfigKind { + pub fn as_name(&self) -> &'static str { + match self { + Self::Network => "network", + Self::Usb => "usb", + } } } }