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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Procedural macros for the [Tidos](https://docs.rs/tidos) SSR framework.
//!
//! This crate is an implementation detail. All macros are re-exported by the
//! `tidos` crate via `#[doc(inline)] pub use tidos_macro::*;`, so you should
//! depend on `tidos` directly rather than on this crate.
//!
//! For usage examples and full documentation see the
//! [`tidos` crate docs](https://docs.rs/tidos).
extern crate proc_macro;
use TokenStream;
use cratePageWrapper;
use ;
use parse_macro_input;
use Component;
use Uuid;
/// Renders an HTML template to a `String`.
///
/// Write HTML directly in Rust. Interpolate Rust expressions with `{ }` and
/// use the control-flow blocks below for loops, conditionals, and pattern
/// matching.
///
/// # Syntax summary
///
/// | Syntax | Meaning |
/// |---|---|
/// | `{expr}` | Interpolate a Rust expression |
/// | `{#for x in iter} … {/for}` | Loop |
/// | `{#if cond} … {:else if cond} … {:else} … {/if}` | Conditional |
/// | `{#match val} {:case Pat} … {/match}` | Pattern match |
/// | `<Component prop={expr} />` | Render a component |
///
/// # Examples
///
/// ## Inline expression
/// ```rust,no_run
/// use tidos_macro::view;
///
/// let name = "Alice";
/// view! {
/// <p>Hello {name}!</p>
/// }
/// ```
///
/// ## For loop
/// ```rust,no_run
/// use tidos_macro::view;
///
/// let names = vec!["Bob", "Alice"];
///
/// view! {
/// {#for name in names}
/// <p>{format!("Hello {}!", name)}</p>
/// {/for}
/// }
/// ```
///
/// ## If / else
/// ```rust,no_run
/// use tidos_macro::view;
///
/// let age = 18;
/// let is_american = false;
///
/// view! {
/// {#if age >= 18 && !is_american}
/// <p>User is allowed to drink.</p>
/// {:else if age >= 21 && is_american}
/// <p>User is allowed to drink.</p>
/// {:else if age >= 18 && age < 21 && is_american}
/// <p>User is probably the designated driver.</p>
/// {:else}
/// <p>User is not allowed to drink.</p>
/// {/if}
/// }
/// ```
///
/// ## Match statement
/// ```rust,no_run
/// use tidos_macro::view;
///
/// enum Pet { Fish, Dog, Cat, Other { name: String } }
/// use Pet::*;
///
/// let my_pet = Dog;
///
/// view! {
/// {#match my_pet}
/// {:case Fish}
/// <p>Blub!</p>
/// {:case Dog}
/// <p>{"Who's a good boy!"}</p>
/// {:case Cat}
/// <p>Give all mortal possessions to cat!</p>
/// {:case _}
/// <p>Is it a snake or a spider?</p>
/// {/match}
/// }
/// ```
/// Renders an HTML template and wraps it in a `Page`.
///
/// Use `page!` in a route handler instead of [`view!`] when you need to
/// return a full page. Internally it creates a `Page`, renders the template
/// into it, and returns the `Page` value.
///
/// Accepts exactly the same template syntax as [`view!`].
///
/// # Example
///
/// ```rust,no_run
/// use tidos::{page, Component, Page};
///
/// pub fn getting_started() -> Page {
/// let x: isize = Default::default();
///
/// page! {
/// <main>
/// <h1>Getting started</h1>
/// <p>{x.to_string()}</p>
/// </main>
/// }
/// }
/// ```
/// Injects HTML into the `<head>` of the current page.
///
/// Call `head!` inside `Component::to_render` to add elements such as
/// `<title>`, `<link>`, or `<meta>` tags to the page `<head>`. Each call is
/// deduplicated by a compile-time UUID, so it is safe to call from components
/// rendered inside a loop.
///
/// # Example
///
/// ```rust,no_run
/// use tidos::{head, Component, Page};
///
/// pub struct Title {
/// pub title: String,
/// }
///
/// impl Component for Title {
/// fn to_render(&self, page: &mut Page) -> String {
/// head! {
/// <title>{&self.title}</title>
/// }
/// String::new()
/// }
/// }
/// ```
/// Injects a scoped CSS file into the page `<head>` and returns the generated class name.
///
/// The path is resolved relative to the calling source file (like
/// `include_str!`). At compile time the macro generates a unique
/// `tidos-<uuid>` class name, wraps the CSS file content as
/// `.tidos-<uuid> { … }`, and injects a `<style>` tag into the page `<head>`
/// via `Page::add_elements_to_head`. Because injection is keyed by UUID,
/// calling `scoped_css!` inside a loop only injects the style once.
///
/// The macro returns a `&'static str` you can bind to a `class` attribute.
///
/// # Example
///
/// ```rust,no_run
/// use tidos::{scoped_css, view, Component, Page};
///
/// pub struct Card { pub title: String }
///
/// impl Component for Card {
/// fn to_render(&self, page: &mut Page) -> String {
/// view! {
/// <div class={scoped_css!("./card.css")}>
/// <h2>{&self.title}</h2>
/// </div>
/// }
/// }
/// }
/// ```
/// Derives [`Component`](tidos::Component) for a struct that wraps a native
/// [Custom Element](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements).
///
/// Place `#[native_element]` on a struct to automatically generate a
/// `Component` implementation that:
///
/// 1. Injects `<script type="module" src="/dist/StructName.js"></script>` into
/// the page `<head>`
/// 2. Renders the kebab-case HTML tag derived from the struct name, forwarding
/// all fields as HTML attributes.
///
/// # Attribute mapping
///
/// Field names are converted from `snake_case` to `kebab-case`.
/// The struct name is converted from `PascalCase` to `kebab-case` for the
/// HTML tag, and used as-is for the JS filename.
///
/// # Example
///
/// ```rust,no_run
/// use tidos::native_element;
///
/// #[native_element]
/// pub struct GreetName {
/// pub name: String,
/// pub is_shiny: bool,
/// }
/// ```
///
/// Generates roughly:
///
/// ```rust,no_run
/// # use tidos::{Component, Page};
/// # pub struct GreetName { pub name: String, pub is_shiny: bool }
/// impl tidos::Component for GreetName {
/// fn to_render(&self, page: &mut tidos::Page) -> String {
/// tidos::head! { <script type="module" src="/dist/GreetName.js"> }
/// tidos::view! { <greet-name name="Alice" is-shiny></greet-name> }
/// }
/// }
/// ```