Skip to content

Commit 271e811

Browse files
committed
move to macros rs
1 parent d2d09f5 commit 271e811

File tree

3 files changed

+156
-135
lines changed

3 files changed

+156
-135
lines changed

crates/utils/src/attr/types.rs

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,4 @@
1-
/*
2-
A macro to create a single attribute enum
3-
*/
4-
macro_rules! attr_enum {
5-
(
6-
enum $name:ident;
7-
pub {
8-
$(
9-
$(#[$pub_meta:meta])*
10-
$pub:ident: $pub_str:literal,
11-
)*
12-
};
13-
priv {
14-
$(
15-
$(#[$priv_meta:meta])*
16-
$priv:ident,
17-
)*
18-
};
19-
) => {
20-
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, Eq, Hash)]
21-
pub enum $name {
22-
$(
23-
$(#[$pub_meta])*
24-
#[strum(serialize = $pub_str)]
25-
$pub,
26-
)*
27-
$(
28-
$(#[$priv_meta])*
29-
#[strum(disabled)]
30-
$priv,
31-
)*
32-
}
33-
};
34-
(enum $name:ident;) => {
35-
attr_enum! {
36-
enum $name;
37-
pub {};
38-
priv {};
39-
}
40-
};
41-
(enum $name:ident;
42-
pub {
43-
$(
44-
$(#[$pub_meta:meta])*
45-
$pub:ident: $pub_str:literal,
46-
)*
47-
};
48-
) => {
49-
attr_enum! {
50-
enum $name;
51-
pub {
52-
$(
53-
$(#[$pub_meta])*
54-
$pub: $pub_str,
55-
)*
56-
};
57-
priv {};
58-
}
59-
};
60-
(enum $name:ident;
61-
priv {
62-
$(
63-
$(#[$priv_meta:meta])*
64-
$priv:ident,
65-
)*
66-
};
67-
) => {
68-
attr_enum! {
69-
enum $name;
70-
pub {};
71-
priv {
72-
$(
73-
$(#[$priv_meta])*
74-
$priv,
75-
)*
76-
};
77-
}
78-
};
79-
}
80-
81-
/*
82-
We can define a set of attributes as follows:
83-
```rust
84-
attr_set! {
85-
set_name;
86-
bool {
87-
pub:
88-
/// This is a toplevel component
89-
TopLevel: "top_level",
90-
/// Use a counter based FSM design
91-
CounterFSM: "counter_fsm",
92-
93-
priv:
94-
/// this is a private flag
95-
HiddenFlag,
96-
};
97-
num {
98-
pub:
99-
/// Example numerical attribute
100-
ExampleNum: "example_num",
101-
priv:
102-
/// Private numerical attribute
103-
HiddenNum,
104-
};
105-
}
106-
*/
107-
macro_rules! attr_set {
108-
(
109-
$module:ident;
110-
flag {
111-
$(
112-
$flag_tokens:tt
113-
)*
114-
};
115-
numeric {
116-
$(
117-
$num_tokens:tt
118-
)*
119-
};
120-
) => {
121-
pub mod $module {
122-
attr_enum! {
123-
enum Bool;
124-
$($flag_tokens)*
125-
}
126-
127-
attr_enum! {
128-
enum Num;
129-
$($num_tokens)*
130-
}
131-
132-
pub type Attrs = crate::Attributes<Bool, Num>;
133-
}
134-
};
135-
}
1+
use crate::attr_set;
1362

1373
attr_set! {
1384
comp_attrs;

crates/utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod errors;
33
mod global_sym;
44
mod gsym;
55
mod id;
6+
mod macros;
67
mod math;
78
mod position;
89
mod reporter;

crates/utils/src/macros.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#[macro_export]
2+
/*
3+
A macro to create a single attribute enum. Automatically derives [FromString] for the enum using the [strum] crate. Supports private flags that cannot be derived from a string.
4+
5+
Example usage:
6+
```rust
7+
attr_enum! {
8+
enum Bool;
9+
pub {
10+
/// This is a toplevel component
11+
TopLevel: "top_level",
12+
/// Use a counter based FSM design
13+
CounterFSM: "counter_fsm",
14+
};
15+
priv {
16+
/// this is a private flag
17+
HiddenFlag,
18+
};
19+
}
20+
```
21+
*/
22+
macro_rules! attr_enum {
23+
(
24+
enum $name:ident;
25+
pub {
26+
$(
27+
$(#[$pub_meta:meta])*
28+
$pub:ident: $pub_str:literal,
29+
)*
30+
};
31+
priv {
32+
$(
33+
$(#[$priv_meta:meta])*
34+
$priv:ident,
35+
)*
36+
};
37+
) => {
38+
#[derive(Clone, Copy, PartialEq, strum_macros::EnumString, Eq, Hash)]
39+
pub enum $name {
40+
$(
41+
$(#[$pub_meta])*
42+
#[strum(serialize = $pub_str)]
43+
$pub,
44+
)*
45+
$(
46+
$(#[$priv_meta])*
47+
#[strum(disabled)]
48+
$priv,
49+
)*
50+
}
51+
};
52+
(enum $name:ident;) => {
53+
$crate::attr_enum! {
54+
enum $name;
55+
pub {};
56+
priv {};
57+
}
58+
};
59+
(enum $name:ident;
60+
pub {
61+
$(
62+
$(#[$pub_meta:meta])*
63+
$pub:ident: $pub_str:literal,
64+
)*
65+
};
66+
) => {
67+
$crate::attr_enum! {
68+
enum $name;
69+
pub {
70+
$(
71+
$(#[$pub_meta])*
72+
$pub: $pub_str,
73+
)*
74+
};
75+
priv {};
76+
}
77+
};
78+
(enum $name:ident;
79+
priv {
80+
$(
81+
$(#[$priv_meta:meta])*
82+
$priv:ident,
83+
)*
84+
};
85+
) => {
86+
attr_enum! {
87+
enum $name;
88+
pub {};
89+
priv {
90+
$(
91+
$(#[$priv_meta])*
92+
$priv,
93+
)*
94+
};
95+
}
96+
};
97+
}
98+
99+
#[macro_export]
100+
/*
101+
We can define a set of attributes as follows:
102+
```rust
103+
attr_set! {
104+
set_name;
105+
bool {
106+
pub:
107+
/// This is a toplevel component
108+
TopLevel: "top_level",
109+
/// Use a counter based FSM design
110+
CounterFSM: "counter_fsm",
111+
112+
priv:
113+
/// this is a private flag
114+
HiddenFlag,
115+
};
116+
num {
117+
pub:
118+
/// Example numerical attribute
119+
ExampleNum: "example_num",
120+
priv:
121+
/// Private numerical attribute
122+
HiddenNum,
123+
};
124+
}
125+
*/
126+
macro_rules! attr_set {
127+
(
128+
$module:ident;
129+
flag {
130+
$(
131+
$flag_tokens:tt
132+
)*
133+
};
134+
numeric {
135+
$(
136+
$num_tokens:tt
137+
)*
138+
};
139+
) => {
140+
pub mod $module {
141+
$crate::attr_enum! {
142+
enum Bool;
143+
$($flag_tokens)*
144+
}
145+
146+
$crate::attr_enum! {
147+
enum Num;
148+
$($num_tokens)*
149+
}
150+
151+
pub type Attrs = $crate::Attributes<Bool, Num>;
152+
}
153+
};
154+
}

0 commit comments

Comments
 (0)