From 88fec2a7e4d5a6a7ae88d27c22bc5068803b0d24 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Tue, 9 Mar 2021 11:23:40 -0500 Subject: [PATCH] rename_section --- src/file.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/file.rs b/src/file.rs index b9b6eca..d2938fc 100644 --- a/src/file.rs +++ b/src/file.rs @@ -743,9 +743,10 @@ impl<'event> GitConfig<'event> { section_name: &'lookup str, subsection_name: impl Into>, ) -> Option { - 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>, + new_section_name: impl Into>, + new_subsection_name: impl Into>>, + ) -> 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