-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_field_value.rs
38 lines (31 loc) · 1.06 KB
/
custom_field_value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::num::ParseIntError;
use h2s::html::HtmlElement;
use h2s::parseable::Parseable;
use h2s::FromHtml;
fn main() {
// You can define an external parseable type yourself
// Currently you have to define a newtype for an external crate struct
struct Duration(std::time::Duration);
impl Parseable for Duration {
type Error = ParseIntError;
type Input<N: HtmlElement> = String;
fn parse<N: HtmlElement>(input: Self::Input<N>) -> Result<Self, Self::Error> {
let sec = input.parse()?;
Ok(Duration(std::time::Duration::from_secs(sec)))
}
}
#[derive(FromHtml)]
struct MyStruct {
#[h2s(select = "div")]
duration1: Duration,
#[h2s(select = "div", attr = "seconds")]
duration2: Duration,
}
let my_struct = h2s::parse::<MyStruct>(r#"<div seconds="456">123</div>"#).unwrap();
assert_eq!(my_struct.duration1.0, std::time::Duration::from_secs(123));
assert_eq!(my_struct.duration2.0, std::time::Duration::from_secs(456));
}
#[test]
fn test() {
main();
}