From 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 14:41:41 +0200 Subject: Merging upstream version 1.70.0+dfsg2. Signed-off-by: Daniel Baumann --- vendor/gix-config/src/file/mutable/value.rs | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 vendor/gix-config/src/file/mutable/value.rs (limited to 'vendor/gix-config/src/file/mutable/value.rs') diff --git a/vendor/gix-config/src/file/mutable/value.rs b/vendor/gix-config/src/file/mutable/value.rs new file mode 100644 index 000000000..2bccfd32a --- /dev/null +++ b/vendor/gix-config/src/file/mutable/value.rs @@ -0,0 +1,63 @@ +use std::borrow::Cow; + +use bstr::BStr; + +use crate::{ + file, + file::{mutable::section::SectionMut, Index, Size}, + lookup, + parse::section, +}; + +/// An intermediate representation of a mutable value obtained from a [`File`][crate::File]. +#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +pub struct ValueMut<'borrow, 'lookup, 'event> { + pub(crate) section: SectionMut<'borrow, 'event>, + pub(crate) key: section::Key<'lookup>, + pub(crate) index: Index, + pub(crate) size: Size, +} + +impl<'borrow, 'lookup, 'event> ValueMut<'borrow, 'lookup, 'event> { + /// Returns the actual value. This is computed each time this is called + /// requiring an allocation for multi-line values. + pub fn get(&self) -> Result, lookup::existing::Error> { + self.section.get(&self.key, self.index, self.index + self.size) + } + + /// Update the value to the provided one. This modifies the value such that + /// the Value event(s) are replaced with a single new event containing the + /// new value. + pub fn set_string(&mut self, input: impl AsRef) { + self.set(input.as_ref()); + } + + /// Update the value to the provided one. This modifies the value such that + /// the Value event(s) are replaced with a single new event containing the + /// new value. + pub fn set<'a>(&mut self, input: impl Into<&'a BStr>) { + if self.size.0 > 0 { + self.section.delete(self.index, self.index + self.size); + } + self.size = self.section.set_internal(self.index, self.key.to_owned(), input.into()); + } + + /// Removes the value. Does nothing when called multiple times in + /// succession. + pub fn delete(&mut self) { + if self.size.0 > 0 { + self.section.delete(self.index, self.index + self.size); + self.size = Size(0); + } + } + + /// Return the section containing the value. + pub fn section(&self) -> &file::Section<'event> { + &self.section + } + + /// Convert this value into its owning mutable section. + pub fn into_section_mut(self) -> file::SectionMut<'borrow, 'event> { + self.section + } +} -- cgit v1.2.3