-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.rs
92 lines (87 loc) · 2.29 KB
/
util.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
macro_rules! try_next {
($iter:expr, $($tt:tt)*) => {
match $iter.next() {
Some(e) => e,
None => return Err(crate::Error::InvalidFormat(format!($($tt)*))),
}
}
}
macro_rules! try_text {
($node:expr) => {
match $node.text() {
Some(t) => t,
None => return Err(crate::Error::InvalidFormat("Can't get text".into())),
}
};
}
macro_rules! parse_attrs_opt {
($node:expr, { $($key:expr => $lvalue:expr,)+ } $(, { $($str_key:expr => $str_lvalue:expr,)+ } )?) => {
for attr in $node.attributes() {
match attr.name() {
$(
$key => $lvalue = attr.value().parse().ok()?,
)+
$(
$(
$str_key => $str_lvalue = attr.value().into(),
)+
)?
_ => {}
}
}
};
}
macro_rules! parse_attrs {
($node:expr, { $($key:expr => $lvalue:expr,)+ } $(, { $($str_key:expr => $str_lvalue:expr,)+ } )?) => {
for attr in $node.attributes() {
match attr.name() {
$(
$key => $lvalue = attr.value().parse()?,
)+
$(
$(
$str_key => $str_lvalue = attr.value().into(),
)+
)?
_ => {}
}
}
};
}
macro_rules! parse_enum {
(
$ty:ty,
$(
($variant:ident, $text:expr),
)+
|$arg:ident| $fallback:expr,
) => {
impl core::str::FromStr for $ty {
type Err = crate::Error;
fn from_str($arg: &str) -> crate::Result<$ty> {
match $arg {
$(
$text => Ok(<$ty>::$variant),
)+
_ => {
$fallback
}
}
}
}
};
(
$ty:ty,
$(
($variant:ident, $text:expr),
)+
) => {
parse_enum! {
$ty,
$(
($variant, $text),
)+
|s| Err(crate::Error::ParseEnumError(core::any::type_name::<$ty>(), s.into())),
}
};
}