canvist_scene 0.2.0

Scene model for the canvist canvas: a node tree of frames, shapes, paths, and text frames with invertible, CRDT-shaped steps
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
//! Scene JSON: a flat list of nodes in paint order.
//!
//! Writing produces the canonical form: every node with its id, type,
//! parent, key, the props that differ from their defaults, and a text
//! frame's document in ProseMirror JSON. Reading also accepts a friendlier
//! form for people and agents: `id` and `key` may be omitted (ids are
//! allocated, keys follow list order), and nodes may nest their children in
//! a `children` array instead of naming a `parent`.

use std::sync::Arc;

use canvist_model::Document;
use rustc_hash::FxHashMap;
use serde_json::Map;
use serde_json::Value as Json;
use serde_json::json;

use crate::FracKey;
use crate::NodeId;
use crate::NodeKind;
use crate::NodeRecord;
use crate::Prop;
use crate::PropKey;
use crate::Props;
use crate::Scene;
use crate::SceneError;
use crate::SceneSchema;
use crate::Shape;
use crate::Step;

/// The `type` of a scene JSON document.
pub const SCENE_TYPE: &str = "canvist-scene";
/// The format version this crate writes.
pub const SCENE_VERSION: u64 = 1;

impl Scene {
	/// Serialize to canonical scene JSON.
	#[must_use]
	pub fn to_json(&self) -> Json {
		let mut defaults = FxHashMap::default();
		let nodes: Vec<Json> = self
			.subtree(NodeId::ROOT)
			.into_iter()
			.filter_map(|id| self.write_node(id, &mut defaults))
			.collect();

		json!({ "type": SCENE_TYPE, "version": SCENE_VERSION, "nodes": nodes })
	}

	/// The JSON of one node, without its children.
	#[must_use]
	pub fn node_json(&self, id: NodeId) -> Option<Json> {
		self.write_node(id, &mut FxHashMap::default())
	}

	/// One node's JSON, reusing each kind's default props across calls.
	fn write_node(&self, id: NodeId, defaults: &mut FxHashMap<NodeKind, Props>) -> Option<Json> {
		let node = self.node(id)?;
		let kind = self.schema().spec(node.kind).kind();
		let defaults = defaults
			.entry(kind)
			.or_insert_with(|| Props::for_kind(kind));
		let mut out = Map::new();
		out.insert("id".into(), json!(id.to_string()));
		out.insert("type".into(), json!(self.schema().spec(node.kind).name()));
		out.insert("parent".into(), json!(node.parent.to_string()));
		out.insert("key".into(), json!(node.key.as_str()));
		out.insert("props".into(), props_json(&node.props, defaults));

		if let Some(doc) = self.text(id) {
			out.insert("text".into(), doc.to_json());
		}

		Some(Json::Object(out))
	}

	/// Parse scene JSON, validating it against `schema`. `client` seeds id
	/// allocation for nodes without ids and for later edits.
	pub fn from_json(
		schema: Arc<SceneSchema>,
		client: u32,
		json: &Json,
	) -> Result<Self, SceneError> {
		if json.get("type").and_then(Json::as_str) != Some(SCENE_TYPE) {
			return Err(SceneError::json(
				"type",
				format!("expected \"{SCENE_TYPE}\""),
			));
		}

		let nodes = json
			.get("nodes")
			.and_then(Json::as_array)
			.ok_or_else(|| SceneError::json("nodes", "expected an array"))?;
		let mut scene = Self::empty(schema, client);
		let records = Reader::new(&mut scene).read(nodes)?;
		scene.apply_steps([Step::InsertNodes { nodes: records }])?;
		Ok(scene)
	}
}

/// Every prop that differs from its default for a node of `kind`, keyed by
/// name. Reading the result with [`Props::for_kind`] as the base restores
/// `props` exactly.
#[must_use]
pub fn props_to_json(props: &Props, kind: NodeKind) -> Json {
	props_json(props, &Props::for_kind(kind))
}

fn props_json(props: &Props, defaults: &Props) -> Json {
	let map = props
		.changed_from(defaults)
		.into_iter()
		.map(|prop| (prop.key().to_string(), prop.to_json()))
		.collect();

	Json::Object(map)
}

/// A flattened node before ids, parents, and keys are resolved.
struct Pending<'json> {
	path: String,
	node: &'json Map<String, Json>,
	id: Option<NodeId>,
	parent: Option<usize>,
}

struct Reader<'scene> {
	scene: &'scene mut Scene,
}

impl<'scene> Reader<'scene> {
	fn new(scene: &'scene mut Scene) -> Self {
		Self { scene }
	}

	/// Flatten nested nodes, reserve explicit ids before allocating the rest,
	/// resolve each parent (nesting or `parent`) and key (given, or after the
	/// previous sibling in list order), then order the records parents first.
	fn read(self, nodes: &[Json]) -> Result<Vec<NodeRecord>, SceneError> {
		let mut pending = Vec::new();
		flatten(nodes, "nodes", None, &mut pending)?;

		for item in &mut pending {
			item.id = optional_id(item.node, &item.path)?;

			if let Some(id) = item.id {
				self.scene.observe_id(id);
			}
		}

		let ids = pending
			.iter()
			.map(|item| item.id.map_or_else(|| self.scene.allocate_id(), Ok))
			.collect::<Result<Vec<_>, _>>()?;
		let mut records = Vec::with_capacity(pending.len());
		let mut last_keys: FxHashMap<NodeId, FracKey> = FxHashMap::default();

		for (item, &id) in pending.iter().zip(&ids) {
			let parent = match item.parent {
				Some(index) => ids[index],
				None => parent_of(item.node, &item.path)?,
			};
			let key = key_of(item.node, &item.path, last_keys.get(&parent))?;
			last_keys.insert(parent, key.clone());
			records.push(self.record(item, id, parent, key)?);
		}

		order_parents_first(records)
	}

	fn record(
		&self,
		item: &Pending<'_>,
		id: NodeId,
		parent: NodeId,
		key: FracKey,
	) -> Result<NodeRecord, SceneError> {
		let schema = self.scene.schema();
		let type_name = item
			.node
			.get("type")
			.and_then(Json::as_str)
			.ok_or_else(|| SceneError::json(format!("{}.type", item.path), "expected a string"))?;
		let kind = schema
			.node_type(type_name)
			.ok_or_else(|| SceneError::UnknownType(type_name.to_owned()))?;
		let behaviour = schema.spec(kind).kind();
		let props = props_from_json(item.node.get("props"), behaviour, &item.path)?;
		let text = match item.node.get("text") {
			Some(text) if behaviour == NodeKind::Text => Some(self.text_blocks(id, text)?),
			Some(_) => return Err(SceneError::NotText(id)),
			None => None,
		};

		Ok(NodeRecord {
			id,
			kind,
			parent,
			key,
			props,
			text,
		})
	}

	fn text_blocks(
		&self,
		id: NodeId,
		text: &Json,
	) -> Result<Vec<canvist_model::BlockTree>, SceneError> {
		let schema = self.scene.schema().text().ok_or(SceneError::NotText(id))?;
		let doc = Document::from_json(schema.clone(), self.scene.client(), text)
			.map_err(|source| SceneError::TextJson { node: id, source })?;

		Ok(doc
			.children(None)
			.iter()
			.filter_map(|&block| doc.tree(block))
			.collect())
	}
}

fn flatten<'json>(
	nodes: &'json [Json],
	path: &str,
	parent: Option<usize>,
	out: &mut Vec<Pending<'json>>,
) -> Result<(), SceneError> {
	for (index, node) in nodes.iter().enumerate() {
		let path = format!("{path}[{index}]");
		let map = node
			.as_object()
			.ok_or_else(|| SceneError::json(&path, "expected an object"))?;
		let me = out.len();
		out.push(Pending {
			path: path.clone(),
			node: map,
			id: None,
			parent,
		});

		if let Some(children) = map.get("children") {
			let children = children
				.as_array()
				.ok_or_else(|| SceneError::json(format!("{path}.children"), "expected an array"))?;
			flatten(children, &format!("{path}.children"), Some(me), out)?;
		}
	}

	Ok(())
}

fn optional_id(node: &Map<String, Json>, path: &str) -> Result<Option<NodeId>, SceneError> {
	node.get("id")
		.map(|id| {
			id.as_str()
				.and_then(|id| id.parse::<NodeId>().ok())
				.filter(|id| !id.is_root())
				.ok_or_else(|| {
					SceneError::json(format!("{path}.id"), "expected a `client.seq` node id")
				})
		})
		.transpose()
}

fn parent_of(node: &Map<String, Json>, path: &str) -> Result<NodeId, SceneError> {
	match node.get("parent") {
		Some(parent) => {
			parent
				.as_str()
				.and_then(|parent| parent.parse().ok())
				.ok_or_else(|| {
					SceneError::json(format!("{path}.parent"), "expected a node id or `root`")
				})
		}
		None if node.get("type").and_then(Json::as_str) == Some("page") => Ok(NodeId::ROOT),
		None => {
			Err(SceneError::json(
				format!("{path}.parent"),
				"missing: nest the node in `children` or name its parent",
			))
		}
	}
}

fn key_of(
	node: &Map<String, Json>,
	path: &str,
	previous: Option<&FracKey>,
) -> Result<FracKey, SceneError> {
	match node.get("key") {
		Some(key) => {
			let text = key
				.as_str()
				.ok_or_else(|| SceneError::json(format!("{path}.key"), "expected a string"))?;
			FracKey::parse(text)
				.map_err(|error| SceneError::json(format!("{path}.key"), error.to_string()))
		}
		None => Ok(FracKey::between(previous, None)?),
	}
}

fn props_from_json(props: Option<&Json>, kind: NodeKind, path: &str) -> Result<Props, SceneError> {
	let mut out = Props::for_kind(kind);
	let Some(props) = props else {
		return Ok(out);
	};
	let map = props
		.as_object()
		.ok_or_else(|| SceneError::json(format!("{path}.props"), "expected an object"))?;

	for (name, value) in map {
		let at = || format!("{path}.props.{name}");
		let key = PropKey::parse(name).ok_or_else(|| SceneError::json(at(), "unknown property"))?;
		let prop = Prop::from_json(&key, value)
			.map_err(|error| SceneError::json(at(), error.to_string()))?;
		out.set(prop);
	}

	if out.shape == Shape::None {
		out.shape = Shape::default_for(kind);
	}

	Ok(out)
}

/// Sort records so each comes after its parent, keeping list order among
/// records at the same depth.
fn order_parents_first(records: Vec<NodeRecord>) -> Result<Vec<NodeRecord>, SceneError> {
	#[derive(Clone, Copy)]
	enum Depth {
		Unvisited,
		Visiting,
		Complete(usize),
	}

	let index: FxHashMap<NodeId, usize> = records
		.iter()
		.enumerate()
		.map(|(at, record)| (record.id, at))
		.collect();
	let mut depths = vec![Depth::Unvisited; records.len()];
	let mut path = Vec::new();

	// Resolve each parent link once, without recursing on attacker-chosen chains.
	for start in 0..records.len() {
		let mut at = start;
		let mut depth = loop {
			match depths[at] {
				Depth::Complete(depth) => break depth + 1,
				Depth::Visiting => return Err(SceneError::Cycle(records[start].id)),
				Depth::Unvisited => {}
			}

			depths[at] = Depth::Visiting;
			path.push(at);
			let Some(&parent) = index.get(&records[at].parent) else {
				break 0;
			};
			at = parent;
		};

		for at in path.drain(..).rev() {
			depths[at] = Depth::Complete(depth);
			depth += 1;
		}
	}

	let mut keyed: Vec<_> = depths
		.into_iter()
		.zip(records)
		.map(|(depth, record)| {
			let Depth::Complete(depth) = depth else {
				unreachable!("all parent depths have been resolved");
			};

			(depth, record)
		})
		.collect();
	keyed.sort_by_key(|(depth, _)| *depth);

	Ok(keyed.into_iter().map(|(_, record)| record).collect())
}

#[cfg(test)]
mod tests {
	use super::*;

	/// Records for testing ordering independently of schema validation.
	fn records(parents: &[(u32, u32)]) -> Vec<NodeRecord> {
		let schema = SceneSchema::builder().build().expect("schema");
		let kind = schema.node_type("page").expect("page type");
		let key = FracKey::between(None, None).expect("key");

		parents
			.iter()
			.map(|&(id, parent)| {
				let parent = match parent {
					0 => NodeId::ROOT,
					_ => NodeId::new(1, parent),
				};

				NodeRecord::new(NodeId::new(1, id), kind, parent, key.clone())
			})
			.collect()
	}

	#[test]
	fn parent_ordering_preserves_input_order_at_each_depth() {
		let input = records(&[(7, 5), (6, 4), (5, 2), (4, 1), (2, 0), (1, 0), (9, 88)]);
		let ordered = order_parents_first(input).expect("acyclic");
		let ids: Vec<_> = ordered.iter().map(|record| record.id.seq()).collect();

		assert_eq!(ids, [2, 1, 9, 5, 4, 7, 6]);
		assert_eq!(ordered[2].parent, NodeId::new(1, 88));
		assert!(order_parents_first(Vec::new()).expect("empty").is_empty());
	}

	#[test]
	fn parent_ordering_reports_cycles_reached_through_a_tail() {
		let input = records(&[(9, 0), (1, 2), (2, 3), (3, 2)]);

		assert_eq!(
			order_parents_first(input),
			Err(SceneError::Cycle(NodeId::new(1, 1)))
		);
	}
}