1 | macro_rules! try_next { |
2 | ($iter:expr, $($tt:tt)*) => { |
3 | match $iter.next() { |
4 | Some(e) => e, |
5 | None => return Err(crate::Error::InvalidFormat(format!($($tt)*))), |
6 | } |
7 | } |
8 | } |
9 | |
10 | macro_rules! try_text { |
11 | ($node:expr) => { |
12 | match $node.text() { |
13 | Some(t) => t, |
14 | None => return Err(crate::Error::InvalidFormat("Can't get text" .into())), |
15 | } |
16 | }; |
17 | } |
18 | |
19 | macro_rules! parse_attrs_opt { |
20 | ($node:expr, { $($key:expr => $lvalue:expr,)+ } $(, { $($str_key:expr => $str_lvalue:expr,)+ } )?) => { |
21 | for attr in $node.attributes() { |
22 | match attr.name() { |
23 | $( |
24 | $key => $lvalue = attr.value().parse().ok()?, |
25 | )+ |
26 | $( |
27 | $( |
28 | $str_key => $str_lvalue = attr.value().into(), |
29 | )+ |
30 | )? |
31 | _ => {} |
32 | } |
33 | } |
34 | }; |
35 | } |
36 | |
37 | macro_rules! parse_attrs { |
38 | ($node:expr, { $($key:expr => $lvalue:expr,)+ } $(, { $($str_key:expr => $str_lvalue:expr,)+ } )?) => { |
39 | for attr in $node.attributes() { |
40 | match attr.name() { |
41 | $( |
42 | $key => $lvalue = attr.value().parse()?, |
43 | )+ |
44 | $( |
45 | $( |
46 | $str_key => $str_lvalue = attr.value().into(), |
47 | )+ |
48 | )? |
49 | _ => {} |
50 | } |
51 | } |
52 | }; |
53 | } |
54 | |
55 | macro_rules! parse_enum { |
56 | ( |
57 | $ty:ty, |
58 | $( |
59 | ($variant:ident, $text:expr), |
60 | )+ |
61 | |$arg:ident| $fallback:expr, |
62 | ) => { |
63 | impl core::str::FromStr for $ty { |
64 | type Err = crate::Error; |
65 | |
66 | fn from_str($arg: &str) -> crate::Result<$ty> { |
67 | match $arg { |
68 | $( |
69 | $text => Ok(<$ty>::$variant), |
70 | )+ |
71 | _ => { |
72 | $fallback |
73 | } |
74 | } |
75 | } |
76 | } |
77 | }; |
78 | ( |
79 | $ty:ty, |
80 | $( |
81 | ($variant:ident, $text:expr), |
82 | )+ |
83 | ) => { |
84 | parse_enum! { |
85 | $ty, |
86 | $( |
87 | ($variant, $text), |
88 | )+ |
89 | |s| Err(crate::Error::ParseEnumError(core::any::type_name::<$ty>(), s.into())), |
90 | } |
91 | }; |
92 | } |
93 | |