Skip to content

Commit 2d12e1d

Browse files
authored
refactor: add SubEnum and EnumFromInner proc macros (#1109)
Adds two new proc macros, `SubEnum` and `EnumFromInner`, to generate `Container` subenum types and conversion implementations.
1 parent 6242f8a commit 2d12e1d

File tree

18 files changed

+1855
-205
lines changed

18 files changed

+1855
-205
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ tokio-tungstenite = "0.26"
1616
tracing = "0.1"
1717
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1818
uuid = { version = "1", features = ["v4", "serde"] }
19+
20+
wm-macros = { path = "packages/wm-macros" }

packages/wm-macros/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "wm-macros"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
syn = "2.0.103"
11+
quote = "1.0"
12+
proc-macro2 = "1.0"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Utilities for working with [syn::Attribute]
2+
3+
pub mod prelude {
4+
pub use super::FindAttributes;
5+
}
6+
7+
/// Trait for filtering lists of [syn::Attribute] by name and type.
8+
pub trait FindAttributes {
9+
/// Find attributes by name. E.g. `#[name]`, `#[name(...)]` or `#[name =
10+
/// <value>]`
11+
fn find_attrs(
12+
&self,
13+
name: &str,
14+
) -> impl Iterator<Item = &syn::Attribute>;
15+
/// Find list attributes by name. E.g. `#[name(...)]`
16+
fn find_list_attrs(
17+
&self,
18+
name: &str,
19+
) -> impl Iterator<Item = &syn::MetaList> {
20+
self
21+
.find_attrs(name)
22+
.filter_map(|attr| attr.meta.require_list().ok())
23+
}
24+
}
25+
26+
impl FindAttributes for Vec<syn::Attribute> {
27+
fn find_attrs(
28+
&self,
29+
name: &str,
30+
) -> impl Iterator<Item = &syn::Attribute> {
31+
self.iter().filter(move |attr| {
32+
attr.path().get_ident().is_some_and(|ident| ident == name)
33+
})
34+
}
35+
}
36+
37+
impl FindAttributes for &[syn::Attribute] {
38+
fn find_attrs(
39+
&self,
40+
name: &str,
41+
) -> impl Iterator<Item = &syn::Attribute> {
42+
self.iter().filter(move |attr| {
43+
attr.path().get_ident().is_some_and(|ident| ident == name)
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)