rename_section

master
Edward Shen 2021-03-09 11:23:40 -05:00
parent b73d97f24a
commit 88fec2a7e4
Signed by: edward
GPG Key ID: 19182661E818369F
1 changed files with 25 additions and 3 deletions

View File

@ -743,9 +743,10 @@ impl<'event> GitConfig<'event> {
section_name: &'lookup str,
subsection_name: impl Into<Option<&'lookup str>>,
) -> Option<SectionBody> {
let section_ids =
self.get_section_ids_by_name_and_subname(section_name, subsection_name.into());
let id = section_ids.ok()?.pop()?;
let id = self
.get_section_ids_by_name_and_subname(section_name, subsection_name.into())
.ok()?
.pop()?;
self.section_order
.remove(self.section_order.iter().position(|v| *v == id).unwrap());
self.sections.remove(&id)
@ -780,6 +781,27 @@ impl<'event> GitConfig<'event> {
)
}
}
/// Renames a section, modifying the last matching section.
///
/// # Errors
///
/// Returns an error if the lookup
pub fn rename_section<'lookup>(
&mut self,
section_name: &'lookup str,
subsection_name: impl Into<Option<&'lookup str>>,
new_section_name: impl Into<SectionHeaderName<'event>>,
new_subsection_name: impl Into<Option<Cow<'event, str>>>,
) -> Result<(), GitConfigError<'lookup>> {
let id = self.get_section_ids_by_name_and_subname(section_name, subsection_name.into())?;
let id = id.last().unwrap();
let header = self.section_headers.get_mut(id).unwrap();
header.name = new_section_name.into();
header.subsection_name = new_subsection_name.into();
Ok(())
}
}
/// An intermediate representation of a mutable value obtained from