Add a match expression.
(&mut self, m: Match)
| 194 | |
| 195 | /// Add a match expression. |
| 196 | pub fn add_match(&mut self, m: Match) { |
| 197 | assert!(matches!(self.lang, Language::Rust)); |
| 198 | fmtln!(self, "match {} {{", m.expr); |
| 199 | self.indent(|fmt| { |
| 200 | for (&(ref fields, ref body), ref names) in m.arms.iter() { |
| 201 | // name { fields } | name { fields } => { body } |
| 202 | let conditions = names |
| 203 | .iter() |
| 204 | .map(|name| { |
| 205 | if !fields.is_empty() { |
| 206 | format!("{} {{ {} }}", name, fields.join(", ")) |
| 207 | } else { |
| 208 | name.clone() |
| 209 | } |
| 210 | }) |
| 211 | .collect::<Vec<_>>() |
| 212 | .join(" |\n") |
| 213 | + " => {"; |
| 214 | |
| 215 | fmt.multi_line(&conditions); |
| 216 | fmt.indent(|fmt| { |
| 217 | fmt.line(body); |
| 218 | }); |
| 219 | fmt.line("}"); |
| 220 | } |
| 221 | |
| 222 | // Make sure to include the catch all clause last. |
| 223 | if let Some(body) = m.catch_all { |
| 224 | fmt.line("_ => {"); |
| 225 | fmt.indent(|fmt| { |
| 226 | fmt.line(body); |
| 227 | }); |
| 228 | fmt.line("}"); |
| 229 | } |
| 230 | }); |
| 231 | self.line("}"); |
| 232 | } |
| 233 | |
| 234 | /// Write `self.lines` to a file. |
| 235 | pub fn write( |