use std::{borrow::Borrow, error::Error, fmt};
use r_dcf_syntax::{FieldName, InvalidFieldName, InvalidLogicalValue, LogicalValue};
use crate::{LineEnding, PackageRecord, Packages, builder::separate_record};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EditError {
RecordOutOfBounds {
index: usize,
},
FieldNotFound {
name: String,
},
FieldName(InvalidFieldName),
Value(InvalidLogicalValue),
}
impl fmt::Display for EditError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RecordOutOfBounds { index } => {
write!(formatter, "record index {index} is out of bounds")
}
Self::FieldNotFound { name } => write!(formatter, "field {name:?} was not found"),
Self::FieldName(error) => error.fmt(formatter),
Self::Value(error) => error.fmt(formatter),
}
}
}
impl Error for EditError {}
impl From<r_dcf_syntax::EditError> for EditError {
fn from(error: r_dcf_syntax::EditError) -> Self {
match error {
r_dcf_syntax::EditError::RecordOutOfBounds { index } => {
Self::RecordOutOfBounds { index }
}
r_dcf_syntax::EditError::FieldNotFound { name } => Self::FieldNotFound { name },
}
}
}
impl Packages {
pub fn replace_last(&self, record: usize, name: &str, value: &str) -> Result<Self, EditError> {
let value = LogicalValue::new(value).map_err(EditError::Value)?;
self.parse
.replace_last(record, name, &value)
.map(Self::from_parse)
.map_err(Into::into)
}
pub fn replace_all(&self, record: usize, name: &str, value: &str) -> Result<Self, EditError> {
let value = LogicalValue::new(value).map_err(EditError::Value)?;
self.parse
.replace_all(record, name, &value)
.map(Self::from_parse)
.map_err(Into::into)
}
pub fn insert_after(
&self,
record: usize,
after: &str,
name: &str,
value: &str,
) -> Result<Self, EditError> {
let name = FieldName::new(name).map_err(EditError::FieldName)?;
let value = LogicalValue::new(value).map_err(EditError::Value)?;
self.parse
.insert_after(record, after, &name, &value)
.map(Self::from_parse)
.map_err(Into::into)
}
pub fn remove_last(&self, record: usize, name: &str) -> Result<Self, EditError> {
self.parse
.remove_last(record, name)
.map(Self::from_parse)
.map_err(Into::into)
}
pub fn remove_all(&self, record: usize, name: &str) -> Result<Self, EditError> {
self.parse
.remove_all(record, name)
.map(Self::from_parse)
.map_err(Into::into)
}
pub fn append_record(
&self,
record: impl Borrow<PackageRecord>,
line_ending: LineEnding,
) -> Self {
let mut source = self.to_string();
separate_record(&mut source, line_ending);
source.push_str(&record.borrow().to_string());
Self::parse(&source)
}
pub fn remove_record(&self, index: usize) -> Result<Self, EditError> {
let records = self.parse.records().collect::<Vec<_>>();
let selected = records
.get(index)
.ok_or(EditError::RecordOutOfBounds { index })?;
let source = self.to_string();
let range = if records.len() == 1 {
selected.source_range().start..selected.source_range().end
} else if let Some(next) = records.get(index + 1) {
selected.source_range().start..next.source_range().start
} else {
records[index - 1].source_range().end..selected.source_range().end
};
let mut output = source;
output.replace_range(range, "");
Ok(Self::parse(&output))
}
fn from_parse(parse: r_dcf_syntax::Parse) -> Self {
Self { parse }
}
}