Unified · Split
| @@ -463,6 +463,72 @@ impl<'event> GitConfig<'event> { · 462 unmodified lines | ||
| 463 | 463 | .collect() |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | + /// Get all sections that match the `section_name`, returning all matching section header along with their body. | |
| 467 | + /// | |
| 468 | + /// An empty `Vec` is returned if there is no section with `section_name`. | |
| 469 | + /// | |
| 470 | + /// # Example | |
| 471 | + /// | |
| 472 | + /// Provided the following config: | |
| 473 | + /// ```plain | |
| 474 | + /// [url "ssh://git@github.com/"] | |
| 475 | + /// insteadOf = https://github.com/ | |
| 476 | + /// [url "ssh://git@bitbucket.org"] | |
| 477 | + /// insteadOf = https://bitbucket.org/ | |
| 478 | + /// ``` | |
| 479 | + /// Calling this method will yield all section bodies and their header: | |
| 480 | + /// | |
| 481 | + /// ```rust | |
| 482 | + /// use git_config::file::{GitConfig, GitConfigError}; | |
| 483 | + /// use git_config::parser::Key; | |
| 484 | + /// use std::borrow::Cow; | |
| 485 | + /// use std::convert::TryFrom; | |
| 486 | + /// use nom::AsBytes; | |
| 487 | + /// | |
| 488 | + /// let input = r#" | |
| 489 | + /// [url "ssh://git@github.com/"] | |
| 490 | + /// insteadOf = https://github.com/ | |
| 491 | + /// [url "ssh://git@bitbucket.org"] | |
| 492 | + /// insteadOf = https://bitbucket.org/ | |
| 493 | + /// "#; | |
| 494 | + /// let config = GitConfig::try_from(input).unwrap(); | |
| 495 | + /// let url = config.sections_by_name_with_header("url"); | |
| 496 | + /// assert_eq!(url.len(), 2); | |
| 497 | + /// | |
| 498 | + /// for (i, (header, body)) in url.iter().enumerate() { | |
| 499 | + /// let url = header.subsection_name.as_ref(); | |
| 500 | + /// let instead_of = body.value(&Key::from("insteadOf")); | |
| 501 | + /// | |
| 502 | + /// // todo(unstable-order): the order is not always the same, so `i` cannot be used here | |
| 503 | + /// if instead_of.as_ref().unwrap().as_ref().as_bytes().eq("https://github.com/".as_bytes()) { | |
| 504 | + /// assert_eq!(instead_of.unwrap().as_ref(), "https://github.com/".as_bytes()); | |
| 505 | + /// assert_eq!(url.unwrap().as_ref(), "ssh://git@github.com/"); | |
| 506 | + /// } else { | |
| 507 | + /// assert_eq!(instead_of.unwrap().as_ref(), "https://bitbucket.org/".as_bytes()); | |
| 508 | + /// assert_eq!(url.unwrap().as_ref(), "ssh://git@bitbucket.org"); | |
| 509 | + /// } | |
| 510 | + /// } | |
| 511 | + /// ``` | |
| 512 | + pub fn sections_by_name_with_header<'lookup>( | |
| 513 | + &self, | |
| 514 | + section_name: &'lookup str, | |
| 515 | + ) -> Vec<(&ParsedSectionHeader<'event>, &SectionBody<'event>)> { | |
| 516 | + self.get_section_ids_by_name(section_name) | |
| 517 | + .unwrap_or_default() | |
| 518 | + .into_iter() | |
| 519 | + .map(|id| { | |
| 520 | + ( | |
| 521 | + self.section_headers | |
| 522 | + .get(&id) | |
| 523 | + .expect("section doesn't have a section header??"), | |
| 524 | + self.sections | |
| 525 | + .get(&id) | |
| 526 | + .expect("section doesn't have id from from lookup"), | |
| 527 | + ) | |
| 528 | + }) | |
| 529 | + .collect() | |
| 530 | + } | |
| 531 | + | |
| 466 | 532 | /// Adds a new section to config. If a subsection name was provided, then |
| 467 | 533 | /// the generated header will use the modern subsection syntax. Returns a |
| 468 | 534 | /// reference to the new section for immediate editing. |