Skip to main content

devpage_design/wire/
patch.rs

1//! Edits on the wire: [`Edits`] to the `proto::Patch` the watch stream sends
2//! and the store logs, and back — each image and font file in it once.
3
4use super::{decode, encode, encode_own, file, files, font_file, font_files, node_id, unnode_id};
5use crate::proto::{self, op::Op as Kind};
6use crate::scene::{self, Applied, Edit, Edits, Scene, Uses};
7
8/// `edits` as a patch.
9pub fn patch(edits: &Edits) -> proto::Patch {
10    proto::Patch {
11        ops: edits.list.iter().map(op).collect(),
12        images: edits.images.values().map(|f| file(f)).collect(),
13        fonts: edits.fonts.values().map(|f| font_file(f)).collect(),
14        themed: edits.themed,
15    }
16}
17
18/// `edit` for the wire: a change carries the node's own attributes alone.
19fn op(edit: &Edit) -> proto::Op {
20    let op = match edit {
21        Edit::Change(n) => Kind::Change(encode_own(n)),
22        Edit::Insert {
23            parent,
24            after,
25            node,
26        } => Kind::Insert(proto::Insert {
27            parent: parent.as_ref().map(node_id),
28            after: after.as_ref().map(node_id),
29            node: Some(encode(node)),
30        }),
31        Edit::Remove(id) => Kind::Remove(node_id(id)),
32        Edit::Move { id, parent, after } => Kind::Move(proto::Move {
33            id: Some(node_id(id)),
34            parent: Some(node_id(parent)),
35            after: after.as_ref().map(node_id),
36        }),
37    };
38    proto::Op { op: Some(op) }
39}
40
41/// The edits a patch to `scene` carries, or why it doesn't: each image and
42/// font file the scene holds already is taken from it, not decoded or
43/// checked again.
44pub(crate) fn edits(patch: &proto::Patch, scene: &Scene) -> Result<Edits, String> {
45    let list = patch
46        .ops
47        .iter()
48        .map(|o| {
49            Ok(match o.op.as_ref().ok_or("an empty operation")? {
50                Kind::Change(n) => Edit::Change(decode(n)?),
51                Kind::Insert(i) => Edit::Insert {
52                    parent: i.parent.as_ref().map(|p| unnode_id(Some(p))).transpose()?,
53                    after: i.after.as_ref().map(|a| unnode_id(Some(a))).transpose()?,
54                    node: decode(i.node.as_ref().ok_or("an insert with no node")?)?,
55                },
56                Kind::Remove(id) => Edit::Remove(unnode_id(Some(id))?),
57                Kind::Move(m) => Edit::Move {
58                    id: unnode_id(m.id.as_ref())?,
59                    parent: unnode_id(m.parent.as_ref())?,
60                    after: m.after.as_ref().map(|a| unnode_id(Some(a))).transpose()?,
61                },
62            })
63        })
64        .collect::<Result<_, String>>()?;
65    Ok(Edits {
66        list,
67        images: files(&patch.images, &scene.images)?,
68        fonts: font_files(&patch.fonts, &scene.fonts)?,
69        themed: patch.themed,
70    })
71}
72
73/// Applies a patch to `scene`, whose files `uses` counts, as a browser
74/// holding the scene does — or says why it can't, leaving both as they
75/// were.
76pub fn apply(scene: &mut Scene, uses: &mut Uses, patch: &proto::Patch) -> Result<Applied, String> {
77    let edits = edits(patch, scene)?;
78    scene::apply(scene, uses, &edits)
79}
80
81#[cfg(test)]
82mod tests;