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
#![deny(
    warnings,
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unstable_features,
    unused_import_braces
)]

//! node macro facilitates users of sauron to use html-like syntax
//! for building view of web app components
use quote::ToTokens;

mod extract_skip_diff;
mod jss;
mod node;
mod view;

/// Quasi-quoting macro for building sauron [Node]s.
///
/// The macro allows for specifying html-like elements and attributes, and
/// supports advanced interpolation and looping.
///
/// [Node]: https://docs.rs/sauron/0/sauron/type.Node.html
///
/// # Elements
///
/// Both open elements with a closing tag, and elements which are immediately
/// closed are supported:
///
/// ```rust
/// use sauron::{node,Node};
///
/// let _: Node<()> = node!(<input type="button" />);
/// let _: Node<()> = node!(<h1>"A title"</h1>);
/// ```
///
/// # Attributes
///
/// Attributes must be valid Rust identifiers. These are translated to
/// `kebab-case` and trimmed. So `x_data_` would be translated to `x-data`.
///
/// Any sort of literal (like `true` or `42u32`) is supported as an attribute
/// argument.
///
/// ```rust
/// use sauron::{node,Node};
///
/// let _: Node<()> = node!(<input x_data_="my data" />);
/// let _: Node<()> = node!(<input x_data_int_=42u32 x_data_bool_=true />);
/// ```
///
/// Attribute values can be interpolated. These expressions must produce
/// an attribute that can be converted into a [Value].
///
/// ```rust
/// use sauron::{node,Node};
///
/// struct Model {
///     value: String,
/// }
///
/// impl Model {
///     pub fn view(&self) -> Node<()> {
///         node!(<input value={self.value.clone()} />)
///     }
/// }
/// ```
///
/// Whole attributes can also be generated. These expressions must produce an
/// [Attribute].
///
/// ```rust
/// use sauron::{node,Node,html::attributes::classes_flag};
///
/// struct Model {
///     editing: bool,
///     completed: bool,
/// }
///
/// impl Model {
///     pub fn view(&self) -> Node<()> {
///         node!(<input {{classes_flag([
///             ("todo", true),
///             ("editing", self.editing),
///             ("completed", self.completed),
///         ])}} />)
///     }
/// }
/// ```
///
/// Finally, we also support empty attributes.
///
/// ```rust
/// use sauron::{node,Node};
///
/// let _: Node<()> = node!(<button disabled />);
/// ```
///
/// [Value]: https://docs.rs/sauron/0/sauron/html/attributes/enum.Value.html
/// [Attribute]: https://docs.rs/sauron/0/sauron/type.Attribute.html
///
/// # Event handlers
///
/// Event handlers are special attributes. Any attribute that starts with `on_`
/// will be matched with event handlers available in [sauron::dom::events].
///
/// ```rust
/// use sauron::{node,Node,
///     events::{InputEvent,KeyboardEvent},
/// };
///
/// enum Msg {
///     Update(String),
///     Add,
///     Nope,
/// }
///
/// struct Model {
///     value: String,
/// }
///
/// impl Model {
///    fn view(&self) -> Node<Msg> {
///        node! {
///            <input
///                class="new-todo"
///                id="new-todo"
///                placeholder="What needs to be done?"
///                value={self.value.to_string()}
///                on_input={|v: InputEvent| Msg::Update(v.value())}
///                on_keypress={|event: KeyboardEvent| {
///                    if event.key() == "Enter" {
///                        Msg::Add
///                    } else {
///                        Msg::Nope
///                    }
///                }} />
///         }
///     }
/// }
/// ```
///
/// [sauron::dom::events]: https://docs.rs/sauron/0/sauron/dom/events/index.html
///
/// # Loops
///
/// Loops are supported through a special construct. They look and behave like
/// regular Rust loops, except that whatever the loop body evaluates to will be
/// appended to the child of a node.
///
/// The loop body must evaluate to a [Node].
///
/// ```rust
/// use sauron::{node,Node,html::text};
///
/// struct Model {
///     items: Vec<String>,
/// }
///
/// impl Model {
///     pub fn view(&self) -> Node<()> {
///         node! {
///             <ul>
///                 {for item in self.items.iter() {
///                     text(item)
///                 }}
///             </ul>
///         }
///     }
/// }
/// ```
///
/// [Node]: https://docs.rs/sauron/0/sauron/type.Node.html
///
/// Note: `node!` macro is used since it is not an html tag
/// while most other framework uses `html!` macro, this prevents
/// the library to have collision with the `html` tag, when used as tag macro
#[proc_macro]
pub fn node(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    node::to_token_stream(input).into()
}

///
#[proc_macro]
pub fn view(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    view::to_token_stream(input).into()
}

/// Generate a skip diff list based on the node used in the view
/// ```rust
/// use sauron_macro::extract_skip_diff;
///
/// let skip = extract_skip_diff!{<ul class="some-list"></ul>};
/// assert_eq!(skip, sauron::skip_if(true,[]), "skip if all attribute values are static");
/// ```
///
/// ```rust
/// use sauron_macro::extract_skip_diff;
///
/// let skip = extract_skip_diff!{"item 1"};
/// assert_eq!(skip, sauron::skip_if(true,[]), "skip raw text");
/// ```
///
#[proc_macro]
pub fn extract_skip_diff(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    extract_skip_diff::to_token_stream(input).into()
}


/// build a css string
///
/// # Example:
/// ```rust
/// use sauron::jss;
///
/// let css = jss!(
///     ".layer": {
///         background_color: "red",
///         border: "1px solid green",
///     },
///
///     ".hide .layer": {
///         opacity: 0,
///     },
/// );
///
/// let expected = "\
///     .layer {\
///     \n  background-color: red;\
///     \n  border: 1px solid green;\
///     \n}\
///     \n\
///     \n.hide .layer {\
///     \n  opacity: 0;\
///     \n}\
///     \n";
/// assert_eq!(expected, css);
/// ```
#[proc_macro]
pub fn jss(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let style_sheet = syn::parse_macro_input!(input as jss::StyleSheet);
    style_sheet.to_token_stream().into()
}

/// build css string that has media selector or any other conditional group
///
/// # Example:
/// ```rust
/// use sauron::jss_with_media;
///
/// let css = jss_with_media!(
///     "@media screen and (max-width: 800px)": {
///       ".layer": {
///         width: "100%",
///       }
///     },
/// );
///
/// let expected = "\
///     @media screen and (max-width: 800px) {\
///         \n.layer {\
///         \n  width: 100%;\
///         \n}\
///         \n\
///         \n}\
///         \n";
/// assert_eq!(expected, css);
/// ```
#[proc_macro]
pub fn jss_with_media(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let css_media = syn::parse_macro_input!(input as jss::StyleSheetWithConditionalGroup);
    css_media.to_token_stream().into()
}

/// build a style attribute
///
/// # Example:
/// ```rust
/// use sauron::style;
/// use sauron::html::units::{px, percent};
/// use sauron::html::attributes::{Attribute,attr};
///
/// let s1: Attribute<()> = style! {
///     background_color: "red",
///     border: (px(1), "solid", "green"),
///     width: percent(100),
/// };
/// let expected: Attribute<()> = attr("style","background-color:red;border:1px solid green;width:100%;");
/// assert_eq!(expected.render_to_string(), s1.render_to_string());
/// ```
#[proc_macro]
pub fn style(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let style = syn::parse_macro_input!(input as jss::Style);
    style.to_attr_tokens().into()
}