lib.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. use proc_macro::TokenStream;
  2. use quote::ToTokens;
  3. use syn::parse_macro_input;
  4. mod inlineprops;
  5. mod props;
  6. // mod rsx;
  7. use dioxus_rsx as rsx;
  8. #[proc_macro]
  9. pub fn format_args_f(input: TokenStream) -> TokenStream {
  10. use rsx::*;
  11. format_args_f_impl(parse_macro_input!(input as IfmtInput))
  12. .unwrap_or_else(|err| err.to_compile_error())
  13. .into()
  14. }
  15. #[proc_macro_derive(Props, attributes(props))]
  16. pub fn derive_typed_builder(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
  17. let input = parse_macro_input!(input as syn::DeriveInput);
  18. match props::impl_my_derive(&input) {
  19. Ok(output) => output.into(),
  20. Err(error) => error.to_compile_error().into(),
  21. }
  22. }
  23. /// The rsx! macro makes it easy for developers to write jsx-style markup in their components.
  24. ///
  25. /// ## Complete Reference Guide:
  26. /// ```ignore
  27. #[doc = include_str!("../../../examples/rsx_usage.rs")]
  28. /// ```
  29. #[proc_macro]
  30. pub fn rsx(s: TokenStream) -> TokenStream {
  31. match syn::parse::<rsx::CallBody>(s) {
  32. Err(err) => err.to_compile_error().into(),
  33. Ok(body) => body.to_token_stream().into(),
  34. }
  35. }
  36. /// The render! macro makes it easy for developers to write jsx-style markup in their components.
  37. ///
  38. /// The render macro automatically renders rsx - making it unhygenic.
  39. ///
  40. /// ## Complete Reference Guide:
  41. /// ```ignore
  42. #[doc = include_str!("../../../examples/rsx_usage.rs")]
  43. /// ```
  44. #[proc_macro]
  45. pub fn render(s: TokenStream) -> TokenStream {
  46. match syn::parse::<rsx::CallBody>(s) {
  47. Err(err) => err.to_compile_error().into(),
  48. Ok(mut body) => {
  49. body.inline_cx = true;
  50. body.into_token_stream().into()
  51. }
  52. }
  53. }
  54. /// Derive props for a component within the component definition.
  55. ///
  56. /// This macro provides a simple transformation from `Scope<{}>` to `Scope<P>`,
  57. /// removing some boilerplate when defining props.
  58. ///
  59. /// You don't *need* to use this macro at all, but it can be helpful in cases where
  60. /// you would be repeating a lot of the usual Rust boilerplate.
  61. ///
  62. /// # Example
  63. /// ```ignore
  64. /// #[inline_props]
  65. /// fn app(cx: Scope, bob: String) -> Element {
  66. /// cx.render(rsx!("hello, {bob}"))
  67. /// }
  68. ///
  69. /// // is equivalent to
  70. ///
  71. /// #[derive(PartialEq, Props)]
  72. /// struct AppProps {
  73. /// bob: String,
  74. /// }
  75. ///
  76. /// fn app(cx: Scope<AppProps>) -> Element {
  77. /// cx.render(rsx!("hello, {bob}"))
  78. /// }
  79. /// ```
  80. #[proc_macro_attribute]
  81. pub fn inline_props(_args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
  82. match syn::parse::<inlineprops::InlinePropsBody>(s) {
  83. Err(e) => e.to_compile_error().into(),
  84. Ok(s) => s.to_token_stream().into(),
  85. }
  86. }