-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseable.rs
100 lines (88 loc) · 2.17 KB
/
parseable.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Implementations of `FromHtml` trait
use crate::extraction_method::{ExtractInnerText, ExtractionMethod, NoOp};
use crate::html::HtmlElement;
use crate::Error;
use crate::FromHtml;
pub trait Parseable: Sized {
type Input<N: HtmlElement>: ExtractedValue;
type Error: Error;
fn parse<N: HtmlElement>(input: Self::Input<N>) -> Result<Self, Self::Error>;
}
impl<T: FromHtml> Parseable for T {
type Input<N: HtmlElement> = N;
type Error = T::Error;
fn parse<N: HtmlElement>(input: Self::Input<N>) -> Result<Self, Self::Error> {
Self::from_html(input)
}
}
macro_rules! impl_parseable {
($($t:ty),*) => {
$(
impl Parseable for $t {
type Input<N: HtmlElement> = String;
type Error = <$t as ::std::str::FromStr>::Err;
fn parse<N: HtmlElement>(input: String) -> Result<Self, Self::Error> {
input.parse()
}
}
)*
};
}
impl_parseable!(
String,
// primitives
bool,
char,
usize,
u8,
u16,
u32,
u64,
u128,
isize,
i8,
i16,
i32,
i64,
i128,
f32,
f64,
// non-zero numerics
std::num::NonZeroU8,
std::num::NonZeroU16,
std::num::NonZeroU32,
std::num::NonZeroU64,
std::num::NonZeroU128,
std::num::NonZeroUsize,
std::num::NonZeroI8,
std::num::NonZeroI16,
std::num::NonZeroI32,
std::num::NonZeroI64,
std::num::NonZeroI128,
std::num::NonZeroIsize,
// misc that implements FromStr in the standard library
std::path::PathBuf,
std::net::IpAddr,
std::net::Ipv4Addr,
std::net::Ipv6Addr,
std::net::SocketAddr,
std::net::SocketAddrV4,
std::net::SocketAddrV6,
std::ffi::OsString
);
pub trait ExtractedValue {
type Default: ExtractionMethod;
fn default_method() -> Self::Default;
}
impl<N: HtmlElement> ExtractedValue for N {
type Default = NoOp;
fn default_method() -> Self::Default {
NoOp
}
}
impl ExtractedValue for String {
type Default = ExtractInnerText;
fn default_method() -> Self::Default {
ExtractInnerText
}
}