hypertext-macros 0.12.1

A blazing fast type-checked HTML macro crate.
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
use std::{
    iter,
    ops::{Deref, DerefMut},
};

use proc_macro2::{Ident, Span, TokenStream};
use quote::{ToTokens, quote, quote_spanned};
use syn::{
    LitStr, braced,
    parse::Parse,
    token::{Brace, Paren},
};

use super::UnquotedName;

pub fn lazy<T: Parse + Generate>(tokens: TokenStream, move_: bool) -> syn::Result<TokenStream> {
    let mut g = Generator::new_closure(T::CONTEXT);

    g.push(syn::parse2::<T>(tokens)?);

    let block = g.finish();

    let buffer_ident = Generator::buffer_ident();

    let move_token = move_.then(|| quote!(move));

    let marker_ident = T::CONTEXT.marker_type();

    Ok(quote! {
        ::hypertext::Lazy::<_, #marker_ident>::dangerously_create(
            #move_token |#buffer_ident: &mut ::hypertext::Buffer<#marker_ident>| {

                #block
            }
        )
    })
}

pub fn literal<T: Parse + Generate>(tokens: TokenStream) -> syn::Result<TokenStream> {
    let mut g = Generator::new_static(T::CONTEXT);

    g.push(syn::parse2::<T>(tokens)?);

    let literal = g.finish().to_token_stream();

    let marker_ident = T::CONTEXT.marker_type();

    Ok(quote! {
        ::hypertext::Raw::<_, #marker_ident>::dangerously_create(#literal)
    })
}

pub struct Generator {
    lazy: bool,
    context: Context,
    brace_token: Brace,
    parts: Vec<Part>,
    checks: Checks,
}

impl Generator {
    pub fn buffer_ident() -> Ident {
        Ident::new("__hypertext_buffer", Span::mixed_site())
    }

    fn new_closure(context: Context) -> Self {
        Self::new_with_brace(context, true, Brace::default())
    }

    fn new_static(context: Context) -> Self {
        Self::new_with_brace(context, false, Brace::default())
    }

    const fn new_with_brace(context: Context, lazy: bool, brace_token: Brace) -> Self {
        Self {
            lazy,
            context,
            brace_token,
            parts: Vec::new(),
            checks: Checks::new(),
        }
    }

    fn finish(self) -> AnyBlock {
        let render = if self.lazy {
            let buffer_ident = Self::buffer_ident();
            let mut stmts = TokenStream::new();

            let mut parts = self.parts.into_iter();

            let mut size_estimate = 0;

            while let Some(part) = parts.next() {
                match part {
                    Part::Static(lit) => {
                        let mut dynamic_stmt = None;
                        let static_parts = iter::once(lit)
                            .chain(parts.by_ref().map_while(|part| match part {
                                Part::Static(lit) => Some(lit),
                                Part::Dynamic(stmt) => {
                                    dynamic_stmt = Some(stmt);
                                    None
                                }
                            }))
                            .inspect(|static_part| {
                                size_estimate += static_part.value().len();
                            });

                        stmts.extend(quote! {
                            #buffer_ident.dangerously_get_string().push_str(::core::concat!(#(#static_parts),*));
                        });
                        stmts.extend(dynamic_stmt);
                    }
                    Part::Dynamic(stmt) => {
                        stmts.extend(stmt);
                    }
                }
            }

            quote! {
                #buffer_ident.dangerously_get_string().reserve(#size_estimate);
                #stmts
            }
        } else {
            let mut static_parts = Vec::new();
            let mut errors = TokenStream::new();

            for part in self.parts {
                match part {
                    Part::Static(lit) => static_parts.push(lit),
                    Part::Dynamic(stmt) => errors.extend(
                        syn::Error::new_spanned(
                            stmt,
                            "static evaluation cannot contain dynamic parts",
                        )
                        .to_compile_error(),
                    ),
                }
            }

            quote! {
                #errors
                ::core::concat!(#(#static_parts),*)
            }
        };

        let checks = self.checks;

        AnyBlock {
            brace_token: self.brace_token,
            stmts: quote! {
                #checks
                #render
            },
        }
    }

    pub fn block_with(&mut self, brace_token: Brace, f: impl FnOnce(&mut Self)) -> AnyBlock {
        let mut g = Self::new_with_brace(self.context, true, brace_token);

        f(&mut g);

        self.checks.append(&mut g.checks);

        g.finish()
    }

    pub fn push_in_block(&mut self, brace_token: Brace, f: impl FnOnce(&mut Self)) {
        let block = self.block_with(brace_token, f);
        self.push_stmt(block);
    }

    pub fn push_str(&mut self, s: &'static str) {
        self.push_spanned_str(s, Span::mixed_site());
    }

    pub fn push_spanned_str(&mut self, s: &'static str, span: Span) {
        self.parts.push(Part::Static(LitStr::new(s, span)));
    }

    pub fn push_escaped_lit(&mut self, context: Context, lit: &LitStr) {
        let value = lit.value();
        let escaped_value = match context {
            Context::Node => html_escape::encode_text(&value),
            Context::AttributeValue => html_escape::encode_double_quoted_attribute(&value),
        };

        self.parts
            .push(Part::Static(LitStr::new(&escaped_value, lit.span())));
    }

    pub fn push_lits(&mut self, literals: Vec<LitStr>) {
        for lit in literals {
            self.parts.push(Part::Static(lit));
        }
    }

    pub fn push_expr(&mut self, paren_token: Paren, context: Context, expr: impl ToTokens) {
        let buffer_ident = Self::buffer_ident();
        let buffer_expr = match (self.context, context) {
            (Context::Node, Context::Node) | (Context::AttributeValue, Context::AttributeValue) => {
                quote!(#buffer_ident)
            }
            (Context::Node, Context::AttributeValue) => {
                quote!(#buffer_ident.as_attribute_buffer())
            }
            (Context::AttributeValue, Context::Node) => unreachable!(),
        };

        let mut paren_expr = TokenStream::new();
        paren_token.surround(&mut paren_expr, |tokens| expr.to_tokens(tokens));
        let reference = quote_spanned!(paren_token.span=> &);
        self.push_stmt(quote! {
            ::hypertext::Renderable::render_to(
                #reference #paren_expr,
                #buffer_expr
            );
        });
    }

    pub fn push_stmt(&mut self, stmt: impl ToTokens) {
        self.parts.push(Part::Dynamic(stmt.to_token_stream()));
    }

    pub fn push_conditional(&mut self, cond: impl ToTokens, f: impl FnOnce(&mut Self)) {
        let then_block = self.block_with(Brace::default(), f);
        self.push_stmt(quote! {
            if #cond #then_block
        });
    }

    pub fn push(&mut self, value: impl Generate) {
        value.generate(self);
    }

    pub fn record_element(&mut self, el_checks: ElementCheck) {
        self.checks.push(el_checks);
    }

    pub fn push_all(&mut self, values: impl IntoIterator<Item = impl Generate>) {
        for value in values {
            self.push(value);
        }
    }
}

enum Part {
    Static(LitStr),
    Dynamic(TokenStream),
}

#[derive(Debug, Clone, Copy)]
pub enum Context {
    Node,
    AttributeValue,
}

impl Context {
    pub fn marker_type(self) -> TokenStream {
        let ident = match self {
            Self::Node => Ident::new("Node", Span::mixed_site()),
            Self::AttributeValue => Ident::new("AttributeValue", Span::mixed_site()),
        };

        quote!(::hypertext::context::#ident)
    }
}

pub trait Generate {
    const CONTEXT: Context;
    fn generate(&self, g: &mut Generator);
}

impl<T: Generate> Generate for &T {
    const CONTEXT: Context = T::CONTEXT;

    fn generate(&self, g: &mut Generator) {
        (*self).generate(g);
    }
}

struct Checks {
    elements: Vec<ElementCheck>,
}

impl Checks {
    const fn new() -> Self {
        Self {
            elements: Vec::new(),
        }
    }

    fn append(&mut self, other: &mut Self) {
        self.elements.append(&mut other.elements);
    }
}

impl ToTokens for Checks {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if self.is_empty() {
            return;
        }

        let checks = &self.elements;

        quote! {
            const _: fn() = || {
                #[allow(unused_imports)]
                use hypertext_elements::*;

                #[doc(hidden)]
                fn check_element<
                    K: ::hypertext::validation::ElementKind
                >(_: impl ::hypertext::validation::Element<Kind = K>) {}

                #(#checks)*
            };
        }
        .to_tokens(tokens);
    }
}

impl Deref for Checks {
    type Target = Vec<ElementCheck>;

    fn deref(&self) -> &Self::Target {
        &self.elements
    }
}

impl DerefMut for Checks {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.elements
    }
}

pub struct ElementCheck {
    ident: String,
    kind: ElementKind,
    opening_spans: Vec<Span>,
    closing_spans: Vec<Span>,
    attributes: Vec<AttributeCheck>,
}

impl ElementCheck {
    pub fn new(el_name: &UnquotedName, element_kind: ElementKind) -> Self {
        Self {
            ident: el_name.ident_string(),
            kind: element_kind,
            opening_spans: el_name.spans(),
            closing_spans: Vec::new(),
            attributes: Vec::new(),
        }
    }

    pub fn set_closing_spans(&mut self, spans: Vec<Span>) {
        self.closing_spans = spans;
    }

    pub fn push_attribute(&mut self, attr: AttributeCheck) {
        self.attributes.push(attr);
    }
}

impl ToTokens for ElementCheck {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let kind = self.kind;

        let el_checks = self
            .opening_spans
            .iter()
            .chain(&self.closing_spans)
            .map(|span| {
                let el = Ident::new_raw(&self.ident, *span);

                quote! {
                    check_element::<#kind>(#el);
                }
            });

        let el = Ident::new_raw(
            &self.ident,
            self.opening_spans
                .first()
                .copied()
                .unwrap_or_else(Span::mixed_site),
        );

        let attr_checks = self
            .attributes
            .iter()
            .map(|attr| attr.to_token_stream_with_el(&el));

        quote! {
            #(#el_checks)*
            #(#attr_checks)*
        }
        .to_tokens(tokens);
    }
}

#[derive(Debug, Clone, Copy)]
pub enum ElementKind {
    Normal,
    Void,
}

impl ToTokens for ElementKind {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Normal => quote!(::hypertext::validation::Normal),
            Self::Void => quote!(::hypertext::validation::Void),
        }
        .to_tokens(tokens);
    }
}

pub struct AttributeCheck {
    kind: AttributeCheckKind,
    ident: String,
    spans: Vec<Span>,
}

impl AttributeCheck {
    pub const fn new(kind: AttributeCheckKind, ident: String, spans: Vec<Span>) -> Self {
        Self { kind, ident, spans }
    }

    fn to_token_stream_with_el(&self, el: &Ident) -> TokenStream {
        let kind = &self.kind;

        self.spans
            .iter()
            .map(|span| {
                let ident = Ident::new_raw(&self.ident, *span);

                quote! {
                    let _: #kind = <#el>::#ident;
                }
            })
            .collect()
    }
}

pub enum AttributeCheckKind {
    Normal,
    Namespace,
    Symbol,
}

impl ToTokens for AttributeCheckKind {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Normal => quote!(::hypertext::validation::Attribute),
            Self::Namespace => quote!(::hypertext::validation::AttributeNamespace),
            Self::Symbol => quote!(::hypertext::validation::AttributeSymbol),
        }
        .to_tokens(tokens);
    }
}

pub struct AnyBlock {
    pub brace_token: Brace,
    pub stmts: TokenStream,
}

impl Parse for AnyBlock {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let content;

        Ok(Self {
            brace_token: braced!(content in input),
            stmts: content.parse()?,
        })
    }
}

impl ToTokens for AnyBlock {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.brace_token.surround(tokens, |tokens| {
            self.stmts.to_tokens(tokens);
        });
    }
}