crdt_richtext/legacy/
range_map.rs1use std::{collections::BTreeSet, ops::Range, sync::Arc};
2pub mod tree_impl;
3
4use crate::{Annotation, Lamport, OpID};
5
6pub trait RangeMap {
7 fn init() -> Self;
8 fn insert<F>(&mut self, pos: usize, len: usize, f: F)
10 where
11 F: FnMut(&Annotation) -> AnnPosRelativeToInsert;
12 fn insert_directly(&mut self, pos: usize, len: usize) {
13 self.insert(pos, len, |_| AnnPosRelativeToInsert::IncludeInsert);
14 }
15 fn delete(&mut self, pos: usize, len: usize);
16 fn annotate(&mut self, pos: usize, len: usize, annotation: Annotation);
17 fn adjust_annotation(
19 &mut self,
20 target_id: OpID,
21 lamport: Lamport,
22 patch_id: OpID,
23 start_shift: Option<(isize, Option<OpID>)>,
24 end_shift: Option<(isize, Option<OpID>)>,
25 );
26 fn delete_annotation(&mut self, id: OpID);
27 fn get_annotations(&mut self, pos: usize, len: usize) -> Vec<Span>;
29 fn get_annotation_pos(&self, id: OpID) -> Option<(Arc<Annotation>, Range<usize>)>;
30 fn len(&self) -> usize;
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum AnnPosRelativeToInsert {
36 Before,
37 After,
38 IncludeInsert,
39}
40
41#[derive(Clone, Debug, Default, PartialEq, Eq)]
42pub struct Span {
43 pub annotations: BTreeSet<Arc<Annotation>>,
44 pub len: usize,
45}
46
47impl Span {
48 pub fn new(len: usize) -> Self {
49 Span {
50 annotations: BTreeSet::new(),
51 len,
52 }
53 }
54}
55
56#[cfg(feature = "test")]
57pub mod dumb_impl;