clap/args/
macros.rs

1#[cfg(feature = "yaml")]
2macro_rules! yaml_tuple2 {
3    ($a:ident, $v:ident, $c:ident) => {{
4        if let Some(vec) = $v.as_vec() {
5            for ys in vec {
6                if let Some(tup) = ys.as_vec() {
7                    debug_assert_eq!(2, tup.len());
8                    $a = $a.$c(yaml_str!(tup[0]), yaml_str!(tup[1]));
9                } else {
10                    panic!("Failed to convert YAML value to vec");
11                }
12            }
13        } else {
14            panic!("Failed to convert YAML value to vec");
15        }
16        $a
17    }};
18}
19
20#[cfg(feature = "yaml")]
21macro_rules! yaml_tuple3 {
22    ($a:ident, $v:ident, $c:ident) => {{
23        if let Some(vec) = $v.as_vec() {
24            for ys in vec {
25                if let Some(tup) = ys.as_vec() {
26                    debug_assert_eq!(3, tup.len());
27                    $a = $a.$c(yaml_str!(tup[0]), yaml_opt_str!(tup[1]), yaml_str!(tup[2]));
28                } else {
29                    panic!("Failed to convert YAML value to vec");
30                }
31            }
32        } else {
33            panic!("Failed to convert YAML value to vec");
34        }
35        $a
36    }};
37}
38
39#[cfg(feature = "yaml")]
40macro_rules! yaml_vec_or_str {
41    ($v:ident, $a:ident, $c:ident) => {{
42        let maybe_vec = $v.as_vec();
43        if let Some(vec) = maybe_vec {
44            for ys in vec {
45                if let Some(s) = ys.as_str() {
46                    $a = $a.$c(s);
47                } else {
48                    panic!("Failed to convert YAML value {:?} to a string", ys);
49                }
50            }
51        } else {
52            if let Some(s) = $v.as_str() {
53                $a = $a.$c(s);
54            } else {
55                panic!(
56                    "Failed to convert YAML value {:?} to either a vec or string",
57                    $v
58                );
59            }
60        }
61        $a
62    }};
63}
64
65#[cfg(feature = "yaml")]
66macro_rules! yaml_opt_str {
67    ($v:expr) => {{
68        if $v.is_null() {
69            Some(
70                $v.as_str()
71                    .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)),
72            )
73        } else {
74            None
75        }
76    }};
77}
78
79#[cfg(feature = "yaml")]
80macro_rules! yaml_str {
81    ($v:expr) => {{
82        $v.as_str()
83            .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
84    }};
85}
86
87#[cfg(feature = "yaml")]
88macro_rules! yaml_to_str {
89    ($a:ident, $v:ident, $c:ident) => {{
90        $a.$c(yaml_str!($v))
91    }};
92}
93
94#[cfg(feature = "yaml")]
95macro_rules! yaml_to_bool {
96    ($a:ident, $v:ident, $c:ident) => {{
97        $a.$c($v
98            .as_bool()
99            .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)))
100    }};
101}
102
103#[cfg(feature = "yaml")]
104macro_rules! yaml_to_u64 {
105    ($a:ident, $v:ident, $c:ident) => {{
106        $a.$c($v
107            .as_i64()
108            .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
109            as u64)
110    }};
111}
112
113#[cfg(feature = "yaml")]
114macro_rules! yaml_to_usize {
115    ($a:ident, $v:ident, $c:ident) => {{
116        $a.$c($v
117            .as_i64()
118            .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
119            as usize)
120    }};
121}