1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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<Cow<'_, BStr>, 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<str>) {
        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
    }
}