gix_config/file/mutable/
value.rs1use std::borrow::Cow;
2
3use bstr::BStr;
4
5use crate::{
6 file,
7 file::{mutable::section::SectionMut, Index, Size},
8 lookup,
9 parse::section,
10};
11
12#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
14pub struct ValueMut<'borrow, 'lookup, 'event> {
15 pub(crate) section: SectionMut<'borrow, 'event>,
16 pub(crate) key: section::ValueName<'lookup>,
17 pub(crate) index: Index,
18 pub(crate) size: Size,
19}
20
21impl<'borrow, 'event> ValueMut<'borrow, '_, 'event> {
22 pub fn get(&self) -> Result<Cow<'_, BStr>, lookup::existing::Error> {
25 self.section.get(&self.key, self.index, self.index + self.size)
26 }
27
28 pub fn set_string(&mut self, input: impl AsRef<str>) {
32 self.set(input.as_ref());
33 }
34
35 pub fn set<'a>(&mut self, input: impl Into<&'a BStr>) {
39 if self.size.0 > 0 {
40 self.section.delete(self.index, self.index + self.size);
41 }
42 self.size = self.section.set_internal(self.index, self.key.to_owned(), input.into());
43 }
44
45 pub fn delete(&mut self) {
48 if self.size.0 > 0 {
49 self.section.delete(self.index, self.index + self.size);
50 self.size = Size(0);
51 }
52 }
53
54 pub fn section(&self) -> &file::Section<'event> {
56 &self.section
57 }
58
59 pub fn into_section_mut(self) -> file::SectionMut<'borrow, 'event> {
61 self.section
62 }
63}