use bstr::BString;
use crate::{
file,
file::{Index, Size, mutable::section::SectionMut},
lookup,
parse::section,
};
#[derive(Debug)]
pub struct ValueMut<'borrow> {
pub(crate) section: SectionMut<'borrow>,
pub(crate) key: section::ValueName,
pub(crate) index: Index,
pub(crate) size: Size,
}
impl<'borrow> ValueMut<'borrow> {
pub fn get(&self) -> Result<BString, lookup::existing::Error> {
self.section.get(&self.key, self.index, self.index + self.size)
}
pub fn set_string(&mut self, input: impl AsRef<str>) -> Result<(), crate::parse::span::Error> {
self.set(input.as_ref())
}
pub fn set(&mut self, input: impl crate::AsBStr) -> Result<(), crate::parse::span::Error> {
let new_size = self
.section
.set_internal(self.index, self.key.to_owned(), input.as_bstr())?;
if self.size.0 > 0 {
self.section
.delete(self.index + new_size, self.index + new_size + self.size);
}
self.size = new_size;
Ok(())
}
pub fn delete(&mut self) {
if self.size.0 > 0 {
self.section.delete(self.index, self.index + self.size);
self.size = Size(0);
}
}
pub fn section(&self) -> file::SectionRef<'_> {
self.section.section()
}
pub fn into_section_mut(self) -> file::SectionMut<'borrow> {
self.section
}
}