Skip to main content

rustolio_web_core_macros/
lib.rs

1//
2// SPDX-License-Identifier: MPL-2.0
3//
4// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9//
10
11mod component;
12mod container;
13
14use proc_macro::TokenStream;
15use syn::{ItemFn, parse_macro_input};
16
17macro_rules! tags {
18    ($method:ident,) => {};
19    ($method:ident, $name:ident $($rest:tt)*) => (
20        /// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/$name
21        #[proc_macro]
22        pub fn $name(input: TokenStream) -> TokenStream {
23            match container::tag(stringify!($name), proc_macro2::Ident::new(stringify!($method), proc_macro2::Span::call_site()), input) {
24                Ok(v) => v.into(),
25                Err(e) => e.into_compile_error().into(),
26            }
27        }
28
29        tags!($method, $($rest)*);
30    )
31}
32
33// HTML
34tags!(
35    html, a abbr address area article aside audio b base bdi bdo blockquote br button canvas caption cite code col colgroup data datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main map mark meter nav noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script section select small source span strong style sub summary sup table tbody td template textarea tfoot th thead time title tr track u ul var video wbr);
36
37// SVG
38tags!(svg, svg path cirle rect);
39
40// Custom
41tags!(html, dom_aware);
42
43#[proc_macro]
44pub fn raw(input: TokenStream) -> TokenStream {
45    match container::raw(input) {
46        Ok(v) => v.into(),
47        Err(e) => e.into_compile_error().into(),
48    }
49}
50
51#[proc_macro]
52pub fn attributes(input: TokenStream) -> TokenStream {
53    match container::attributes(input) {
54        Ok(v) => v.into(),
55        Err(e) => e.into_compile_error().into(),
56    }
57}
58
59#[proc_macro]
60pub fn elements(input: TokenStream) -> TokenStream {
61    match container::elements(input) {
62        Ok(v) => v.into(),
63        Err(e) => e.into_compile_error().into(),
64    }
65}
66
67#[proc_macro]
68pub fn element(input: TokenStream) -> TokenStream {
69    match container::element(input) {
70        Ok(v) => v.into(),
71        Err(e) => e.into_compile_error().into(),
72    }
73}
74
75#[proc_macro_attribute]
76pub fn component(attr: TokenStream, item: TokenStream) -> TokenStream {
77    let item = parse_macro_input!(item as ItemFn);
78    match component::component(attr, item) {
79        Ok(v) => v.into(),
80        Err(e) => e.into_compile_error().into(),
81    }
82}