1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::{
    self,
    ops::{
        Deref,
        DerefMut,
    },
};

use freya_native_core::NodeId;
use rustc_hash::{
    FxHashMap,
    FxHashSet,
};
use uuid::Uuid;

#[derive(Default, Clone)]
pub struct ParagraphElements(FxHashMap<Uuid, FxHashSet<NodeId>>);

impl ParagraphElements {
    pub fn insert_paragraph(&mut self, node_id: NodeId, text_id: Uuid) {
        let text_group = self.0.entry(text_id).or_default();

        text_group.insert(node_id);
    }

    pub fn remove_paragraph(&mut self, node_id: NodeId, text_id: &Uuid) {
        let text_group = self.0.get_mut(text_id);

        if let Some(text_group) = text_group {
            text_group.retain(|id| *id != node_id);

            if text_group.is_empty() {
                self.0.remove(text_id);
            }
        }
    }
}

impl Deref for ParagraphElements {
    type Target = FxHashMap<Uuid, FxHashSet<NodeId>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ParagraphElements {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}