Skip to content

Commit

Permalink
refactor(config-macro): avoid polluting the crate namespace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ROMemories committed Mar 7, 2024
1 parent 8370086 commit 0940215
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions src/riot-rs-macros/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -77,51 +80,53 @@ pub fn config(args: TokenStream, item: TokenStream) -> TokenStream {
TokenStream::from(expanded)
}

#[derive(Default)]
struct ConfigAttributes {
kind: Option<ConfigKind>,
}
mod config_macro {
#[derive(Default)]
pub struct ConfigAttributes {
pub kind: Option<ConfigKind>,
}

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::<ConfigKind>().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::<ConfigKind>().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::<ConfigKind>()
.map(|c| format!("`{}`", c.as_name()))
.collect::<Vec<_>>()
.join(", ");
Err(meta.error(format!(
"unsupported parameter ({supported_params} are supported)",
)))
}

let supported_params = all::<ConfigKind>()
.map(|c| format!("`{}`", c.as_name()))
.collect::<Vec<_>>()
.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",
}
}
}
}

0 comments on commit 0940215

Please sign in to comment.