defy

Crates.iodefy
lib.rsdefy
version0.1.5
sourcesrc
created_at2023-02-26 09:48:39.661996
updated_at2023-04-14 16:20:24.106787
descriptionReplacement for the yew::html! macro with more Rust-idiomatic, editor-friendly syntax.
homepage
repositoryhttps://github.com/SOF3/defy
max_upload_size
id794936
size36,239
Jonathan Chan Kwan Yin (SOF3)

documentation

README

defy

GitHub actions crates.io crates.io docs.rs GitHub GitHub

Replacement for the yew::html! macro with more Rust-idiomatic, editor-friendly syntax.

The syntax used in this crate is largely inspired by kotlinx.html and horrorshow.

Example

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#"
        <h1>Hello world</h1>
        <ul>
            unmatched
            <li data-length="3">bar</li>
            <h3>2</h3>
        </ul>
    "#
    .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) and provides better syntactic sugar for constructs like for loops.

Commit count: 8

cargo fmt