rename_section

This commit is contained in:
Edward Shen 2021-03-09 11:23:40 -05:00
parent b73d97f24a
commit 88fec2a7e4
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -743,9 +743,10 @@ impl<'event> GitConfig<'event> {
section_name: &'lookup str, section_name: &'lookup str,
subsection_name: impl Into<Option<&'lookup str>>, subsection_name: impl Into<Option<&'lookup str>>,
) -> Option<SectionBody> { ) -> Option<SectionBody> {
let section_ids = let id = self
self.get_section_ids_by_name_and_subname(section_name, subsection_name.into()); .get_section_ids_by_name_and_subname(section_name, subsection_name.into())
let id = section_ids.ok()?.pop()?; .ok()?
.pop()?;
self.section_order self.section_order
.remove(self.section_order.iter().position(|v| *v == id).unwrap()); .remove(self.section_order.iter().position(|v| *v == id).unwrap());
self.sections.remove(&id) 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 /// An intermediate representation of a mutable value obtained from