lib.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. use proc_macro::TokenStream;
  2. use quote::ToTokens;
  3. use syn::parse_macro_input;
  4. mod ifmt;
  5. mod inlineprops;
  6. mod props;
  7. #[proc_macro]
  8. pub fn format_args_f(input: TokenStream) -> TokenStream {
  9. use ifmt::*;
  10. let item = parse_macro_input!(input as IfmtInput);
  11. format_args_f_impl(item)
  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. /// ```
  27. /// const Example: Component = |cx| {
  28. /// let formatting = "formatting!";
  29. /// let formatting_tuple = ("a", "b");
  30. /// let lazy_fmt = format_args!("lazily formatted text");
  31. /// cx.render(rsx! {
  32. /// div {
  33. /// // Elements
  34. /// div {}
  35. /// h1 {"Some text"}
  36. /// h1 {"Some text with {formatting}"}
  37. /// h1 {"Formatting basic expressions {formatting_tuple.0} and {formatting_tuple.1}"}
  38. /// h2 {
  39. /// "Multiple"
  40. /// "Text"
  41. /// "Blocks"
  42. /// "Use comments as separators in html"
  43. /// }
  44. /// div {
  45. /// h1 {"multiple"}
  46. /// h2 {"nested"}
  47. /// h3 {"elements"}
  48. /// }
  49. /// div {
  50. /// class: "my special div"
  51. /// h1 {"Headers and attributes!"}
  52. /// }
  53. /// div {
  54. /// // pass simple rust expressions in
  55. /// class: lazy_fmt,
  56. /// id: format_args!("attributes can be passed lazily with std::fmt::Arguments"),
  57. /// div {
  58. /// class: {
  59. /// const WORD: &str = "expressions";
  60. /// format_args!("Arguments can be passed in through curly braces for complex {}", WORD)
  61. /// }
  62. /// }
  63. /// }
  64. ///
  65. /// // Expressions can be used in element position too:
  66. /// {rsx!(p { "More templating!" })}
  67. /// {html!(<p>"Even HTML templating!!"</p>)}
  68. ///
  69. /// // Iterators
  70. /// {(0..10).map(|i| rsx!(li { "{i}" }))}
  71. /// {{
  72. /// let data = std::collections::HashMap::<&'static str, &'static str>::new();
  73. /// // Iterators *should* have keys when you can provide them.
  74. /// // Keys make your app run faster. Make sure your keys are stable, unique, and predictable.
  75. /// // Using an "ID" associated with your data is a good idea.
  76. /// data.into_iter().map(|(k, v)| rsx!(li { key: "{k}" "{v}" }))
  77. /// }}
  78. ///
  79. /// // Matching
  80. /// {match true {
  81. /// true => rsx!(h1 {"Top text"}),
  82. /// false => rsx!(h1 {"Bottom text"})
  83. /// }}
  84. ///
  85. /// // Conditional rendering
  86. /// // Dioxus conditional rendering is based around None/Some. We have no special syntax for conditionals.
  87. /// // You can convert a bool condition to rsx! with .then and .or
  88. /// {true.then(|| rsx!(div {}))}
  89. ///
  90. /// // True conditions
  91. /// {if true {
  92. /// rsx!(h1 {"Top text"})
  93. /// } else {
  94. /// rsx!(h1 {"Bottom text"})
  95. /// }}
  96. ///
  97. /// // returning "None" is a bit noisy... but rare in practice
  98. /// {None as Option<()>}
  99. ///
  100. /// // Use the Dioxus type-alias for less noise
  101. /// {NONE_ELEMENT}
  102. ///
  103. /// // can also just use empty fragments
  104. /// Fragment {}
  105. ///
  106. /// // Fragments let you insert groups of nodes without a parent.
  107. /// // This lets you make components that insert elements as siblings without a container.
  108. /// div {"A"}
  109. /// Fragment {
  110. /// div {"B"}
  111. /// div {"C"}
  112. /// Fragment {
  113. /// "D"
  114. /// Fragment {
  115. /// "heavily nested fragments is an antipattern"
  116. /// "they cause Dioxus to do unnecessary work"
  117. /// "don't use them carelessly if you can help it"
  118. /// }
  119. /// }
  120. /// }
  121. ///
  122. /// // Components
  123. /// // Can accept any paths
  124. /// // Notice how you still get syntax highlighting and IDE support :)
  125. /// Baller {}
  126. /// baller::Baller { }
  127. /// crate::baller::Baller {}
  128. ///
  129. /// // Can take properties
  130. /// Taller { a: "asd" }
  131. ///
  132. /// // Can take optional properties
  133. /// Taller { a: "asd" }
  134. ///
  135. /// // Can pass in props directly as an expression
  136. /// {{
  137. /// let props = TallerProps {a: "hello"};
  138. /// rsx!(Taller { ..props })
  139. /// }}
  140. ///
  141. /// // Spreading can also be overridden manually
  142. /// Taller {
  143. /// ..TallerProps { a: "ballin!" }
  144. /// a: "not ballin!"
  145. /// }
  146. ///
  147. /// // Can take children too!
  148. /// Taller { a: "asd", div {"hello world!"} }
  149. /// }
  150. /// })
  151. /// };
  152. ///
  153. /// mod baller {
  154. /// use super::*;
  155. /// pub struct BallerProps {}
  156. ///
  157. /// /// This component totally balls
  158. /// pub fn Baller(cx: Scope) -> DomTree {
  159. /// todo!()
  160. /// }
  161. /// }
  162. ///
  163. /// #[derive(Debug, PartialEq, Props)]
  164. /// pub struct TallerProps {
  165. /// a: &'static str,
  166. /// }
  167. ///
  168. /// /// This component is taller than most :)
  169. /// pub fn Taller(cx: Scope<TallerProps>) -> DomTree {
  170. /// let b = true;
  171. /// todo!()
  172. /// }
  173. /// ```
  174. #[proc_macro_error::proc_macro_error]
  175. #[proc_macro]
  176. pub fn rsx(s: TokenStream) -> TokenStream {
  177. match syn::parse::<dioxus_rsx::CallBody>(s) {
  178. Err(err) => err.to_compile_error().into(),
  179. Ok(stream) => stream.to_token_stream().into(),
  180. }
  181. }
  182. /// Derive props for a component within the component definition.
  183. ///
  184. /// This macro provides a simple transformation from `Scope<{}>` to `Scope<P>`,
  185. /// removing some boilerplate when defining props.
  186. ///
  187. /// You don't *need* to use this macro at all, but it can be helpful in cases where
  188. /// you would be repeating a lot of the usual Rust boilerplate.
  189. ///
  190. /// # Example
  191. /// ```
  192. /// #[inline_props]
  193. /// fn app(cx: Scope, bob: String) -> Element {
  194. /// cx.render(rsx!("hello, {bob}"))
  195. /// }
  196. ///
  197. /// // is equivalent to
  198. ///
  199. /// #[derive(PartialEq, Props)]
  200. /// struct AppProps {
  201. /// bob: String,
  202. /// }
  203. ///
  204. /// fn app(cx: Scope<AppProps>) -> Element {
  205. /// cx.render(rsx!("hello, {bob}"))
  206. /// }
  207. /// ```
  208. #[proc_macro_attribute]
  209. pub fn inline_props(_args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
  210. match syn::parse::<inlineprops::InlinePropsBody>(s) {
  211. Err(e) => e.to_compile_error().into(),
  212. Ok(s) => s.to_token_stream().into(),
  213. }
  214. }