Skip to content

Commit 7891e60

Browse files
committed
feat: leptos_viz_macro
1 parent c1ec81a commit 7891e60

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

Cargo.toml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
resolver = "2"
33
members = [
4+
"leptos_viz_macro",
45
"examples/*",
56
]
67

@@ -19,13 +20,16 @@ tokio = { version = "1", default-features = false }
1920
tokio-util = { version = "0.7", features = ["rt"] }
2021

2122
leptos_viz = { path = "." }
22-
leptos = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
23-
leptos_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
24-
leptos_meta = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
25-
leptos_router = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
26-
leptos_reactive = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
27-
leptos_integration_utils = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
28-
server_fn = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
23+
leptos_viz_macro = { path = "leptos_viz_macro" }
24+
25+
leptos = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
26+
leptos_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
27+
leptos_meta = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
28+
leptos_router = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
29+
leptos_reactive = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
30+
leptos_integration_utils = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
31+
server_fn = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
32+
server_fn_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
2933

3034
# registration system
3135
dashmap = "5"

examples/todo_app_sqlite_viz/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ http.workspace = true
2626
futures.workspace = true
2727
leptos.workspace = true
2828
leptos_viz = { workspace = true, optional = true }
29+
leptos_viz_macro = { workspace = true }
2930
leptos_macro = { workspace = true, features = ["nightly"] }
3031
leptos_meta = { workspace = true, features = ["nightly"] }
3132
leptos_router = { workspace = true, features = ["nightly"] }
3233
leptos_reactive = { workspace = true, features = ["nightly"] }
34+
server_fn = { workspace = true, features = ["ssr", "serde-lite"] }
3335

3436
[features]
3537
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]

examples/todo_app_sqlite_viz/src/todo.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use cfg_if::cfg_if;
33
use leptos::*;
44
use leptos_meta::*;
55
use leptos_router::*;
6+
use leptos_viz_macro::server;
67
use serde::{Deserialize, Serialize};
8+
use server_fn::codec::SerdeLite;
79

810
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
911
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
@@ -26,14 +28,14 @@ cfg_if! {
2628
}
2729
}
2830

29-
#[server(GetTodos, "/api")]
31+
#[server]
3032
pub async fn get_todos() -> Result<Vec<Todo>, ServerFnError> {
3133
// this is just an example of how to access server context injected in the handlers
3234
// http::Request doesn't implement Clone, so more work will be needed to do use_context() on this
33-
let req_parts = use_context::<leptos_viz::RequestParts>();
35+
let parts = use_context::<http::request::Parts>();
3436

35-
if let Some(req_parts) = req_parts {
36-
println!("Uri = {:?}", req_parts.uri);
37+
if let Some(parts) = parts {
38+
println!("Uri = {:?}", parts.uri);
3739
}
3840

3941
use futures::TryStreamExt;
@@ -63,7 +65,7 @@ pub async fn get_todos() -> Result<Vec<Todo>, ServerFnError> {
6365
Ok(todos)
6466
}
6567

66-
#[server(AddTodo, "/api")]
68+
#[server]
6769
pub async fn add_todo(title: String) -> Result<(), ServerFnError> {
6870
let mut conn = db().await?;
6971

@@ -81,7 +83,7 @@ pub async fn add_todo(title: String) -> Result<(), ServerFnError> {
8183
}
8284

8385
// The struct name and path prefix arguments are optional.
84-
#[server]
86+
#[server(output = SerdeLite)]
8587
pub async fn delete_todo(id: u16) -> Result<(), ServerFnError> {
8688
let mut conn = db().await?;
8789

@@ -97,7 +99,6 @@ pub fn TodoApp() -> impl IntoView {
9799
//let id = use_context::<String>();
98100
provide_meta_context();
99101
view! {
100-
101102
<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
102103
<Stylesheet id="leptos" href="/pkg/todo_app_sqlite_viz.css"/>
103104
<Router>
@@ -126,7 +127,6 @@ pub fn Todos() -> impl IntoView {
126127
);
127128

128129
view! {
129-
130130
<div>
131131
<MultiActionForm action=add_todo>
132132
<label>

leptos_viz_macro/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "leptos_viz_macro"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
server_fn_macro.workspace = true
11+
viz.workspace = true
12+
syn = "2.0.48"
13+
tracing = "0.1"

leptos_viz_macro/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use proc_macro::TokenStream;
2+
use server_fn_macro::server_macro_impl;
3+
use syn::__private::ToTokens;
4+
5+
#[proc_macro_attribute]
6+
pub fn server(args: TokenStream, s: TokenStream) -> TokenStream {
7+
match server_macro_impl(
8+
args.into(),
9+
s.into(),
10+
Some(syn::parse_quote!(::leptos::server_fn)),
11+
"/api",
12+
Some(syn::parse_quote!(::viz::Request)),
13+
Some(syn::parse_quote!(::viz::Response)),
14+
) {
15+
Err(e) => e.to_compile_error().into(),
16+
Ok(s) => s.to_token_stream().into(),
17+
}
18+
}

0 commit comments

Comments
 (0)