content_tree/
testrange.rs1use rle::{HasLength, MergableSpan, SplitableSpan, SplitableSpanHelpers};
2use crate::{Toggleable, ContentLength};
3use rle::Searchable;
4
5#[derive(Debug, Clone, Copy, Eq, PartialEq)]
7pub struct TestRange {
8 pub id: u32,
9 pub len: u32,
10 pub is_activated: bool,
11}
12
13impl Default for TestRange {
14 fn default() -> Self {
15 Self {
16 id: u32::MAX,
17 len: u32::MAX,
18 is_activated: false
19 }
20 }
21}
22
23impl HasLength for TestRange {
24 fn len(&self) -> usize { self.len as usize }
25}
26impl SplitableSpanHelpers for TestRange {
27 fn truncate_h(&mut self, at: usize) -> Self {
28 assert!(at > 0 && at < self.len as usize);
29 let other = Self {
30 id: self.id + at as u32,
31 len: self.len - at as u32,
32 is_activated: self.is_activated
33 };
34 self.len = at as u32;
35 other
36 }
37
38 fn truncate_keeping_right_h(&mut self, at: usize) -> Self {
39 let mut other = *self;
40 *self = other.truncate(at);
41 other
42 }
43}
44impl MergableSpan for TestRange {
45 fn can_append(&self, other: &Self) -> bool {
46 other.id == self.id + self.len && other.is_activated == self.is_activated
47 }
48
49 fn append(&mut self, other: Self) {
50 assert!(self.can_append(&other));
51 self.len += other.len;
52 }
53
54 fn prepend(&mut self, other: Self) {
55 assert!(other.can_append(self));
56 self.len += other.len;
57 self.id = other.id;
58 }
59}
60
61impl Toggleable for TestRange {
62 fn is_activated(&self) -> bool {
63 self.is_activated
64 }
65
66 fn mark_activated(&mut self) {
67 assert!(!self.is_activated);
68 self.is_activated = true;
69 }
70
71 fn mark_deactivated(&mut self) {
72 assert!(self.is_activated);
73 self.is_activated = false;
74 }
75}
76
77impl ContentLength for TestRange {
78 fn content_len(&self) -> usize {
79 if self.is_activated { self.len() } else { 0 }
80 }
81
82 fn content_len_at_offset(&self, offset: usize) -> usize {
83 assert!(offset <= self.len as usize);
84 if self.is_activated { offset } else { 0 }
85 }
86}
87
88impl Searchable for TestRange {
89 type Item = (u32, bool);
90
91 fn get_offset(&self, loc: Self::Item) -> Option<usize> {
92 if self.is_activated == loc.1 && loc.0 >= self.id && loc.0 < (self.id + self.len) {
93 Some((loc.0 - self.id) as usize)
94 } else { None }
95 }
96
97 fn at_offset(&self, offset: usize) -> Self::Item {
98 (offset as u32 + self.id, self.is_activated)
99 }
100}