ruukh-codegen 0.0.3

Codegen for the Ruukh framework.
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use super::kw;
use super::HtmlRoot;
use crate::suffix::{EVENT_SUFFIX, PROPS_SUFFIX};
use heck::{CamelCase, KebabCase, SnakeCase};
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::{
    braced,
    parse::{Error, Parse, ParseStream, Result as ParseResult},
    punctuated::Punctuated,
    spanned::Spanned,
    token, Token, {Expr, Ident},
};

pub enum HtmlElement {
    Normal(NormalHtmlElement),
    SelfClosing(SelfClosingHtmlElement),
}

impl Parse for HtmlElement {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        if kw::is_self_closing(&input) {
            Ok(HtmlElement::SelfClosing(input.parse()?))
        } else {
            Ok(HtmlElement::Normal(input.parse()?))
        }
    }
}

impl HtmlElement {
    pub fn expand(&self) -> TokenStream {
        match self {
            HtmlElement::Normal(ref normal) => normal.expand(),
            HtmlElement::SelfClosing(ref self_closing) => self_closing.expand(),
        }
    }

    pub fn key(&self) -> Option<&KeyAttribute> {
        match self {
            HtmlElement::Normal(ref el) => el.key(),
            HtmlElement::SelfClosing(ref el) => el.key(),
        }
    }
}

pub struct NormalHtmlElement {
    pub opening_tag: OpeningTag,
    pub child: Box<HtmlRoot>,
    pub closing_tag: ClosingTag,
}

impl Parse for NormalHtmlElement {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let opening_tag: OpeningTag = input.parse()?;
        let child: HtmlRoot = input.parse()?;
        let closing_tag: ClosingTag = input.parse()?;

        if opening_tag.tag_name.is_component() && child.flat_len != 0 {
            return Err(Error::new(
                closing_tag.tag_name.span(),
                "Component children are not supported right now.",
            ));
        }

        let not_same = match (&opening_tag.tag_name, &closing_tag.tag_name) {
            (TagName::Tag { name: ref op, .. }, TagName::Tag { name: ref cl, .. }) => op != cl,
            (TagName::Component { ident: ref op }, TagName::Component { ident: ref cl }) => {
                op != cl
            }
            _ => true,
        };

        if not_same {
            return Err(Error::new(
                closing_tag.tag_name.span(),
                "Opening and closing tag must be same.",
            ));
        }

        Ok(NormalHtmlElement {
            opening_tag,
            child: Box::new(child),
            closing_tag,
        })
    }
}

impl NormalHtmlElement {
    fn expand(&self) -> TokenStream {
        let child_expanded = self.child.expand();
        self.opening_tag.expand_with(&child_expanded)
    }

    pub fn key(&self) -> Option<&KeyAttribute> {
        self.opening_tag.key.as_ref()
    }
}

pub struct SelfClosingHtmlElement {
    pub tag: SelfClosingTag,
}

impl Parse for SelfClosingHtmlElement {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        Ok(SelfClosingHtmlElement {
            tag: input.parse()?,
        })
    }
}

impl SelfClosingHtmlElement {
    fn expand(&self) -> TokenStream {
        self.tag.expand()
    }

    pub fn key(&self) -> Option<&KeyAttribute> {
        self.tag.key.as_ref()
    }
}

pub struct OpeningTag {
    pub lt: Token![<],
    pub tag_name: TagName,
    pub key: Option<KeyAttribute>,
    pub prop_attributes: Vec<HtmlAttribute>,
    pub event_attributes: Vec<HtmlAttribute>,
    pub gt: Token![>],
}

impl Parse for OpeningTag {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let lt = input.parse()?;
        let tag_name = input.parse()?;
        let mut key = None;

        let mut attributes: Vec<HtmlAttribute> = vec![];
        while !input.peek(Token![>]) {
            if input.peek(kw::key) {
                key = Some(input.parse()?);
            } else {
                attributes.push(input.parse()?);
            }
        }

        let gt = input.parse()?;

        let (mut prop_attributes, mut event_attributes) = attributes
            .into_iter()
            .partition::<Vec<_>, _>(|attr| attr.at.is_none());

        prop_attributes.sort_by(|l, r| l.key.name.cmp(&r.key.name));
        event_attributes.sort_by(|l, r| l.key.name.cmp(&r.key.name));

        Ok(OpeningTag {
            lt,
            tag_name,
            key,
            prop_attributes,
            event_attributes,
            gt,
        })
    }
}

impl OpeningTag {
    fn expand_with(&self, child: &TokenStream) -> TokenStream {
        match self.tag_name {
            TagName::Tag { ref name, .. } => {
                let prop_attributes: Vec<_> = self
                    .prop_attributes
                    .iter()
                    .map(|p| p.expand_as_prop_attribute().unwrap())
                    .collect();
                let event_attributes: Vec<_> = self
                    .event_attributes
                    .iter()
                    .map(|e| e.expand_as_event_attribute().unwrap())
                    .collect();

                quote! {
                    ruukh::vdom::velement::VElement::new(
                        #name,
                        vec![#(#prop_attributes),*],
                        vec![#(#event_attributes),*],
                        #child
                    )
                }
            }
            TagName::Component { ref ident } => {
                let prop_attributes: Vec<_> = self
                    .prop_attributes
                    .iter()
                    .map(|p| p.expand_as_named_arg())
                    .collect();

                let event_attributes: Vec<_> = self
                    .event_attributes
                    .iter()
                    .map(|e| e.expand_as_named_arg())
                    .collect();

                let props_ident = Ident::new(&format!("{}{}", ident, PROPS_SUFFIX), ident.span());
                let event_ident = Ident::new(&format!("{}{}", ident, EVENT_SUFFIX), ident.span());
                let span = ident.span();
                quote_spanned!{span=>
                    ruukh::vdom::vcomponent::VComponent::new::<#ident>(
                        #props_ident!(#(#prop_attributes),*),
                        #event_ident!(#(#event_attributes),*),
                    )
                }
            }
        }
    }
}

pub struct ClosingTag {
    pub lt: Token![<],
    pub slash: Token![/],
    pub tag_name: TagName,
    pub gt: Token![>],
}

impl Parse for ClosingTag {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        Ok(ClosingTag {
            lt: input.parse()?,
            slash: input.parse()?,
            tag_name: input.parse()?,
            gt: input.parse()?,
        })
    }
}

pub struct SelfClosingTag {
    pub lt: Token![<],
    pub tag_name: TagName,
    pub key: Option<KeyAttribute>,
    pub prop_attributes: Vec<HtmlAttribute>,
    pub event_attributes: Vec<HtmlAttribute>,
    pub slash: Option<Token![/]>,
    pub gt: Token![>],
}

impl Parse for SelfClosingTag {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let lt = input.parse()?;
        let tag_name = input.parse()?;
        let mut key = None;

        let mut attributes: Vec<HtmlAttribute> = vec![];
        while !input.peek(Token![/]) && !input.peek(Token![>]) {
            if input.peek(kw::key) {
                key = Some(input.parse()?);
            } else {
                attributes.push(input.parse()?);
            }
        }

        let slash = input.parse()?;
        let gt = input.parse()?;

        let (prop_attributes, event_attributes) =
            attributes.into_iter().partition(|attr| attr.at.is_none());

        Ok(SelfClosingTag {
            lt,
            tag_name,
            key,
            prop_attributes,
            event_attributes,
            slash,
            gt,
        })
    }
}

impl SelfClosingTag {
    fn expand(&self) -> TokenStream {
        match self.tag_name {
            TagName::Tag { ref name, .. } => {
                let prop_attributes: Vec<_> = self
                    .prop_attributes
                    .iter()
                    .map(|p| p.expand_as_prop_attribute().unwrap())
                    .collect();
                let event_attributes: Vec<_> = self
                    .event_attributes
                    .iter()
                    .map(|e| e.expand_as_event_attribute().unwrap())
                    .collect();

                quote! {
                    ruukh::vdom::velement::VElement::childless(
                        #name,
                        vec![#(#prop_attributes),*],
                        vec![#(#event_attributes),*]
                    )
                }
            }
            _ => unreachable!("The spec specified self-closing tags are the only ones allowed."),
        }
    }
}

pub struct KeyAttribute {
    pub key: kw::key,
    pub eq: Token![=],
    pub brace: token::Brace,
    pub value: Expr,
}

impl Parse for KeyAttribute {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let key = input.parse()?;
        let eq = input.parse()?;
        let content;
        let brace = braced!(content in input);
        let value = content.parse()?;
        Ok(KeyAttribute {
            key,
            eq,
            brace,
            value,
        })
    }
}

impl KeyAttribute {
    pub fn expand(&self) -> TokenStream {
        let value = &self.value;
        quote! {
            ruukh::vdom::Key::new(#value)
        }
    }
}

pub struct HtmlAttribute {
    pub at: Option<Token![@]>,
    pub key: AttributeName,
    pub eq: Token![=],
    pub brace: token::Brace,
    pub value: Expr,
}

impl Parse for HtmlAttribute {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let at = input.parse()?;
        let key = input.parse()?;
        let eq = input.parse()?;
        let content;
        let brace = braced!(content in input);
        let value = content.parse()?;
        Ok(HtmlAttribute {
            at,
            key,
            eq,
            brace,
            value,
        })
    }
}

impl HtmlAttribute {
    fn expand_as_prop_attribute(&self) -> Option<TokenStream> {
        if self.at.is_some() {
            return None;
        }
        let key = &self.key.name;
        let value = &self.value;

        Some(quote! {
            ruukh::vdom::velement::Attribute::new(#key, #value)
        })
    }

    fn expand_as_event_attribute(&self) -> Option<TokenStream> {
        self.at?;
        let key = &self.key.name;
        let value = &self.value;

        Some(quote! {
            ruukh::vdom::velement::EventListener::new(#key, Box::new(#value))
        })
    }

    fn expand_as_named_arg(&self) -> TokenStream {
        let key = Ident::new(&self.key.name.to_snake_case(), Span::call_site());
        let value = &self.value;

        quote! {
            #key: #value
        }
    }
}

pub enum TagName {
    Tag { name: String, span: Span },
    Component { ident: Ident },
}

impl TagName {
    fn is_component(&self) -> bool {
        match self {
            TagName::Component { .. } => true,
            _ => false,
        }
    }

    fn span(&self) -> Span {
        match self {
            TagName::Tag { ref span, .. } => span.clone(),
            TagName::Component { ref ident } => ident.span(),
        }
    }
}

impl Parse for TagName {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let idents = input.call(Punctuated::<Ident, Token![-]>::parse_separated_nonempty)?;
        let span = idents.span();
        let mut idents = idents.into_iter().collect::<Vec<_>>();

        let ident = idents.get(0).as_ref().unwrap().to_string();
        if ident == ident.to_camel_case() {
            if idents.len() != 1 {
                return Err(Error::new(span, "no dashes in a component tag allowed."));
            }
            return Ok(TagName::Component {
                ident: idents.swap_remove(0),
            });
        }

        let tag_name = idents
            .into_iter()
            .map(|ident| ident.to_string())
            .collect::<Vec<_>>()
            .join("-");

        let kebab_tag_name = tag_name.to_kebab_case();
        if tag_name != kebab_tag_name {
            return Err(Error::new(
                span,
                &format!("tag name in kebab case only like {}.", kebab_tag_name),
            ));
        }

        Ok(TagName::Tag {
            name: tag_name,
            span,
        })
    }
}

pub struct AttributeName {
    name: String,
}

impl Parse for AttributeName {
    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
        let idents = input.call(Punctuated::<Ident, Token![-]>::parse_separated_nonempty)?;
        let span = idents.span();
        let name = idents
            .into_iter()
            .map(|id| id.to_string())
            .collect::<Vec<_>>()
            .join("-");

        let kebab_name = name.to_kebab_case();
        if name != kebab_name {
            return Err(Error::new(
                span,
                &format!("attribute name in kebab case only like {}.", kebab_name),
            ));
        }

        Ok(AttributeName { name })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn should_parse_an_html_element() {
        let _: HtmlElement = syn::parse_str("<div></div>").unwrap();
    }

    #[test]
    fn should_parse_normal_html_element() {
        let _: NormalHtmlElement = syn::parse_str("<div></div>").unwrap();
    }

    #[test]
    fn should_parse_normal_html_element_with_child() {
        let _: NormalHtmlElement = syn::parse_str(r#"<div>"Hello"</div>"#).unwrap();
    }

    #[test]
    fn should_parse_opening_tag() {
        let _: OpeningTag = syn::parse_str("<div>").unwrap();
    }

    #[test]
    fn should_parse_closing_tag() {
        let _: ClosingTag = syn::parse_str("</div>").unwrap();
    }

    #[test]
    fn should_parse_self_closing_html_element() {
        let _: SelfClosingHtmlElement = syn::parse_str("<input>").unwrap();
    }

    #[test]
    fn should_parse_self_closing_tag() {
        let _: SelfClosingTag = syn::parse_str("<input>").unwrap();
    }

    #[test]
    fn should_parse_self_closing_tag_with_slash() {
        let _: SelfClosingTag = syn::parse_str("<input/>").unwrap();
    }

    #[test]
    fn should_parse_normal_attribute() {
        let attr: HtmlAttribute = syn::parse_str(r#"name={"value"}"#).unwrap();
        assert!(attr.at.is_none());
    }

    #[test]
    fn should_parse_event_attribute() {
        let attr: HtmlAttribute = syn::parse_str(r#"@input={fn_name}"#).unwrap();
        assert!(attr.at.is_some());
    }

    #[test]
    fn should_parse_single_tag_name() {
        let parsed: TagName = syn::parse_str("Identifier").unwrap();
        if let TagName::Component { ident } = parsed {
            assert_eq!(ident, "Identifier");
        }
    }

    #[test]
    fn should_parse_dashed_tag_name() {
        let parsed: TagName = syn::parse_str("first-second-third").unwrap();
        if let TagName::Component { ident } = parsed {
            assert_eq!(ident, "first-second-third");
        }
    }
}