Skip to content

Commit

Permalink
fix implicit return in htmx-script
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Sep 15, 2023
1 parent 1354549 commit a4eb1b6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ html-escape = "0.2.13"
forr = "0.2"
actix-web = { version = "4.4.0", default-features = false, optional = true }
axum-core = { version = "0.3.4", optional = true }
serde = "1.0.188"
serde_json = "1.0.107"

[dev-dependencies]
insta = "1.31.0"
Expand Down
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/ModProg/htmx"
documentation = "https://docs.rs/htmx"

[features]
# default = ["actix"]
default = ["actix"]
axum = ["htmx/axum", "dep:tokio", "dep:axum"]
tauri = ["dep:tauri-runtime", "dep:tauri-runtime-wry", "dep:url"]
actix = ["htmx/actix-web", "dep:actix-web"]
Expand Down
5 changes: 4 additions & 1 deletion example/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use htmx::{htmx, HtmxSrc};

#[get("/")]
async fn index() -> impl Responder {
let rust_str = ["hello", "world", "!"];
htmx! {
<head>
<HtmxSrc/>
<script>
fn hello_function() {
alert("RUSTY JS!!!!")
console.log($rust_str);
let rust_str = $rust_str;
alert($"RUSTY JS!!!! ${rust_str.join(' ')}");
}
</script>
</head>
Expand Down
5 changes: 3 additions & 2 deletions htmx-script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ToTokens for JsTokens {
let mut last_verbatum = mem::take(&mut last_verbatum);
last_verbatum.push(' ');
js_tokens.push(quote!($out.push_str(#last_verbatum)));
js_tokens.push(quote!($out.push_str(#ident.to_js())))
js_tokens.push(quote!($out.push_str(#ident.to_js().as_str())))
}
}
}
Expand Down Expand Up @@ -140,11 +140,11 @@ impl ToJs for Stmt {
Stmt::Binding(b) => b.to_js(js),
Stmt::Item(i) => i.to_js(js),
Stmt::Expr(e, None) => {
"return".to_js(js);
e.to_js(js);
";".to_js(js);
}
Stmt::Expr(e, Some(_)) => {
"return".to_js(js);
e.to_js(js);
";".to_js(js);
}
Expand Down Expand Up @@ -791,6 +791,7 @@ fn basic() -> syn::Result<()> {
fn on_click(event) {
// TODO support rust in template strings
let name = $name;
console.log($name);
alert($"Hi ${name} you triggered an event ${event.type}");
}
};
Expand Down
2 changes: 1 addition & 1 deletion htmx-script/src/snapshots/htmx_script__basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: htmx-script/src/lib.rs
expression: ast.to_java_script().to_token_stream().to_string()
---
{ use :: htmx :: ToJs as _ ; let mut __htmx_script_out = :: std :: prelude :: v1 :: String :: new () ; __htmx_script_out . push_str (" function on_click ( event , ) { const name = ") ; __htmx_script_out . push_str (name . to_js ()) ; __htmx_script_out . push_str (" ; return alert ( `Hi ${name} you triggered an event ${event.type}` , ) ; }") ; __htmx_script_out }
{ use :: htmx :: ToJs as _ ; let mut __htmx_script_out = :: std :: prelude :: v1 :: String :: new () ; __htmx_script_out . push_str (" function on_click ( event , ) { const name = ") ; __htmx_script_out . push_str (name . to_js () . as_str ()) ; __htmx_script_out . push_str (" ; console . log ( ") ; __htmx_script_out . push_str (name . to_js () . as_str ()) ; __htmx_script_out . push_str (" , ) ; alert ( `Hi ${name} you triggered an event ${event.type}` , ) ; }") ; __htmx_script_out }
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const DOCTYPE: &str = "<!DOCTYPE html>";
pub struct Html(String);

pub trait ToJs {
// TODO
fn to_js(&self) -> String;
}

impl<T: Serialize> ToJs for T {
fn to_js(&self) -> String {
serde_json::to_string(self).expect("Serialization shouldn't fail.")
}
}

impl Html {
Expand Down Expand Up @@ -170,3 +176,4 @@ mod axum {
}
#[cfg(feature = "axum")]
pub use axum::*;
use serde::Serialize;

0 comments on commit a4eb1b6

Please sign in to comment.