Skip to content

Commit

Permalink
Use example from inline docs in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey authored and vldm committed Oct 9, 2023
1 parent ff8c6c5 commit aa9d210
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use std::convert::TryFrom;

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

// Create HTML `TokenStream`.
let tokens = quote! { <hello world>"hi"</hello> };
Expand All @@ -33,14 +36,20 @@ let tokens = quote! { <hello world>"hi"</hello> };
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") };
let Node::Element(element) = &nodes[0] else {
bail!("element")
};
let NodeAttribute::Attribute(attribute) = &element.attributes()[0] else {
bail!("attribute")
};
let Node::Text(text) = &element.children[0] else {
bail!("text")
};

// Work with the nodes.
assert_eq!(element.name.to_string(), "hello");
assert_eq!(element.name().to_string(), "hello");
assert_eq!(attribute.key.to_string(), "world");
assert_eq!(String::try_from(&text.value)?, "hi");
assert_eq!(text.value_string(), "hi");
```

## Powered by rstml
Expand Down

0 comments on commit aa9d210

Please sign in to comment.