use crate::inspect::Projection;
use super::{EditOutcome, RunFormat, Transaction, check_format};
impl Transaction<'_> {
pub(super) fn resolve_format_run(
&self,
projection: &Projection,
find: &str,
occurrence: Option<usize>,
format: &RunFormat,
outcome: &mut EditOutcome,
) -> Result<(usize, usize), (String, String)> {
if *format == RunFormat::default() {
return Err((
"INVALID_EDIT".into(),
"format_run needs at least one format field".into(),
));
}
check_format(format, find).map_err(|m| ("INVALID_EDIT".to_string(), m))?;
self.find_range(projection, find, occurrence, outcome)
}
}
pub(super) fn format_overlaps_edit(
(format_start, format_end): (usize, usize),
(start, end): (usize, usize),
) -> bool {
if start == end {
format_start < start && start < format_end
} else {
start < format_end && end > format_start
}
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
use super::format_overlaps_edit;
#[test]
fn insertions_at_the_edges_and_edits_beside_the_range_do_not_overlap() {
assert!(!format_overlaps_edit((4, 8), (4, 4)));
assert!(!format_overlaps_edit((4, 8), (8, 8)));
assert!(format_overlaps_edit((4, 8), (5, 5)));
assert!(!format_overlaps_edit((4, 8), (0, 4)));
assert!(!format_overlaps_edit((4, 8), (8, 10)));
assert!(format_overlaps_edit((4, 8), (7, 10)));
assert!(format_overlaps_edit((4, 8), (0, 5)));
assert!(format_overlaps_edit((4, 8), (2, 12)));
}
}