Skip to content

Commit

Permalink
feat: leptos_viz_macro
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Jan 24, 2024
1 parent c1ec81a commit 7891e60
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"leptos_viz_macro",
"examples/*",
]

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

leptos_viz = { path = "." }
leptos = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_meta = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_router = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_reactive = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_integration_utils = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
server_fn = { git = "https://github.com/leptos-rs/leptos.git", rev = "04747fc" }
leptos_viz_macro = { path = "leptos_viz_macro" }

leptos = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
leptos_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
leptos_meta = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
leptos_router = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
leptos_reactive = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
leptos_integration_utils = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
server_fn = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }
server_fn_macro = { git = "https://github.com/leptos-rs/leptos.git", rev = "b450f0f" }

# registration system
dashmap = "5"
Expand Down
2 changes: 2 additions & 0 deletions examples/todo_app_sqlite_viz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ http.workspace = true
futures.workspace = true
leptos.workspace = true
leptos_viz = { workspace = true, optional = true }
leptos_viz_macro = { workspace = true }
leptos_macro = { workspace = true, features = ["nightly"] }
leptos_meta = { workspace = true, features = ["nightly"] }
leptos_router = { workspace = true, features = ["nightly"] }
leptos_reactive = { workspace = true, features = ["nightly"] }
server_fn = { workspace = true, features = ["ssr", "serde-lite"] }

[features]
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
Expand Down
16 changes: 8 additions & 8 deletions examples/todo_app_sqlite_viz/src/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use cfg_if::cfg_if;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos_viz_macro::server;
use serde::{Deserialize, Serialize};
use server_fn::codec::SerdeLite;

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

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

if let Some(req_parts) = req_parts {
println!("Uri = {:?}", req_parts.uri);
if let Some(parts) = parts {
println!("Uri = {:?}", parts.uri);
}

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

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

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

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

Expand All @@ -97,7 +99,6 @@ pub fn TodoApp() -> impl IntoView {
//let id = use_context::<String>();
provide_meta_context();
view! {

<Link rel="shortcut icon" type_="image/ico" href="/favicon.ico"/>
<Stylesheet id="leptos" href="/pkg/todo_app_sqlite_viz.css"/>
<Router>
Expand Down Expand Up @@ -126,7 +127,6 @@ pub fn Todos() -> impl IntoView {
);

view! {

<div>
<MultiActionForm action=add_todo>
<label>
Expand Down
13 changes: 13 additions & 0 deletions leptos_viz_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "leptos_viz_macro"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
server_fn_macro.workspace = true
viz.workspace = true
syn = "2.0.48"
tracing = "0.1"
18 changes: 18 additions & 0 deletions leptos_viz_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use proc_macro::TokenStream;
use server_fn_macro::server_macro_impl;
use syn::__private::ToTokens;

#[proc_macro_attribute]
pub fn server(args: TokenStream, s: TokenStream) -> TokenStream {
match server_macro_impl(
args.into(),
s.into(),
Some(syn::parse_quote!(::leptos::server_fn)),
"/api",
Some(syn::parse_quote!(::viz::Request)),
Some(syn::parse_quote!(::viz::Response)),
) {
Err(e) => e.to_compile_error().into(),
Ok(s) => s.to_token_stream().into(),
}
}

0 comments on commit 7891e60

Please sign in to comment.