lib.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use proc_macro::TokenStream;
  2. use quote::ToTokens;
  3. use syn::parse_macro_input;
  4. mod fc;
  5. mod htm;
  6. mod ifmt;
  7. mod props;
  8. mod rsxt;
  9. mod util;
  10. /// The html! macro makes it easy for developers to write jsx-style markup in their components.
  11. /// We aim to keep functional parity with html templates.
  12. #[proc_macro]
  13. pub fn html(s: TokenStream) -> TokenStream {
  14. match syn::parse::<htm::HtmlRender>(s) {
  15. Err(e) => e.to_compile_error().into(),
  16. Ok(s) => s.to_token_stream().into(),
  17. }
  18. }
  19. /// The html! macro makes it easy for developers to write jsx-style markup in their components.
  20. /// We aim to keep functional parity with html templates.
  21. #[proc_macro]
  22. pub fn rsx(s: TokenStream) -> TokenStream {
  23. match syn::parse::<rsxt::RsxRender>(s) {
  24. Err(e) => e.to_compile_error().into(),
  25. Ok(s) => s.to_token_stream().into(),
  26. }
  27. }
  28. // #[proc_macro_attribute]
  29. // pub fn fc(attr: TokenStream, item: TokenStream) -> TokenStream {
  30. /// Label a function or static closure as a functional component.
  31. /// This macro reduces the need to create a separate properties struct.
  32. ///
  33. /// Using this macro is fun and simple
  34. ///
  35. /// ```ignore
  36. ///
  37. /// #[fc]
  38. /// fn Example(ctx: Context, name: &str) -> DomTree {
  39. /// ctx.render(rsx! { h1 {"hello {name}"} })
  40. /// }
  41. /// ```
  42. #[proc_macro_attribute]
  43. pub fn fc(attr: TokenStream, item: TokenStream) -> TokenStream {
  44. match syn::parse::<fc::FunctionComponent>(item) {
  45. Err(e) => e.to_compile_error().into(),
  46. Ok(s) => s.to_token_stream().into(),
  47. }
  48. }
  49. #[proc_macro]
  50. pub fn format_args_f(input: TokenStream) -> TokenStream {
  51. use ifmt::*;
  52. let item = parse_macro_input!(input as IfmtInput);
  53. format_args_f_impl(item)
  54. .unwrap_or_else(|err| err.to_compile_error())
  55. .into()
  56. }
  57. #[proc_macro_derive(Props, attributes(builder))]
  58. pub fn derive_typed_builder(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
  59. let input = parse_macro_input!(input as syn::DeriveInput);
  60. match props::impl_my_derive(&input) {
  61. Ok(output) => output.into(),
  62. Err(error) => error.to_compile_error().into(),
  63. }
  64. }