https://github.com/sof3/defy
Replacement for the `yew::html!` macro with more Rust-idiomatic, editor-friendly syntax.
https://github.com/sof3/defy
Last synced: 10 months ago
JSON representation
Replacement for the `yew::html!` macro with more Rust-idiomatic, editor-friendly syntax.
- Host: GitHub
- URL: https://github.com/sof3/defy
- Owner: SOF3
- License: apache-2.0
- Created: 2023-02-26T09:49:08.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-30T03:59:06.000Z (over 2 years ago)
- Last Synced: 2025-02-28T04:08:02.228Z (11 months ago)
- Language: Rust
- Size: 30.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# defy
[](https://github.com/SOF3/defy/actions?query=workflow%3ACI)
[](https://crates.io/crates/defy)
[](https://crates.io/crates/defy)
[](https://docs.rs/defy)
[](https://github.com/SOF3/defy)
[](https://github.com/SOF3/defy)
Replacement for the [`yew::html!` macro](https://docs.rs/yew/latest/yew/macro.html.html)
with more Rust-idiomatic, editor-friendly syntax.
The syntax used in this crate is largely inspired by
[kotlinx.html](https://github.com/Kotlin/kotlinx.html) and
[horrorshow](https://docs.rs/horrorshow).
## Example
```rust
use defy::defy;
struct Datum {
field: &'static str,
display: bool,
label: Label,
}
enum Label {
First(i32),
Second(u64),
}
let data = vec![
Datum { field: "foo", display: false, label: Label::First(1) },
Datum { field: "bar", display: true, label: Label::Second(2) },
];
let vnode = defy! {
h1 {
+ "Hello world";
}
ul {
for datum in data {
let field = datum.field;
if datum.display {
li(data-length = field.len().to_string()) {
+ field;
}
}
match datum.label {
Label::First(i) if i > 3 => {
h2 { +i; }
}
Label::Second(i) => {
h3 { +i; }
}
_ => { +"unmatched"; }
}
}
}
};
// Rendering code omitted
assert_eq!(
vnode_html.as_str().replace(['\n', ' '], ""),
r#"
Hello world
- bar
unmatched
2
"#
.replace(['\n', ' '], "")
);
```
## Why invent another syntax?
Yew already provides several editor plugins
to help make editors treat `html!` blocks as HTML syntax.
However the editor covergae is not complete,
and interacts weirdly with normal Rust syntax.
`defy` uses a syntax that resembles the normal Rust syntax
(a similar idea as [ron](https://github.com/ron-rs/ron))
and provides better syntactic sugar for constructs like for loops.