1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! `nade` is a attribute macro that adds ***na***med and ***de***fault
//! arguments to Rust functions.
//!
//! ## Usage
//!
//! ```rust
//! use nade::nade;
//!
//! pub fn one() -> u32 {
//! 1
//! }
//!
//! #[nade]
//! pub fn foo(
//! /// You can add doc comments to the parameter. It will be shown in the doc of the macro.
//! /// The world is 42.
//! #[nade(42)]
//! a: u32,
//!
//! /// Call a function
//! #[nade(one())]
//! b: u32,
//!
//! /// Default value of u32
//! #[nade]
//! c: u32,
//!
//! d: u32,
//! ) -> u32 {
//! a + b + c + d
//! }
//!
//! assert_eq!(foo!(1, 2, 3, 4), 10); // foo(1, 2, 3, 4)
//! assert_eq!(foo!(d = 2), 45); // foo(42, one(), Default::default(), 2)
//! assert_eq!(foo!(1, c = 2, b = 3, 4), 10); // foo(1, 3, 2, 4)
//! ```
//!
//! ## How it works
//!
//! If you write a function like this:
//!
//! ```rust
//! pub fn one() -> u32 {
//! 1
//! }
//!
//! #[nade]
//! pub fn foo(#[nade(42)] a: u32, #[nade(one())] b: u32, #[nade] c: u32, d: u32) -> u32 {
//! a + b + c + d
//! }
//! ```
//!
//! it will be expanded to:
//!
//! ```rust
//! pub fn one() -> u32 {
//! 1
//! }
//!
//! pub fn foo(a: u32, b: u32, c: u32, d: u32) -> u32 {
//! a + b + c + d
//! }
//!
//! #[doc(hidden)]
//! pub mod foo {
//! pub use ::nade::helper;
//! }
//!
//! #[::nade::macro_v(pub)]
//! macro_rules! foo {
//! ($($args:tt)*) => {
//! foo::helper!(
//! ($($args)*)
//! (a = 42, b = one(), c = Default::default())
//! (foo)
//! )
//! };
//! }
//! ```
//!
//! The attribute macro `#[macro_v(pub)]` make the visibility of the declarative
//! macro the same as the function. When the visibility of the function is
//! `pub(crate)`, `#[macro_v(pub(crate))]` is also generated.
//! see [macro-v](https://github.com/ZihanType/macro-v) for details.
//!
//! Then, when you call the macro `foo` like this:
//!
//! ```rust
//! foo!(32, d = 1, c = 2);
//! ```
//!
//! it will be expanded to:
//!
//! ```rust
//! foo(32, one(), 2, 1);
//! ```
//!
//! ## Limitations
//!
//! 1. When you call the macro `foo`, you must use the `use` statement to bring
//! the macro into scope, like this:
//!
//! ```rust
//! use some_crate::foo;
//!
//! foo!(32, d = 1, c = 2);
//! ```
//!
//! Because the attribute macro `nade` will generate a macro and a mod with
//! the same name as the function, and the macro use the function and the mod,
//! so you must use the `use` statement to bring the macro, the function and the
//! mod into scope.
//!
//! 2. The default argument expression must be declared in the scope of the
//! macro call.
//!
//! Because default argument expression is expanded to the macro call site,
//! so it must be declared in the scope of the macro call.
//!
//! ## How to bypass the limitations
//!
//! 1. You can pass a module path starting with `$crate::` for the `nade`
//! attribute macro on the function, like this:
//!
//! ```rust
//! #[nade($crate::module)]
//! pub fn foo(
//! #[nade(42)]
//! a: u32,
//!
//! #[nade(one())]
//! b: u32,
//!
//! #[nade]
//! c: u32,
//!
//! d: u32
//! ) -> u32 {
//! a + b + c + d
//! }
//! ```
//!
//! it will be expanded to:
//!
//! ```rust
//! pub fn foo(a: u32, b: u32, c: u32, d: u32) -> u32 {
//! a + b + c + d
//! }
//!
//! #[doc(hidden)]
//! pub mod __foo {
//! pub use ::nade::helper;
//! }
//!
//! #[macro_v(pub)]
//! macro_rules! foo {
//! ($($args:tt)*) => {
//! $crate::module::__foo::helper!(
//! ($($args)*)
//! (a = 42, b = one(), c = Default::default())
//! ($crate::module::foo)
//! )
//! };
//! }
//! ```
//!
//! Then, you can not use the `use` statement to bring the macro into scope,
//! like this:
//!
//! ```rust
//! some_crate::foo!(32, d = 1, c = 2);
//! ```
//!
//! 2. In the `nade` attribute macro on the parameter, you can specify the
//! default argument expression using the full path, either `$crate::a::expr`,
//! or `::a::b::expr`. In fact, when you use `#[nade]` on an parameter, you are
//! using `#[nade(::core::default::Default::default())]`, like this:
//!
//! ```rust
//! pub fn one() -> u32 {
//! 1
//! }
//!
//! pub static PATH: &str = "a";
//!
//! #[nade]
//! pub fn foo<T1, T2, T3, T4>(
//! #[nade($crate::module::one())]
//! a: T1,
//!
//! #[nade(::std::path::Path::new("a"))]
//! b: T2,
//!
//! #[nade($crate::module::PATH)]
//! c: T3,
//!
//! #[nade("Hello")]
//! d: T4
//! ) {
//! let _ = (a, b, c, d);
//! }
//! ```
//!
//! ## Credits
//!
//! This crate is inspired by these crates:
//!
//! [default-args](https://github.com/buttercrab/default-args.rs)
//!
//! [duang](https://github.com/xiaoniu-578fa6bff964d005/duang)
//!
//! [leptos](https://github.com/gbj/leptos)
//!
//! [typed-builder](https://github.com/idanarye/rust-typed-builder)
pub use macro_v;
pub use helper;
pub use nade;