#unhtml #html #deserializor

unhtml

deserializor from html

9 releases (4 breaking)

✓ Uses Rust 2018 edition

0.6.0 Feb 12, 2019
0.5.1 Feb 1, 2019
0.4.0 Jan 8, 2019
0.3.1 Jan 8, 2019
0.1.2 Dec 5, 2018

#207 in Encoding

Download history 28/week @ 2019-01-13 9/week @ 2019-01-20 19/week @ 2019-01-27 8/week @ 2019-02-03 15/week @ 2019-02-10 4/week @ 2019-02-17 34/week @ 2019-02-24 27/week @ 2019-03-03 6/week @ 2019-03-10 6/week @ 2019-03-17 155/week @ 2019-03-24 4/week @ 2019-03-31 10/week @ 2019-04-07 3/week @ 2019-04-14 3/week @ 2019-04-21

114 downloads per month
Used in 3 crates

MIT license

23KB
341 lines

unhtml

Build status Crate version License: MIT Rust Docs

There are two trait in crate unhtml

  • FromHtml

The only method of FromHtml you should care about is fn from_html(html: &str) -> Result<Self, Error> and this method is implemented for all types implemented FromStr<E, T>

impl<E, T> FromHtml for T
    where E: failure::Fail,
          T: FromStr<Err=E> {
    fn from_html(html: &str) -> Result<Self, Error> {
        Ok(T::from_str(html.trim())?)
    }
}

You can implement FromHtml automatically for struct by crate unhtml_derive Crate version

  • VecFromHtml

VecFromHtml is implemented for Vec<T> where T: FromHtml by default

impl<T> VecFromHtml for Vec<T>
    where T: FromHtml {
    type Elem = T;
}

As FromHtml is implemented for u8 by default

use unhtml::scraper::Html;
use unhtml::VecFromHtml;
let html = Html::parse_fragment(r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="test">
        <a href="1"></a>
        <a href="2"></a>
        <a href="3"></a>
    </div>
</body>
</html>
"#);
let results = Vec::<u8>::from_attr("#test > a", "href", html.root_element()).unwrap();
assert_eq!(1u8, results[0]);
assert_eq!(2u8, results[1]);
assert_eq!(3u8, results[2]);

Dependencies

~3.5MB
~66K SLoC