Use MaybeSplit

This commit is contained in:
Austen Adler 2022-07-17 20:32:18 -04:00
parent b2d6e18c82
commit 85cb79c331

View File

@ -3,6 +3,13 @@ use std::{fmt, str::FromStr};
pub type Selection = String; pub type Selection = String;
#[derive(PartialEq, Eq, Debug)]
pub enum MaybeSplit<T> {
Nothing,
Just(T),
JustTwo(T, T),
}
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub struct SelectionWithDesc { pub struct SelectionWithDesc {
pub content: Selection, pub content: Selection,
@ -49,7 +56,7 @@ impl SelectionDesc {
} }
#[must_use] #[must_use]
pub fn subtract(&self, b: &Self) -> Vec<Self> { pub fn subtract(&self, b: &Self) -> MaybeSplit<Self> {
// let sorted_self = self.sort(); // let sorted_self = self.sort();
// let sorted_b = b.sort(); // let sorted_b = b.sort();
@ -71,16 +78,16 @@ impl SelectionDesc {
match (left_contained, right_contained, b_contained) { match (left_contained, right_contained, b_contained) {
(true, true, _) => { (true, true, _) => {
// self is contained by b // self is contained by b
vec![] MaybeSplit::Nothing
} }
(false, false, false) => { (false, false, false) => {
// There is no intersection // There is no intersection
// TODO: Why can't I clone myself? // TODO: Why can't I clone myself?
vec![self.clone()] MaybeSplit::Just(self.clone())
} }
(false, false, true) => { (false, false, true) => {
// B is contained and it does not intersect with left or right // B is contained and it does not intersect with left or right
vec![ MaybeSplit::JustTwo(
Self { Self {
left: self.left, left: self.left,
right: AnchorPosition { right: AnchorPosition {
@ -95,27 +102,27 @@ impl SelectionDesc {
}, },
right: self.right, right: self.right,
}, },
] )
} }
(true, false, _) => { (true, false, _) => {
// Only self's left is contained // Only self's left is contained
vec![Self { MaybeSplit::Just(Self {
left: AnchorPosition { left: AnchorPosition {
row: b.right.row, row: b.right.row,
col: b.right.col.saturating_add(1), col: b.right.col.saturating_add(1),
}, },
right: self.right, right: self.right,
}] })
} }
(false, true, _) => { (false, true, _) => {
// Only self's right is contained // Only self's right is contained
vec![Self { MaybeSplit::Just(Self {
left: self.left, left: self.left,
right: AnchorPosition { right: AnchorPosition {
row: b.left.row, row: b.left.row,
col: b.left.col.saturating_sub(1), col: b.left.col.saturating_sub(1),
}, },
}] })
} }
} }
} }
@ -585,61 +592,64 @@ mod test {
// 01234567 // 01234567
// a: ^_^ // a: ^_^
// b: ^____^ // b: ^____^
assert_eq!(sd!(1, 3).subtract(&sd!(0, 5)), vec![]); assert_eq!(sd!(1, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^__^ // a: ^__^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 3).subtract(&sd!(0, 5)), vec![]); assert_eq!(sd!(0, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^___^ // a: ^___^
// b: ^___^ // b: ^___^
assert_eq!(sd!(1, 5).subtract(&sd!(1, 5)), vec![]); assert_eq!(sd!(1, 5).subtract(&sd!(1, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^_____^ // a: ^_____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 6).subtract(&sd!(0, 5)), vec![sd!(6, 6)]); assert_eq!(sd!(0, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// 01234567 // 01234567
// a: ^____^ // a: ^____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(1, 6).subtract(&sd!(0, 5)), vec![sd!(6, 6)]); assert_eq!(sd!(1, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// 01234567 // 01234567
// a: ^____^ // a: ^____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 5).subtract(&sd!(1, 6)), vec![sd!(0, 0)]); assert_eq!(sd!(0, 5).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// 01234567 // 01234567
// a: ^______^ // a: ^______^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 7).subtract(&sd!(1, 6)), vec![sd!(0, 0), sd!(7, 7)]); assert_eq!(
sd!(0, 7).subtract(&sd!(1, 6)),
MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7))
);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(3, 3).subtract(&sd!(0, 5)), vec![]); assert_eq!(sd!(3, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 0).subtract(&sd!(0, 5)), vec![]); assert_eq!(sd!(0, 0).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 0).subtract(&sd!(1, 6)), vec![sd!(0, 0)]); assert_eq!(sd!(0, 0).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(5, 5).subtract(&sd!(0, 5)), vec![]); assert_eq!(sd!(5, 5).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(6, 6).subtract(&sd!(0, 5)), vec![sd!(6, 6)]); assert_eq!(sd!(6, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
} }
} }