rstml

crates.io page docs.rs page codecov build license: MIT Rust templating for XML-based formats (HTML, SVG, MathML) implemented on top of proc-macro::TokenStreams. Similar to JSX but for Rust (commonly named RSX).The parsed result is a nested Node structure, similar to the browser DOM, where node name and value are syn expressions to support building proc macros.

The fork of original syn-rsx repo. It was created because of various reasons: - The author of original syn-rsx retired, end left new features without attention https://github.com/stoically/syn-rsx/issues/49 - Syn v2 was released and syn-rsx need to be mooved to new version. - The idea of lossless parsing was left unattended. - Unquoted text feature should advance. - Interest in recoverable parsing and better IDE support.

See comparsion for more detail.

```rust use std::convert::TryFrom;

use eyre::bail; use quote::quote; use rstml::{parse2, Node, NodeAttribute, NodeElement, NodeText};

// Create HTML TokenStream. let tokens = quote! { "hi" };

// Parse the tokens into a tree of Nodes. let nodes = parse2(tokens)?;

// Extract some specific nodes from the tree. let Node::Element(element) = &nodes[0] else { bail!("element") }; let Node::Attribute(attribute) = &element.attributes[0] else { bail!("attribute") }; let Node::Text(text) = &element.children[0] else { bail!("text") };

// Work with the nodes. asserteq!(element.name.tostring(), "hello"); asserteq!(attribute.key.tostring(), "world"); asserteq!(String::tryfrom(&text.value)?, "hi"); ```

You might want to check out the [html-to-string-macro example] as well.

Features