syn-rsx

crates.io page docs.rs page codecov build license: MIT

[syn]-powered parser for JSX-like [TokenStream]s, aka 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.

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

use eyre::bail; use quote::quote; use syn_rsx::{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