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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use ;
/// A location of file or directory of a project.
///
/// It must be implemented by application since it is project specific.
/// See [example implementation](https://github.com/alexfedoseev/steward/tree/master/examples/cli/loc.rs) in the repository.
/// Generates location functions for a project directory structure.
///
/// This macro creates public functions that return `Loc` instances for each path
/// in your project. It requires a `ROOT` static and a `Loc` type to be defined
/// in the same scope.
///
/// # Syntax
///
/// ```ignore
/// loc! {
/// src, // fn src() -> Loc
/// ".env": env_file, // fn env_file() -> Loc for ".env"
/// src => { // fn src() + nested children
/// lib, // fn lib() -> Loc for "src/lib"
/// bin => { ... }, // fn bin() + further nesting
/// },
/// apps: web_apps => { ... }, // fn web_apps() + nested children
/// target: _ => { debug }, // skip parent function, process children only
/// }
/// ```
///
/// # Requirements
///
/// The macro expects the following to be defined in scope:
/// - `ROOT` - A static that dereferences to `Loc`
/// - `Loc` - A type that implements [`Location`] trait
///
/// # Examples
///
/// Basic usage with flat structure:
///
/// ```ignore
/// loc! {
/// src, // fn src() -> Loc { ROOT.join("src") }
/// target, // fn target() -> Loc { ROOT.join("target") }
/// "Cargo.toml", // fn Cargo.toml is invalid, use renaming:
/// }
/// ```
///
/// Renaming paths to valid function names:
///
/// ```ignore
/// loc! {
/// "Cargo.toml": cargo_toml, // fn cargo_toml() -> Loc { ROOT.join("Cargo.toml") }
/// ".env": env_file, // fn env_file() -> Loc { ROOT.join(".env") }
/// }
/// ```
///
/// Nested directory structure:
///
/// ```ignore
/// loc! {
/// src => {
/// lib, // fn lib() -> Loc { ROOT.join("src/lib") }
/// bin, // fn bin() -> Loc { ROOT.join("src/bin") }
/// },
/// tests => {
/// unit, // fn unit() -> Loc { ROOT.join("tests/unit") }
/// integration,
/// },
/// }
/// ```
///
/// Skipping function generation with `_` (useful for intermediate directories):
///
/// ```ignore
/// loc! {
/// node_modules: _ => { // No function for node_modules itself
/// ".bin": node_bin, // fn node_bin() -> Loc { ROOT.join("node_modules/.bin") }
/// },
/// }
/// ```
///
/// Complete example:
///
/// ```ignore
/// use std::{
/// env,
/// path::{Path, PathBuf},
/// sync::LazyLock,
/// };
///
/// use steward::Location;
///
/// #[derive(Clone, Debug)]
/// pub struct Loc(PathBuf);
///
/// static ROOT: LazyLock<Loc> = LazyLock::new(Loc::find_root);
///
/// loc! {
/// src => {
/// main,
/// lib,
/// },
/// "Cargo.toml": cargo_toml,
/// ".gitignore": gitignore,
/// target: _ => {
/// debug,
/// release,
/// },
/// }
///
/// impl Loc {
/// fn find_root() -> Loc {
/// const ROOT_MARKER: &str = "Cargo.lock";
///
/// let cwd = env::current_dir().expect("Failed to get cwd");
///
/// fn traverse(dir: PathBuf) -> Loc {
/// if dir.join(ROOT_MARKER).exists() {
/// Loc(dir)
/// } else {
/// traverse(
/// dir.parent()
/// .expect("Failed to find root marker")
/// .to_path_buf(),
/// )
/// }
/// }
///
/// traverse(cwd)
/// }
/// }
///
/// impl Location for Loc {
/// fn apex() -> Self { ROOT.clone() }
/// fn as_path(&self) -> &PathBuf { &self.0 }
/// fn join<P: AsRef<Path>>(&self, path: P) -> Self { Self(self.0.join(path)) }
/// }
///
/// // Generated functions:
/// // fn src() -> Loc // ROOT.join("src")
/// // fn main() -> Loc // ROOT.join("src/main")
/// // fn lib() -> Loc // ROOT.join("src/lib")
/// // fn cargo_toml() -> Loc // ROOT.join("Cargo.toml")
/// // fn gitignore() -> Loc // ROOT.join(".gitignore")
/// // fn debug() -> Loc // ROOT.join("target/debug")
/// // fn release() -> Loc // ROOT.join("target/release")
/// // Note: no target() function due to `: _`
/// ```
;
// Process subpaths
=> ;
// Generate a root-level node
=> ;
=> ;
// Generate a subnode
=> ;
=> ;
// Only generate function if name is not _
=> ;
=> ;
// Handle path strings - converts literals and identifiers to strings
=> ;
=> ;
}