Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotreload: Ability to update templates without recompiling #451

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
32df16f
hotreload prototype
wrapperup Sep 20, 2023
1c8ca91
add error handling with crappy panic unwind
wrapperup Sep 20, 2023
ebb7e86
re-export leon placeholder runtime templating
wrapperup Oct 1, 2023
e38f4de
Switch to proc_macro_error2
sbruder Sep 7, 2024
8cdedd8
Merge remote-tracking branch 'origin/main' into hotreload-prototype-v2
untitaker Nov 10, 2024
4d0a6b4
smaller fixes
untitaker Nov 10, 2024
69f014d
remove use of label to avoid compiler warnings
untitaker Nov 10, 2024
9b20dac
try to print a message
untitaker Nov 10, 2024
0c796c0
render div.foo { } correctly
untitaker Nov 10, 2024
3a7cc9a
make hotreload feature optional
untitaker Nov 10, 2024
aaeeb87
revert move to panics, tweak feature mgmt, fix warnings
untitaker Nov 10, 2024
a2b0c76
use render_to! for attribute values
untitaker Nov 10, 2024
594a67b
restore errors for compile-time maud
untitaker Nov 10, 2024
2e50953
fallback to static rendering for control structures
untitaker Nov 10, 2024
d9eeeff
some bugfixes
untitaker Nov 10, 2024
d4539e1
refactor
untitaker Nov 10, 2024
2ba0b9d
fix more bugs
untitaker Nov 10, 2024
73abc4d
deduplicate code
untitaker Nov 10, 2024
ba33b13
more refactoring
untitaker Nov 10, 2024
3279c40
fmt
untitaker Nov 10, 2024
e0083db
fix all errors
untitaker Nov 10, 2024
e0c8948
fix tests under hotreload flag as well
untitaker Nov 11, 2024
f3913b8
clippy
untitaker Nov 11, 2024
ffad273
refactor block and implement match
untitaker Nov 11, 2024
eecd6b6
Merge branch 'proc_macro_error2' into hotreload-prototype-v2
untitaker Nov 11, 2024
c71446b
merge fixup
untitaker Nov 11, 2024
6cc257b
add hotreload.rs
untitaker Nov 11, 2024
f934dc9
remove debug prints
untitaker Nov 11, 2024
9dc45a5
remove leon dependency
untitaker Nov 11, 2024
606e03a
wip on lazy eval
untitaker Nov 11, 2024
0064a6d
simplify source code scanning
untitaker Nov 11, 2024
87cbe6f
handle reordering of sourcecode better
untitaker Nov 11, 2024
6202b54
add MAUD_ON_ERROR
untitaker Nov 11, 2024
362f6bd
fix testsuite
untitaker Nov 17, 2024
bf62e4a
fall back to original source in more cases
untitaker Nov 17, 2024
f5b95df
Revert "fix testsuite"
untitaker Nov 17, 2024
999bb71
fix tests again
untitaker Nov 17, 2024
8bab5c5
fmt
untitaker Nov 17, 2024
0b5d700
remove invalid stub sourcecode
untitaker Nov 17, 2024
62c2356
update lockfile for docs
untitaker Nov 17, 2024
b0b4d74
nightly fmt
untitaker Nov 17, 2024
cfa9a5b
clippy
untitaker Nov 17, 2024
308f34e
fix syntax error
untitaker Nov 17, 2024
7174573
more clippy
untitaker Nov 17, 2024
f585e29
another clippy moment
untitaker Nov 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

- Switch to `proc_macro_error2`
[#441](https://github.com/lambda-fairy/maud/issues/441)
[#442](https://github.com/lambda-fairy/maud/pull/442)

## [0.26.0] - 2024-01-15

- Remove `AsRef<str>` restriction from `PreEscaped`
Expand Down
66 changes: 41 additions & 25 deletions docs/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 maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ edition.workspace = true

[features]
default = []
hotreload = ["maud_macros_impl/hotreload", "maud_macros/hotreload"]

# Web framework integrations
actix-web = ["actix-web-dep", "futures-util"]
axum = ["axum-core", "http"]

[dependencies]
maud_macros = { version = "0.26.0", path = "../maud_macros" }
maud_macros_impl = { version = "0.26.0", path = "../maud_macros_impl" }
itoa = "1"
rocket = { version = "0.5", optional = true }
futures-util = { version = "0.3.0", optional = true, default-features = false }
Expand Down
34 changes: 0 additions & 34 deletions maud/src/escape.rs

This file was deleted.

37 changes: 32 additions & 5 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(feature = "hotreload"), no_std)]

//! A macro for writing HTML templates.
//!
Expand All @@ -14,9 +14,13 @@ extern crate alloc;
use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc};
use core::fmt::{self, Arguments, Display, Write};

pub use maud_macros::html as html_static;

#[cfg(not(feature = "hotreload"))]
pub use maud_macros::html;

mod escape;
#[cfg(feature = "hotreload")]
pub use {maud_macros::html_hotreload, maud_macros::html_hotreload as html};

/// An adapter that escapes HTML special characters.
///
Expand Down Expand Up @@ -52,7 +56,7 @@ impl<'a> Escaper<'a> {

impl fmt::Write for Escaper<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
escape::escape_to_string(s, self.0);
maud_macros_impl::escape_to_string(s, self.0);
Ok(())
}
}
Expand Down Expand Up @@ -110,7 +114,7 @@ pub trait Render {

impl Render for str {
fn render_to(&self, w: &mut String) {
escape::escape_to_string(self, w);
maud_macros_impl::escape_to_string(self, w);
}
}

Expand Down Expand Up @@ -415,8 +419,12 @@ mod submillisecond_support {
#[doc(hidden)]
pub mod macro_private {
use crate::{display, Render};
use alloc::string::String;
pub use alloc::{boxed::Box, string::String, vec::Vec};
use core::fmt::Display;
#[cfg(feature = "hotreload")]
pub use std::collections::HashMap;
#[cfg(feature = "hotreload")]
pub use std::env::var as env_var;

#[doc(hidden)]
#[macro_export]
Expand Down Expand Up @@ -461,4 +469,23 @@ pub mod macro_private {
display(value).render_to(buffer);
}
}

#[cfg(feature = "hotreload")]
pub use maud_macros_impl::*;

#[cfg(feature = "hotreload")]
pub fn render_runtime_error(e: &str) -> crate::Markup {
eprintln!("{}", e);

match env_var("MAUD_ON_ERROR").as_deref() {
Ok("panic") => panic!("{}", e),
Ok("exit") => {
eprintln!("{}", e);
::std::process::exit(2);
}
_ => {}
}

crate::PreEscaped(alloc::format!("\"> --> <div style='background: black; color: white; position: absolute; top: 0; left: 0; z-index: 1000'><h1 style='red'>Template Errors:</h1><pre>{}</pre></div>", e))
}
}
63 changes: 63 additions & 0 deletions maud/tests/hotreload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! track regressions specific to the hotreload feature in maud
use maud::{html, Markup};
use maud_macros_impl::gather_html_macro_invocations;

#[test]
fn regression_match_inline_tag() {
fn render(x: Option<usize>) -> Markup {
html! {
div id="main" {
@match x {
Some(42) => div.green {
"yes! fourty! two!"
},
Some(_) => div.yellow {
"it's a number?"
},
None => div.red {
"okay."
},
}
}
}
}

assert_eq!(
render(Some(42)).into_string(),
r#"<div id="main"><div class="green">yes! fourty! two!</div></div>"#
);
assert_eq!(
render(Some(420)).into_string(),
r#"<div id="main"><div class="yellow">it's a number?</div></div>"#
);
assert_eq!(
render(None).into_string(),
r#"<div id="main"><div class="red">okay.</div></div>"#
);
}

#[test]
fn regression_basic() {
let result = html! {
"hello world"
};

assert_eq!(result.into_string(), "hello world");
}

#[test]
fn test_gather_html_macro_invocations() {
let file = file!();
let line = line!();

let _foo = maud::html! {
"Hello world"
};

assert_eq!(
gather_html_macro_invocations(file, line)
.unwrap()
.to_string(),
"\"Hello world\""
);
}
5 changes: 3 additions & 2 deletions maud/tests/warnings/keyword-without-at.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
error: found keyword `if`

= help: should this be a `@if`?

--> $DIR/keyword-without-at.rs:5:9
|
5 | if {}
| ^^
|
= help: should this be a `@if`?
Loading
Loading