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
//! 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 crate;
use ;
use parse_macro_input;
use Component;
use Uuid;
use crateI18n;
/// 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>
/// }
/// }
/// }
/// ```
/// Looks up a [Fluent](https://projectfluent.org/) translation key and returns the translated `String`.
///
/// Requires the `i18n` feature flag on the `tidos` crate and a call to
/// `enable_i18n!` in `main.rs`.
///
/// The current locale is read from `page.lang`, which is set by the `Lang`
/// Rocket request guard.
///
/// # Syntax
///
/// ```text
/// i18n!("message-key")
/// i18n!("message-key", "variable", value, …)
/// ```
///
/// Variables are passed as alternating key-value pairs after the message key.
/// Keys must be string literals; values can be any Rust expression.
///
/// # Example
///
/// ```rust,no_run
/// use tidos::i18n::i18n;
/// use tidos::{view, Component, Page};
///
/// pub struct Greeting;
///
/// impl Component for Greeting {
/// fn to_render(&self, page: &mut Page) -> String {
/// view! {
/// <h1>{i18n!("greeting")}</h1>
/// <p>{i18n!("shared-photos", "userName", "Anne", "photoCount", 3)}</p>
/// }
/// }
/// }
/// ```