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
use crate::op_set;
use crate::op_set::OpSet;
use crate::types::ObjId;
use crate::{clock::Clock, exid::ExId, Prop};
/// An iterator over the "parents" of an object
///
/// The "parent" of an object in this context is the ([`ExId`], [`Prop`]) pair which specifies the
/// location of this object in the composite object which contains it. Each element in the iterator
/// is a [`Parent`], yielded in reverse order. This means that once the iterator returns `None` you
/// have reached the root of the document.
///
/// This is returned by [`crate::ReadDoc::parents`]
#[derive(Debug)]
pub struct Parents<'a> {
pub(crate) obj: ObjId,
pub(crate) ops: &'a OpSet,
pub(crate) clock: Option<Clock>,
}
impl<'a> Parents<'a> {
/// Return the path this `Parents` represents
///
/// This is _not_ in reverse order.
pub fn path(self) -> Vec<(ExId, Prop)> {
let mut path = self
.map(|Parent { obj, prop, .. }| (obj, prop))
.collect::<Vec<_>>();
path.reverse();
path
}
/// Like `path` but returns `None` if the target is not visible
pub fn visible_path(self) -> Option<Vec<(ExId, Prop)>> {
let mut path = Vec::new();
for Parent { obj, prop, visible } in self {
if !visible {
return None;
}
path.push((obj, prop))
}
path.reverse();
Some(path)
}
}
impl<'a> Iterator for Parents<'a> {
type Item = Parent;
fn next(&mut self) -> Option<Self::Item> {
if self.obj.is_root() {
return None;
}
let op_set::Parent { obj, prop, visible } =
self.ops.parent_object(&self.obj, self.clock.as_ref())?;
self.obj = obj;
let obj = self.ops.id_to_exid(self.obj.0);
Some(Parent { obj, prop, visible })
}
}
/// A component of a path to an object
#[derive(Debug, PartialEq, Eq)]
pub struct Parent {
/// The object ID this component refers to
pub obj: ExId,
/// The property within `obj` this component refers to
pub prop: Prop,
/// Whether this component is "visible"
///
/// An "invisible" component is one where the property is hidden, either because it has been
/// deleted or because there is a conflict on this (object, property) pair and this value does
/// not win the conflict.
pub visible: bool,
}
#[cfg(test)]
mod tests {
use super::Parent;
use crate::{transaction::Transactable, Prop, ReadDoc};
#[test]
fn test_invisible_parents() {
// Create a document with a list of objects, then delete one of the objects, then generate
// a path to the deleted object.
let mut doc = crate::AutoCommit::new();
let list = doc
.put_object(crate::ROOT, "list", crate::ObjType::List)
.unwrap();
let obj1 = doc.insert_object(&list, 0, crate::ObjType::Map).unwrap();
let _obj2 = doc.insert_object(&list, 1, crate::ObjType::Map).unwrap();
doc.put(&obj1, "key", "value").unwrap();
doc.delete(&list, 0).unwrap();
let mut parents = doc.parents(&obj1).unwrap().collect::<Vec<_>>();
parents.reverse();
assert_eq!(
parents,
vec![
Parent {
obj: crate::ROOT,
prop: Prop::Map("list".to_string()),
visible: true,
},
Parent {
obj: list,
prop: Prop::Seq(0),
visible: false,
},
]
);
}
}