A declarative HTML parser library in Rust, which works like a deserializer from HTML to struct.
```rust use h2s::FromHtml;
pub struct Page {
#[h2s(attr = "lang")]
lang: String,
#[h2s(select = "div > h1.blog-title")]
blog_title: String,
#[h2s(select = ".articles > div")]
articles: Vec
pub struct Article {
#[h2s(select = "h2 > a")]
title: String,
#[h2s(select = "div > span")]
viewcount: usize,
#[h2s(select = "h2 > a", attr = "href")]
url: String,
#[h2s(select = "ul > li")]
tags: Vec
let html = r#"
"#;let page = h2s::parse::
asserteq!(page, Page { lang: "en".tostring(), blogtitle: "My tech blog".tostring(), articles: vec![ Article { title: "article1".tostring(), url: "https://example.com/1".tostring(), viewcount: 901, tags: vec!["Tag1".tostring(), "Tag2".tostring()], firsttag: Some("Tag1".tostring()), }, Article { title: "article2".tostring(), url: "https://example.com/2".tostring(), viewcount: 849, tags: vec![], firsttag: None, }, Article { title: "article3".tostring(), url: "https://example.com/3".tostring(), viewcount: 103, tags: vec!["Tag3".tostring()], firsttag: Some("Tag3".to_string()), }, ] });
// When the input HTML document structure does not match the expected,
// h2s::parse
will return an error with a detailed reason.
let invalidhtml = html.replace(r#"article3"#, "");
let err = h2s::parse::
You can use the following types as a field value of the struct to parse.
String
usize
, i64
, NonZeroU32
, ... )T
is a basic type)[T;N]
Option<T>
Vec<T>
MIT