summaryrefslogtreecommitdiffstats
path: root/vendor/gix-config/src/file/access/mutate.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/gix-config/src/file/access/mutate.rs')
-rw-r--r--vendor/gix-config/src/file/access/mutate.rs103
1 files changed, 69 insertions, 34 deletions
diff --git a/vendor/gix-config/src/file/access/mutate.rs b/vendor/gix-config/src/file/access/mutate.rs
index e1cfc6e1c..4844a34f4 100644
--- a/vendor/gix-config/src/file/access/mutate.rs
+++ b/vendor/gix-config/src/file/access/mutate.rs
@@ -18,10 +18,17 @@ impl<'event> File<'event> {
name: impl AsRef<str>,
subsection_name: Option<&BStr>,
) -> Result<SectionMut<'a, 'event>, lookup::existing::Error> {
+ self.section_mut_inner(name.as_ref(), subsection_name)
+ }
+
+ fn section_mut_inner<'a>(
+ &'a mut self,
+ name: &str,
+ subsection_name: Option<&BStr>,
+ ) -> Result<SectionMut<'a, 'event>, lookup::existing::Error> {
let id = self
- .section_ids_by_name_and_subname(name.as_ref(), subsection_name)?
- .rev()
- .next()
+ .section_ids_by_name_and_subname(name, subsection_name)?
+ .next_back()
.expect("BUG: Section lookup vec was empty");
let nl = self.detect_newline_style_smallvec();
Ok(self
@@ -65,7 +72,15 @@ impl<'event> File<'event> {
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
) -> Result<SectionMut<'a, 'event>, section::header::Error> {
- let name = name.as_ref();
+ self.section_mut_or_create_new_filter_inner(name.as_ref(), subsection_name, filter)
+ }
+
+ fn section_mut_or_create_new_filter_inner<'a>(
+ &'a mut self,
+ name: &str,
+ subsection_name: Option<&BStr>,
+ filter: &mut MetadataFilter,
+ ) -> Result<SectionMut<'a, 'event>, section::header::Error> {
match self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name)
.ok()
@@ -97,8 +112,17 @@ impl<'event> File<'event> {
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
) -> Result<Option<file::SectionMut<'a, 'event>>, lookup::existing::Error> {
+ self.section_mut_filter_inner(name.as_ref(), subsection_name, filter)
+ }
+
+ fn section_mut_filter_inner<'a>(
+ &'a mut self,
+ name: &str,
+ subsection_name: Option<&BStr>,
+ filter: &mut MetadataFilter,
+ ) -> Result<Option<file::SectionMut<'a, 'event>>, lookup::existing::Error> {
let id = self
- .section_ids_by_name_and_subname(name.as_ref(), subsection_name)?
+ .section_ids_by_name_and_subname(name, subsection_name)?
.rev()
.find(|id| {
let s = &self.sections[id];
@@ -131,10 +155,10 @@ impl<'event> File<'event> {
/// # use std::borrow::Cow;
/// # use gix_config::File;
/// # use std::convert::TryFrom;
- /// let mut gix_config = gix_config::File::default();
- /// let section = gix_config.new_section("hello", Some(Cow::Borrowed("world".into())))?;
+ /// let mut git_config = gix_config::File::default();
+ /// let section = git_config.new_section("hello", Some(Cow::Borrowed("world".into())))?;
/// let nl = section.newline().to_owned();
- /// assert_eq!(gix_config.to_string(), format!("[hello \"world\"]{nl}"));
+ /// assert_eq!(git_config.to_string(), format!("[hello \"world\"]{nl}"));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
@@ -146,13 +170,13 @@ impl<'event> File<'event> {
/// # use std::convert::TryFrom;
/// # use bstr::ByteSlice;
/// # use gix_config::parse::section;
- /// let mut gix_config = gix_config::File::default();
- /// let mut section = gix_config.new_section("hello", Some(Cow::Borrowed("world".into())))?;
+ /// let mut git_config = gix_config::File::default();
+ /// let mut section = git_config.new_section("hello", Some(Cow::Borrowed("world".into())))?;
/// section.push(section::Key::try_from("a")?, Some("b".into()));
/// let nl = section.newline().to_owned();
- /// assert_eq!(gix_config.to_string(), format!("[hello \"world\"]{nl}\ta = b{nl}"));
- /// let _section = gix_config.new_section("core", None);
- /// assert_eq!(gix_config.to_string(), format!("[hello \"world\"]{nl}\ta = b{nl}[core]{nl}"));
+ /// assert_eq!(git_config.to_string(), format!("[hello \"world\"]{nl}\ta = b{nl}"));
+ /// let _section = git_config.new_section("core", None);
+ /// assert_eq!(git_config.to_string(), format!("[hello \"world\"]{nl}\ta = b{nl}[core]{nl}"));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new_section(
@@ -160,6 +184,14 @@ impl<'event> File<'event> {
name: impl Into<Cow<'event, str>>,
subsection: impl Into<Option<Cow<'event, BStr>>>,
) -> Result<SectionMut<'_, 'event>, section::header::Error> {
+ self.new_section_inner(name.into(), subsection.into())
+ }
+
+ fn new_section_inner(
+ &mut self,
+ name: Cow<'event, str>,
+ subsection: Option<Cow<'event, BStr>>,
+ ) -> Result<SectionMut<'_, 'event>, section::header::Error> {
let id = self.push_section_internal(file::Section::new(name, subsection, OwnShared::clone(&self.meta))?);
let nl = self.detect_newline_style_smallvec();
let mut section = self.sections.get_mut(&id).expect("each id yields a section").to_mut(nl);
@@ -178,13 +210,13 @@ impl<'event> File<'event> {
/// ```
/// # use gix_config::File;
/// # use std::convert::TryFrom;
- /// let mut gix_config = gix_config::File::try_from(
+ /// let mut git_config = gix_config::File::try_from(
/// r#"[hello "world"]
/// some-value = 4
/// "#)?;
///
- /// let section = gix_config.remove_section("hello", Some("world".into()));
- /// assert_eq!(gix_config.to_string(), "");
+ /// let section = git_config.remove_section("hello", Some("world".into()));
+ /// assert_eq!(git_config.to_string(), "");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
@@ -193,27 +225,26 @@ impl<'event> File<'event> {
/// ```
/// # use gix_config::File;
/// # use std::convert::TryFrom;
- /// let mut gix_config = gix_config::File::try_from(
+ /// let mut git_config = gix_config::File::try_from(
/// r#"[hello "world"]
/// some-value = 4
/// [hello "world"]
/// some-value = 5
/// "#)?;
///
- /// let section = gix_config.remove_section("hello", Some("world".into()));
- /// assert_eq!(gix_config.to_string(), "[hello \"world\"]\n some-value = 4\n");
+ /// let section = git_config.remove_section("hello", Some("world".into()));
+ /// assert_eq!(git_config.to_string(), "[hello \"world\"]\n some-value = 4\n");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn remove_section<'a>(
&mut self,
- name: &str,
+ name: impl AsRef<str>,
subsection_name: impl Into<Option<&'a BStr>>,
) -> Option<file::Section<'event>> {
let id = self
- .section_ids_by_name_and_subname(name, subsection_name.into())
+ .section_ids_by_name_and_subname(name.as_ref(), subsection_name.into())
.ok()?
- .rev()
- .next()?;
+ .next_back()?;
self.remove_section_by_id(id)
}
@@ -256,12 +287,21 @@ impl<'event> File<'event> {
/// later sections with the same name have precedent over earlier ones.
pub fn remove_section_filter<'a>(
&mut self,
- name: &str,
+ name: impl AsRef<str>,
subsection_name: impl Into<Option<&'a BStr>>,
filter: &mut MetadataFilter,
) -> Option<file::Section<'event>> {
+ self.remove_section_filter_inner(name.as_ref(), subsection_name.into(), filter)
+ }
+
+ fn remove_section_filter_inner(
+ &mut self,
+ name: &str,
+ subsection_name: Option<&BStr>,
+ filter: &mut MetadataFilter,
+ ) -> Option<file::Section<'event>> {
let id = self
- .section_ids_by_name_and_subname(name, subsection_name.into())
+ .section_ids_by_name_and_subname(name, subsection_name)
.ok()?
.rev()
.find(|id| filter(self.sections.get(id).expect("each id has a section").meta()))?;
@@ -274,17 +314,13 @@ impl<'event> File<'event> {
self.sections.remove(&id)
}
- /// Adds the provided section to the config, returning a mutable reference
- /// to it for immediate editing.
+ /// Adds the provided `section` to the config, returning a mutable reference to it for immediate editing.
/// Note that its meta-data will remain as is.
- pub fn push_section(
- &mut self,
- section: file::Section<'event>,
- ) -> Result<SectionMut<'_, 'event>, section::header::Error> {
+ pub fn push_section(&mut self, section: file::Section<'event>) -> SectionMut<'_, 'event> {
let id = self.push_section_internal(section);
let nl = self.detect_newline_style_smallvec();
let section = self.sections.get_mut(&id).expect("each id yields a section").to_mut(nl);
- Ok(section)
+ section
}
/// Renames the section with `name` and `subsection_name`, modifying the last matching section
@@ -298,8 +334,7 @@ impl<'event> File<'event> {
) -> Result<(), rename_section::Error> {
let id = self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name.into())?
- .rev()
- .next()
+ .next_back()
.expect("list of sections were empty, which violates invariant");
let section = self.sections.get_mut(&id).expect("known section-id");
section.header = section::Header::new(new_name, new_subsection_name)?;