crdt_richtext/legacy/
range_map.rs

1use 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    /// f is used to position the annotations when they ends in the insert range
9    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    /// should keep the shrink annotations around even if they are deleted completely
18    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    /// TODO: need to clarify the rules when encounter an empty span on the edges
28    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/// the position of annotation relative to a new insert
34#[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;