~repos /tide-jsx

#rust#proc-macro#jsx

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

Tide + JSX


13ca0b5b Peter John

3 years ago
v0.4.0
.github/workflows/rust.yml DELETED
@@ -1,30 +0,0 @@
1
- name: Rust
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- pull_request:
8
-
9
- jobs:
10
- build:
11
- runs-on: ubuntu-latest
12
- steps:
13
- - uses: hecrj/setup-rust-action@v1
14
- with:
15
- rust-version: stable
16
- - uses: actions/checkout@v1
17
- - name: Build
18
- run: cargo build --verbose
19
- - name: Run tests
20
- run: cargo test --verbose
21
- fmt:
22
- runs-on: ubuntu-latest
23
- steps:
24
- - uses: hecrj/setup-rust-action@v1
25
- with:
26
- rust-version: stable
27
- components: rustfmt
28
- - uses: actions/checkout@v1
29
- - name: cargo fmt
30
- run: cargo fmt -- --check
.github/workflows/test.yml ADDED
@@ -0,0 +1,35 @@
1
+ name: Test
2
+ on: [push, pull_request]
3
+ jobs:
4
+ format:
5
+ runs-on: ubuntu-latest
6
+ steps:
7
+ - uses: actions-rs/toolchain@v1
8
+ with:
9
+ toolchain: stable
10
+ - uses: actions/checkout@master
11
+ - run: rustup component add rustfmt
12
+ - run: cargo fmt --all -- --check
13
+ test:
14
+ runs-on: ${{ matrix.os }}
15
+ continue-on-error: ${{ matrix.experimental }}
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ os: [ubuntu-latest, windows-latest, macOS-latest]
20
+ rust: [stable]
21
+ experimental: [false]
22
+ include:
23
+ - rust: nightly
24
+ os: ubuntu-latest
25
+ experimental: true
26
+ steps:
27
+ - uses: actions-rs/toolchain@v1
28
+ with:
29
+ toolchain: ${{ matrix.rust }}
30
+ - uses: actions/checkout@master
31
+ - name: Run tests
32
+ run: |
33
+ cargo build --verbose
34
+ cargo test --verbose
35
+ cargo build --example basic
Cargo.lock CHANGED
@@ -1465,7 +1465,7 @@ dependencies = [
1465
1465
 
1466
1466
  [[package]]
1467
1467
  name = "tide-jsx"
1468
- version = "0.3.2"
1468
+ version = "0.4.0"
1469
1469
  dependencies = [
1470
1470
  "async-std",
1471
1471
  "pretty_assertions",
@@ -1476,7 +1476,7 @@ dependencies = [
1476
1476
 
1477
1477
  [[package]]
1478
1478
  name = "tide-jsx-impl"
1479
- version = "0.2.1"
1479
+ version = "0.3.0"
1480
1480
  dependencies = [
1481
1481
  "proc-macro-error",
1482
1482
  "proc-macro2",
Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "tide-jsx"
3
- version = "0.3.3"
3
+ version = "0.4.0"
4
4
  authors = ["pyrossh", "Gal Schlezinger <gal@spitfire.co.il>"]
5
5
  edition = "2021"
6
6
  description = "A safe and simple template engine with the ergonomics of JSX"
@@ -12,7 +12,7 @@ license = "MIT"
12
12
 
13
13
  [dependencies]
14
14
  tide = "0.16.0"
15
- tide-jsx-impl = { path = "impl", version = "0.2.2" }
15
+ tide-jsx-impl = { path = "impl", version = "0.3.0" }
16
16
 
17
17
  [dev-dependencies]
18
18
  pretty_assertions = "0.6"
README.md CHANGED
@@ -104,15 +104,7 @@ use render::{component, rsx, html, Render};
104
104
  #[component]
105
105
  fn Page<'a, Children: Render>(title: &'a str, children: Children) {
106
106
  rsx! {
107
- <>
107
+
108
- <HTML5Doctype />
109
- <html>
110
- <head><title>{title}</title></head>
111
- <body>
112
- {children}
113
- </body>
114
- </html>
115
- </>
116
108
  }
117
109
  }
118
110
 
examples/basic.rs ADDED
@@ -0,0 +1,45 @@
1
+ use tide::http::mime;
2
+ use tide::utils::After;
3
+ use tide::{Request, Response, log};
4
+ use tide_jsx::{view, rsx, component};
5
+ use tide_jsx::html::HTML5Doctype;
6
+
7
+ #[component]
8
+ fn Heading<'title>(title: &'title str) {
9
+ rsx! { <h1 class={"title"}>{title}</h1> }
10
+ }
11
+
12
+ async fn index(_req: Request<()>) -> tide::Result {
13
+ view! {
14
+ <>
15
+ <HTML5Doctype />
16
+ <html>
17
+ <head><title>{"Tide JSX"}</title></head>
18
+ <body>
19
+ <div>
20
+ <Heading title={"Hello world"} />
21
+ </div>
22
+ </body>
23
+ </html>
24
+ </>
25
+ }
26
+ }
27
+
28
+ #[async_std::main]
29
+ async fn main() -> tide::Result<()> {
30
+ log::start();
31
+ let mut app = tide::new();
32
+ app.with(tide::log::LogMiddleware::new());
33
+ app.with(After(|mut res: Response| async {
34
+ if let Some(err) = res.error() {
35
+ let msg = format!("<h1>Error: {:?}</h1>", err);
36
+ res.set_status(err.status());
37
+ res.set_content_type(mime::HTML);
38
+ res.set_body(msg);
39
+ }
40
+ Ok(res)
41
+ }));
42
+ app.at("/").get(index);
43
+ app.listen("127.0.0.1:5000").await?;
44
+ Ok(())
45
+ }
impl/Cargo.lock CHANGED
@@ -103,7 +103,7 @@ dependencies = [
103
103
 
104
104
  [[package]]
105
105
  name = "tide-jsx-impl"
106
- version = "0.2.1"
106
+ version = "0.3.0"
107
107
  dependencies = [
108
108
  "pretty_assertions",
109
109
  "proc-macro-error",
impl/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "tide-jsx-impl"
3
- version = "0.2.2"
3
+ version = "0.3.0"
4
4
  authors = ["pyrossh", "Gal Schlezinger <gal@spitfire.co.il>"]
5
5
  edition = "2021"
6
6
  description = "The macros needed for `render`"
impl/src/lib.rs CHANGED
@@ -36,10 +36,10 @@ pub fn rsx(input: TokenStream) -> TokenStream {
36
36
  pub fn view(input: TokenStream) -> TokenStream {
37
37
  let el = parse_macro_input!(input as Element);
38
38
  let result = quote! {
39
- ::tide::Response::builder(StatusCode::Ok)
39
+ Ok(::tide::Response::builder(tide::http::StatusCode::Ok)
40
40
  .content_type(::tide::http::mime::HTML)
41
- .body(#el.render())
41
+ .body(::tide_jsx::Render::render(#el))
42
- .build()
42
+ .build())
43
43
  };
44
44
  TokenStream::from(result)
45
45
  }
tests/lib.rs CHANGED
@@ -153,7 +153,8 @@ fn vec() {
153
153
 
154
154
  #[async_std::test]
155
155
  async fn render_view() -> std::io::Result<()> {
156
- let mut res = view! { <p>{"hello"}</p> };
156
+ let result = view! { <p>{"hello"}</p> } as tide::Result;
157
+ let mut res = result.unwrap();
157
158
  assert_eq!(res.status(), StatusCode::Ok);
158
159
  assert_eq!(res.header("content-type").unwrap().as_str(), tide::http::mime::HTML.to_string());
159
160
  assert_eq!(res.take_body().into_string().await.unwrap(), "<p>hello</p>");