yrs_tree 0.4.1

A Rust library implementing a CRDT-based tree data structure powered by Yrs
Documentation
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::fmt;
use std::sync::Arc;

use uuid::Uuid;
use yrs::block::Prelim;

use crate::{
    iter::{TraversalOrder, TreeIter},
    Result, Tree, TreeError,
};

/// The ID of a node in a tree. Strings can be made into `NodeId`s using the `into()` method,
/// and `NodeId`s can be converted back into strings using the `to_string()` method.
///
/// Note that `Into<NodeId>` for the string `"<ROOT>"` will return `NodeId::Root`,
/// which cannot be used as a node ID as it is reserved for the actual root node of the tree.
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum NodeId {
    #[default]
    Root,
    Id(String),
}

impl fmt::Display for NodeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            NodeId::Root => write!(f, "<ROOT>"),
            NodeId::Id(id) => write!(f, "{}", id),
        }
    }
}

impl PartialEq<&str> for NodeId {
    fn eq(&self, other: &&str) -> bool {
        match self {
            NodeId::Root => *other == "<ROOT>",
            NodeId::Id(id) => id == other,
        }
    }
}

impl From<&str> for NodeId {
    fn from(id: &str) -> Self {
        match id {
            "<ROOT>" => NodeId::Root,
            _ => NodeId::Id(id.to_string()),
        }
    }
}

impl From<&String> for NodeId {
    fn from(id: &String) -> Self {
        NodeId::from(id.as_str())
    }
}

impl From<String> for NodeId {
    fn from(id: String) -> Self {
        NodeId::from(id.as_str())
    }
}

/// A trait for objects that can behave like a node in a tree;
/// this is implemented for [`Node`] and [`Tree`]. When these methods
/// are used on a [`Tree`], they behave as if they were called on the root node.
pub trait NodeApi {
    /// Returns the ID of the node.
    fn id(self: &Arc<Self>) -> &NodeId;

    /// Creates a new child node with a generated ID.
    fn create_child(self: &Arc<Self>) -> Result<Arc<Node>>;

    /// Creates a new child node with a generated ID at the given index in the parent's children.
    fn create_child_at(self: &Arc<Self>, index: usize) -> Result<Arc<Node>>;

    /// Creates a new child node with the given ID at the end of the parent's children.
    fn create_child_with_id(self: &Arc<Self>, id: impl Into<NodeId>) -> Result<Arc<Node>>;

    /// Creates a new child node with the given ID at the given index in the parent's children.
    fn create_child_with_id_at(
        self: &Arc<Self>,
        id: impl Into<NodeId>,
        index: usize,
    ) -> Result<Arc<Node>>;

    /// Moves the node to the given parent, placing it in that parent's children at the given index.
    ///
    /// Given:
    ///
    /// ```text
    /// <ROOT>
    /// ├──A
    /// │  ├──C
    /// │  ├──D
    /// │  └──E
    /// └──B
    /// ```
    ///
    /// If we call `B.move_to(&A, Some(1))`, we get:
    ///
    /// ```text
    /// <ROOT>
    /// └──A
    ///    ├──C
    ///    ├──B
    ///    ├──D
    ///    └──E
    /// ```
    ///
    /// Passing `None` as the index moves the node to the end of the parent's children.
    fn move_to(self: &Arc<Self>, parent: &Node, index: Option<usize>) -> Result<()>;

    /// Moves the node before the given node.
    ///
    /// Given:
    ///
    /// ```text
    /// <ROOT>
    /// ├──A
    /// │  ├──C
    /// │  ├──D
    /// │  └──E
    /// └──B
    /// ```
    ///
    /// If we call `B.move_before(&E)`, we get:
    ///
    /// ```text
    /// <ROOT>
    /// └──A
    ///    ├──C
    ///    ├──D
    ///    ├──B
    ///    └──E
    /// ```
    fn move_before(self: &Arc<Self>, other: &Arc<Node>) -> Result<()>;

    /// Moves the node after the given node.
    ///
    /// Given:
    ///
    /// ```text
    /// <ROOT>
    /// ├──A
    /// │  ├──C
    /// │  ├──D
    /// │  └──E
    /// └──B
    /// ```
    ///
    /// If we call `B.move_after(&E)`, we get:
    ///
    /// ```text
    /// <ROOT>
    /// └──A
    ///    ├──C
    ///    ├──D
    ///    ├──E
    ///    └──B
    /// ```
    fn move_after(self: &Arc<Self>, other: &Arc<Node>) -> Result<()>;

    /// Returns the parent of the node.
    fn parent(self: &Arc<Self>) -> Option<Arc<Node>>;

    /// Returns the ancestors of the node, starting with the node's parent and ending
    /// at the root node.
    fn ancestors(self: &Arc<Self>) -> Vec<Arc<Node>>;

    /// Returns the children of the node.
    fn children(self: &Arc<Self>) -> Vec<Arc<Node>>;

    /// Returns the descendants of the node. Equivalent to `self.traverse(order).skip(1).collect()`.
    fn descendants(self: &Arc<Self>, order: TraversalOrder) -> Vec<Arc<Node>>;

    /// Returns the siblings of the node.
    fn siblings(self: &Arc<Self>) -> Vec<Arc<Node>>;

    /// Returns an iterator over the node and its descendants in the given order.
    fn traverse(self: &Arc<Self>, order: TraversalOrder) -> TreeIter;

    /// Returns the depth of the node. The root node has a depth of 0; all other
    /// nodes have a depth of 1 plus the depth of their parent.
    fn depth(self: &Arc<Self>) -> usize;

    /// Deletes the node from the tree.
    ///
    /// `strategy` can be one of:
    ///   * [`DeleteStrategy::Promote`] - assign this Node's children
    ///     to its parent, placing them at the end of the vector.
    ///   * [`DeleteStrategy::Cascade`] - deletes this node and all its children,
    ///     in reverse-depth-first order.
    fn delete(self: &Arc<Self>, strategy: DeleteStrategy) -> Result<()>;
}

/// The strategy to use when deleting a node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeleteStrategy {
    /// Promote this node's children to the node's parent.
    Promote,
    /// Cascade the deletion to this node's children.
    Cascade,
}

/// A node in a tree.
///
/// * See [`Tree`] for methods to create and find nodes in the tree.
/// * See [`NodeApi`] for the operations that can be performed on a node.
pub struct Node {
    id: NodeId,
    tree: Arc<Tree>,
}

impl Node {
    pub(crate) fn new(id: NodeId, tree: Arc<Tree>) -> Arc<Self> {
        Arc::new(Self { id, tree })
    }

    fn do_create_child(
        self: &Arc<Self>,
        id: impl Into<NodeId>,
        index: Option<usize>,
    ) -> Result<Arc<Self>> {
        let id = id.into();

        if id == NodeId::Root {
            return Err(
                TreeError::InvalidId("<ROOT> cannot be used as a node ID".to_string()).into(),
            );
        }

        self.tree.update_node(&id, &self.id, index)?;
        Ok(Self::new(id, self.tree.clone()))
    }

    fn move_relative(self: &Arc<Self>, other: &Arc<Node>, offset: usize) -> Result<()> {
        if other.id == self.id {
            return Err(TreeError::Cycle(self.id.clone(), other.id.clone()).into());
        }

        if other.id == NodeId::Root {
            return Err(TreeError::InvalidTarget(NodeId::Root).into());
        }

        let new_parent = other.parent().unwrap();
        let siblings = other.siblings();
        let cur_idx = siblings
            .iter()
            .position(|sibling| sibling.id == other.id)
            .unwrap();

        let new_index = cur_idx + offset;
        self.tree
            .update_node(&self.id, &new_parent.id, Some(new_index))?;

        Ok(())
    }

    /// Sets a value on the node at the given key.
    ///
    /// See the "Implementors" section of the [`yrs::block::Prelim`] trait for more
    /// information on the values that can be stored.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::sync::Arc;
    /// # use yrs_tree::{Node, Tree, NodeApi};
    /// # use yrs::Doc;
    /// #
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let doc = Arc::new(Doc::new());
    /// # let tree = Tree::new(doc, "directory_structure")?;
    /// let node = tree.create_child()?;
    /// node.set("folder", "New Folder")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn set<V: Prelim + Into<yrs::Any>>(&self, key: &str, value: V) -> Result<V::Return> {
        self.tree.set_data(&self.id, key, value)
    }

    /// Gets a previously set value on the node at the given key.
    ///
    /// See [`yrs::Out`] for more information on the types of values that can be returned.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::sync::Arc;
    /// # use yrs_tree::{Node, Tree, NodeApi};
    /// # use yrs::{Doc, Transact};
    /// # use yrs::types::GetString;
    /// #
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let doc = Arc::new(Doc::new());
    /// # let tree = Tree::new(doc.clone(), "directory_structure")?;
    /// let node = tree.create_child()?;
    /// node.set("folder", "New Folder")?;
    /// # let txn = doc.transact();
    /// let Some(yrs::Out::Any(yrs::Any::String(folder))) = node.get("folder")? else {
    ///     panic!("folder is not a string");
    /// };
    /// assert_eq!(*folder, *"New Folder");
    /// # Ok(())
    /// # }
    /// ```
    pub fn get(&self, key: &str) -> Result<Option<yrs::Out>> {
        self.tree.get_data(&self.id, key)
    }

    /// Gets a previously set value on the node at the given key, cast to a specific type.
    /// If no value was found, [`yrs::Any::Null`] will be substituted for the value and
    /// deserialized into the given type instead (e.g. into an `Option`).
    ///
    /// See [`yrs::types::map::Map::get_as`] for more information.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use std::sync::Arc;
    /// # use yrs_tree::{Node, Tree, NodeApi};
    /// # use yrs::Doc;
    /// # use yrs::types::GetString;
    /// #
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let doc = Arc::new(Doc::new());
    /// # let tree = Tree::new(doc, "directory_structure")?;
    /// let node = tree.create_child()?;
    /// node.set("folder", "New Folder")?;
    /// let folder = node.get_as::<String>("folder")?;
    /// assert_eq!(folder.as_str(), "New Folder");
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_as<V: serde::de::DeserializeOwned>(&self, key: &str) -> Result<V> {
        self.tree.get_data_as(&self.id, key)
    }
}

impl NodeApi for Node {
    fn id(self: &Arc<Self>) -> &NodeId {
        &self.id
    }

    fn create_child(self: &Arc<Self>) -> Result<Arc<Self>> {
        let id = Uuid::now_v7().to_string();
        self.create_child_with_id(id)
    }

    fn create_child_at(self: &Arc<Self>, index: usize) -> Result<Arc<Self>> {
        let id = Uuid::now_v7().to_string();
        self.do_create_child(id, Some(index))
    }

    fn create_child_with_id(self: &Arc<Self>, id: impl Into<NodeId>) -> Result<Arc<Self>> {
        self.do_create_child(id, None)
    }

    fn create_child_with_id_at(
        self: &Arc<Self>,
        id: impl Into<NodeId>,
        index: usize,
    ) -> Result<Arc<Self>> {
        self.do_create_child(id, Some(index))
    }

    fn children(self: &Arc<Self>) -> Vec<Arc<Self>> {
        self.tree
            .get_children(&self.id)
            .into_iter()
            .map(|id| Node::new(id.clone(), self.tree.clone()))
            .collect()
    }

    fn descendants(self: &Arc<Self>, order: TraversalOrder) -> Vec<Arc<Self>> {
        // Don't list ourselves as a descendant
        self.traverse(order).skip(1).collect()
    }

    fn parent(self: &Arc<Self>) -> Option<Arc<Self>> {
        self.tree
            .get_parent(&self.id)
            .map(|id| Node::new(id, self.tree.clone()))
    }

    fn ancestors(self: &Arc<Self>) -> Vec<Arc<Self>> {
        let mut ancestors = vec![];
        let mut current = self.parent();

        while let Some(parent) = current {
            ancestors.push(parent.clone());
            current = parent.parent();
        }

        ancestors
    }

    fn siblings(self: &Arc<Self>) -> Vec<Arc<Self>> {
        if let Some(parent) = self.parent() {
            parent.children().clone()
        } else {
            vec![]
        }
    }

    fn traverse(self: &Arc<Self>, order: TraversalOrder) -> TreeIter {
        self.tree.traverse_starting_at(self.id(), order)
    }

    fn depth(self: &Arc<Self>) -> usize {
        if self.id == NodeId::Root {
            return 0;
        }

        let mut depth = 1;
        let mut current = self.tree.get_parent(&self.id);

        while let Some(parent_id) = current {
            if parent_id == NodeId::Root {
                break;
            }
            depth += 1;
            current = self.tree.get_parent(&parent_id);
        }
        depth
    }

    fn move_to(self: &Arc<Self>, parent: &Node, index: Option<usize>) -> Result<()> {
        self.tree.update_node(&self.id, &parent.id, index)
    }

    fn move_before(self: &Arc<Self>, other: &Arc<Node>) -> Result<()> {
        self.move_relative(other, 0)
    }

    fn move_after(self: &Arc<Self>, other: &Arc<Node>) -> Result<()> {
        self.move_relative(other, 1)
    }

    fn delete(self: &Arc<Self>, strategy: DeleteStrategy) -> Result<()> {
        self.tree.delete_node(&self.id, strategy)
    }
}

impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Node({})", self.id)
    }
}