Parse the next statement in the file.
(input: &mut &'s str)
| 318 | |
| 319 | /// Parse the next statement in the file. |
| 320 | fn statement<'s>(input: &mut &'s str) -> ModalResult<Statement<'s>> { |
| 321 | // First, we figure out what kind of line we're looking at. |
| 322 | let statement_type: String = alt(( |
| 323 | // A Path statement line |
| 324 | // |
| 325 | // Path statements may be preceded with whitespaces. |
| 326 | // Otherwise read the line until terminated by the first space or newline. |
| 327 | // Whitespace characters are encoded as `\s' (space), `\t' (tab), and `\n' (new line) |
| 328 | // which is why we can simply ignore those while parsing the path. |
| 329 | preceded( |
| 330 | space0, |
| 331 | terminated((".", take_until(0.., " ")).take(), alt((' ', '\n'))), |
| 332 | ).and_then(decode_utf8_chars), |
| 333 | terminated("/set", " ").map(|s: &str| s.to_string()), |
| 334 | terminated("/unset", " ").map(|s: &str| s.to_string()), |
| 335 | // A comment line that starts with `#`. |
| 336 | preceded(("#", take_until(0.., "\n")), line_ending).map(|s: &str| s.to_string()), |
| 337 | // An empty line that possibly contains spaces. |
| 338 | preceded(space0, line_ending).map(|s: &str| s.to_string()), |
| 339 | // If none of the above match, fail hard with a correct error message. |
| 340 | fail.context(StrContext::Label("statement")) |
| 341 | .context(StrContext::Expected(StrContextValue::Description( |
| 342 | "'/set', '/unset', or a relative local path (./some/path) followed by their respective properties.", |
| 343 | ))) |
| 344 | )) |
| 345 | .parse_next(input)?; |
| 346 | |
| 347 | // Ignore comments and empty lines. |
| 348 | if statement_type.trim().is_empty() { |
| 349 | return Ok(Statement::Ignored); |
| 350 | } |
| 351 | |
| 352 | // Now parse the properties based on the statement type until the end of line. |
| 353 | let statement = match statement_type.as_str() { |
| 354 | "/set" => Statement::Set(set_properties.parse_next(input)?), |
| 355 | "/unset" => Statement::Unset(unset_properties.parse_next(input)?), |
| 356 | path => Statement::Path { |
| 357 | path: PathBuf::from(path), |
| 358 | properties: properties.parse_next(input)?, |
| 359 | }, |
| 360 | }; |
| 361 | |
| 362 | Ok(statement) |
| 363 | } |
| 364 | |
| 365 | /// Parse a given .MTREE file. |
| 366 | /// |