1 #[cfg(feature = "yaml")] 2 macro_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")] 21 macro_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( 28 yaml_str!(tup[0]), 29 yaml_opt_str!(tup[1]), 30 yaml_opt_str!(tup[2]), 31 ); 32 } else { 33 panic!("Failed to convert YAML value to vec"); 34 } 35 } 36 } else { 37 panic!("Failed to convert YAML value to vec"); 38 } 39 $a 40 }}; 41 } 42 43 #[cfg(feature = "yaml")] 44 macro_rules! yaml_vec_or_str { 45 ($a:ident, $v:ident, $c:ident) => {{ 46 let maybe_vec = $v.as_vec(); 47 if let Some(vec) = maybe_vec { 48 for ys in vec { 49 if let Some(s) = ys.as_str() { 50 $a = $a.$c(s); 51 } else { 52 panic!("Failed to convert YAML value {:?} to a string", ys); 53 } 54 } 55 } else { 56 if let Some(s) = $v.as_str() { 57 $a = $a.$c(s); 58 } else { 59 panic!( 60 "Failed to convert YAML value {:?} to either a vec or string", 61 $v 62 ); 63 } 64 } 65 $a 66 }}; 67 } 68 69 #[cfg(feature = "yaml")] 70 macro_rules! yaml_vec { 71 ($a:ident, $v:ident, $c:ident) => {{ 72 let maybe_vec = $v.as_vec(); 73 if let Some(vec) = maybe_vec { 74 let content = vec.into_iter().map(|ys| { 75 if let Some(s) = ys.as_str() { 76 s 77 } else { 78 panic!("Failed to convert YAML value {:?} to a string", ys); 79 } 80 }); 81 $a = $a.$c(content) 82 } else { 83 panic!("Failed to convert YAML value {:?} to a vec", $v); 84 } 85 $a 86 }}; 87 } 88 89 #[cfg(feature = "yaml")] 90 macro_rules! yaml_opt_str { 91 ($v:expr) => {{ 92 if !$v.is_null() { 93 Some( 94 $v.as_str() 95 .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)), 96 ) 97 } else { 98 None 99 } 100 }}; 101 } 102 103 #[cfg(feature = "yaml")] 104 macro_rules! yaml_char { 105 ($v:expr) => {{ 106 $v.as_str() 107 .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)) 108 .chars() 109 .next() 110 .unwrap_or_else(|| panic!("Expected char")) 111 }}; 112 } 113 114 #[cfg(feature = "yaml")] 115 macro_rules! yaml_str { 116 ($v:expr) => {{ 117 $v.as_str() 118 .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)) 119 }}; 120 } 121 122 #[cfg(feature = "yaml")] 123 macro_rules! yaml_to_char { 124 ($a:ident, $v:ident, $c:ident) => {{ 125 $a.$c(yaml_char!($v)) 126 }}; 127 } 128 129 #[cfg(feature = "yaml")] 130 macro_rules! yaml_to_str { 131 ($a:ident, $v:ident, $c:ident) => {{ 132 $a.$c(yaml_str!($v)) 133 }}; 134 } 135 136 #[cfg(feature = "yaml")] 137 macro_rules! yaml_to_bool { 138 ($a:ident, $v:ident, $c:ident) => {{ 139 $a.$c($v 140 .as_bool() 141 .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))) 142 }}; 143 } 144 145 #[cfg(feature = "yaml")] 146 macro_rules! yaml_to_usize { 147 ($a:ident, $v:ident, $c:ident) => {{ 148 $a.$c($v 149 .as_i64() 150 .unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v)) 151 as usize) 152 }}; 153 } 154 155 #[cfg(feature = "yaml")] 156 macro_rules! yaml_to_setting { 157 ($a:ident, $v:ident, $c:ident, $s:ident, $t:literal, $n:expr) => {{ 158 if let Some(v) = $v.as_vec() { 159 for ys in v { 160 if let Some(s) = ys.as_str() { 161 $a = $a.$c(s.parse::<$s>().unwrap_or_else(|_| { 162 panic!("Unknown {} '{}' found in YAML file for {}", $t, s, $n) 163 })); 164 } else { 165 panic!( 166 "Failed to convert YAML {:?} value to an array of strings", 167 $v 168 ); 169 } 170 } 171 } else if let Some(v) = $v.as_str() { 172 $a = $a.$c(v 173 .parse::<$s>() 174 .unwrap_or_else(|_| panic!("Unknown {} '{}' found in YAML file for {}", $t, v, $n))) 175 } else { 176 panic!("Failed to convert YAML {:?} value to a string", $v); 177 } 178 $a 179 }}; 180 } 181