automerge/patches/patch.rs
1use crate::{
2 marks::{Mark, MarkSet},
3 text_value::ConcreteTextValue,
4 ObjId, Prop, Value,
5};
6use core::fmt::Debug;
7use std::fmt;
8
9use crate::sequence_tree::SequenceTree;
10
11/// A change to the current state of the document
12///
13/// [`Patch`]es are obtained from a [`PatchLog`](super::PatchLog) which has been passed to any of
14/// the various methods which mutate a document and add incremental changes to the
15/// [`PatchLog`](super::PatchLog)
16#[derive(Debug, Clone, PartialEq)]
17pub struct Patch {
18 /// The object this patch modifies
19 pub obj: ObjId,
20 /// The path to the property in the parent object where this object lives
21 pub path: Vec<(ObjId, Prop)>,
22 /// The change this patch represents
23 pub action: PatchAction,
24}
25
26impl Patch {
27 pub(crate) fn has(&self, obj: &ObjId, recursive: bool) -> bool {
28 &self.obj == obj || (recursive && self.path.iter().any(|(o, _)| o == obj))
29 }
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub enum PatchAction {
34 /// A key was created or updated in a map
35 PutMap {
36 key: String,
37 /// The value that was inserted and the object ID of the object that was inserted. Note
38 /// that the Object ID is only meaningful for `Value::Obj` values
39 value: (Value<'static>, ObjId),
40 /// Whether there is a conflict at this key. If there is a conflict this patch represents
41 /// the "winning" value of the conflict. The conflicting values can be obtained with
42 /// [`crate::ReadDoc::get_all`]
43 conflict: bool,
44 },
45 /// An index in a sequence was updated
46 PutSeq {
47 index: usize,
48 /// The value that was set and the object ID of the object that was set. Note that the
49 /// Object ID is only meaningful for `Value::Obj` values
50 value: (Value<'static>, ObjId),
51 /// Whether there is a conflict at this index. If there is a conflict this patch represents
52 /// the "winning" value of the conflict. The conflicting values can be obtained with
53 /// [`crate::ReadDoc::get_all`]
54 conflict: bool,
55 },
56 /// One or more elements were inserted into a sequence
57 Insert {
58 index: usize,
59 /// The values that were inserted, in order that they appear. As with [`Self::PutMap`] and
60 /// [`Self::PutSeq`] the object ID is only meaningful for `Value::Obj` values
61 values: SequenceTree<(Value<'static>, ObjId, bool)>,
62 },
63 /// Some text was spliced into a text object
64 SpliceText {
65 index: usize,
66 /// The text that was inserted
67 value: ConcreteTextValue,
68 /// All marks currently active for this span of text
69 marks: Option<MarkSet>,
70 },
71 /// A counter was incremented
72 Increment {
73 /// The property of the counter that was incremented within the target object
74 prop: Prop,
75 /// The amount incremented, may be negative
76 value: i64,
77 },
78 /// A new conflict has appeared
79 Conflict {
80 /// The conflicted property
81 prop: Prop,
82 },
83 /// A key was deleted from a map
84 DeleteMap { key: String },
85 /// One or more indices were removed from a sequence
86 DeleteSeq { index: usize, length: usize },
87 /// Some marks within a text object were added or removed
88 Mark { marks: Vec<Mark> },
89}
90
91impl fmt::Display for PatchAction {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 write!(f, "{:?}", self)
94 }
95}