~repos /tide-jsx

#rust#proc-macro#jsx

git clone https://pyrossh.dev/repos/tide-jsx.git

Tide + JSX


79a06e56 Gal Schlezinger

6 years ago
docs
Cargo.lock CHANGED
@@ -75,8 +75,10 @@ dependencies = [
75
75
  name = "render_macros"
76
76
  version = "0.1.0"
77
77
  dependencies = [
78
+ "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
78
79
  "proc-macro2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
79
80
  "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
81
+ "render 0.1.0",
80
82
  "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
81
83
  ]
82
84
 
render/src/fragment.rs CHANGED
@@ -1,3 +1,5 @@
1
+ //! The fragment component
2
+
1
3
  use crate::Renderable;
2
4
 
3
5
  /// A top-level root component to combine a same-level components
render/src/html.rs CHANGED
@@ -1,3 +1,5 @@
1
+ //! HTML utilities
2
+
1
3
  use crate::Renderable;
2
4
 
3
5
  /// HTML 5 doctype declaration
render_macros/Cargo.toml CHANGED
@@ -19,3 +19,7 @@ proc-macro = true
19
19
  syn = { version = "1.0", features = ["full"] }
20
20
  quote = "1.0"
21
21
  proc-macro2 = "1.0"
22
+
23
+ [dev-dependencies]
24
+ render = { path = "../render" }
25
+ pretty_assertions = "0.6"
render_macros/src/lib.rs CHANGED
@@ -14,7 +14,71 @@ use proc_macro::TokenStream;
14
14
  use quote::quote;
15
15
  use syn::parse_macro_input;
16
16
 
17
- /// Render a component tree to an HTML string
17
+ /// Render a component tree to an HTML string, using XML-like tags.
18
+ ///
19
+ /// The ergonomics are based on JSX:
20
+ ///
21
+ /// ### Simple HTML elements start with a lowercase
22
+ ///
23
+ /// ```rust
24
+ /// # #![feature(proc_macro_hygiene)]
25
+ /// # use pretty_assertions::assert_eq;
26
+ /// # use render_macros::html;
27
+ /// let rendered = html! { <div id={"main"}>{"Hello"}</div> };
28
+ /// assert_eq!(rendered, r#"<div id="main">Hello</div>"#);
29
+ /// ```
30
+ ///
31
+ /// ### Custom components start with an uppercase
32
+ ///
33
+ /// ```rust
34
+ /// # #![feature(proc_macro_hygiene)]
35
+ /// # use pretty_assertions::assert_eq;
36
+ /// # use render_macros::html;
37
+ /// use render::Renderable;
38
+ ///
39
+ /// #[derive(Debug)]
40
+ /// struct Heading<'t> { title: &'t str }
41
+ ///
42
+ /// impl<'t> Renderable for Heading<'t> {
43
+ /// fn render(self) -> String {
44
+ /// html! { <h1>{self.title}</h1> }
45
+ /// }
46
+ /// }
47
+ ///
48
+ /// let rendered = html! { <Heading title={"Hello world!"} /> };
49
+ ///
50
+ /// assert_eq!(rendered, r#"<h1>Hello world!</h1>"#);
51
+ /// ```
52
+ ///
53
+ /// ### Punning is supported
54
+ /// but instead of expanding to `value={true}`, it expands to
55
+ /// `value={value}` like Rust's punning
56
+ ///
57
+ /// ```rust
58
+ /// # #![feature(proc_macro_hygiene)]
59
+ /// # use render_macros::html;
60
+ /// # use pretty_assertions::assert_eq;
61
+ /// let class = "some_class";
62
+ ///
63
+ /// let rendered = html! {
64
+ /// <div class />
65
+ /// };
66
+ ///
67
+ /// assert_eq!(rendered, r#"<div class="some_class" />"#);
68
+ /// ```
69
+ ///
70
+ /// ### Values are always surrounded by curly braces
71
+ ///
72
+ /// ```rust
73
+ /// # #![feature(proc_macro_hygiene)]
74
+ /// # use render_macros::html;
75
+ /// # use pretty_assertions::assert_eq;
76
+ /// let rendered = html! {
77
+ /// <div id={"main"} />
78
+ /// };
79
+ ///
80
+ /// assert_eq!(rendered, r#"<div id="main" />"#);
81
+ /// ```
18
82
  #[proc_macro]
19
83
  pub fn html(input: TokenStream) -> TokenStream {
20
84
  let el = proc_macro2::TokenStream::from(rsx(input));
@@ -22,7 +86,7 @@ pub fn html(input: TokenStream) -> TokenStream {
22
86
  TokenStream::from(result)
23
87
  }
24
88
 
25
- /// Generate a renderable component tree
89
+ /// Generate a renderable component tree, before rendering it
26
90
  #[proc_macro]
27
91
  pub fn rsx(input: TokenStream) -> TokenStream {
28
92
  let el = parse_macro_input!(input as Element);