From e4349f80731a97bb050c1b989135fc565ede206b Mon Sep 17 00:00:00 2001 From: adz Date: Tue, 10 Dec 2024 01:16:20 +0100 Subject: [PATCH] Hard-coded document schema --- src/document.rs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/document.rs b/src/document.rs index efe76a7..0f3d800 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,8 +1,9 @@ +use std::cell::RefCell; use std::fmt; use anyhow::Result; use automerge::transaction::Transactable; -use automerge::{AutoCommit, AutoSerde, Patch}; +use automerge::{AutoCommit, AutoSerde, ObjId, ObjType, Patch, ReadDoc}; /// Hard-coded automerge document schema in bytes representation for "Aardvark". /// @@ -12,21 +13,28 @@ use automerge::{AutoCommit, AutoSerde, Patch}; /// /// Read more here: /// -const DOCUMENT_SCHEMA: [u8] = [1, 2, 3]; +const DOCUMENT_SCHEMA: [u8; 119] = [ + 133, 111, 74, 131, 14, 200, 8, 95, 0, 109, 1, 16, 163, 64, 79, 49, 42, 30, 77, 109, 146, 45, + 91, 5, 214, 2, 217, 205, 1, 252, 203, 208, 39, 6, 89, 188, 223, 101, 41, 50, 160, 144, 47, 147, + 187, 74, 77, 252, 185, 64, 18, 211, 205, 23, 118, 97, 221, 216, 176, 1, 239, 6, 1, 2, 3, 2, 19, + 2, 35, 2, 64, 2, 86, 2, 7, 21, 5, 33, 2, 35, 2, 52, 1, 66, 2, 86, 2, 128, 1, 2, 127, 0, 127, 1, + 127, 1, 127, 0, 127, 0, 127, 7, 127, 3, 100, 111, 99, 127, 0, 127, 1, 1, 127, 4, 127, 0, 127, + 0, 0, +]; /// Identifier in automerge document path where we store the text. const DOCUMENT_OBJ_ID: &str = "doc"; -#[derive(Debug)] pub struct Document { doc: RefCell, } impl Document { + #[allow(dead_code)] pub fn new() -> Self { - let doc = AutoCommit::new(); + let mut doc = AutoCommit::new(); doc.put_object(automerge::ROOT, DOCUMENT_OBJ_ID, ObjType::Text) - .expect("inserting text object '{DOCUMENT_OBJ_ID}' at root"); + .expect("inserting text object at root"); Self { doc: RefCell::new(doc), } @@ -39,9 +47,19 @@ impl Document { } } + fn text_object(&self) -> ObjId { + let doc = self.doc.borrow(); + let (_value, obj_id) = doc + .get(automerge::ROOT, DOCUMENT_OBJ_ID) + .unwrap_or_default() + .expect("text object at root"); + obj_id + } + pub fn update(&mut self, position: i32, del: i32, text: &str) -> Result<()> { + let text_obj = self.text_object(); let mut doc = self.doc.borrow_mut(); - doc.splice_text(&root, position as usize, del as isize, text)?; + doc.splice_text(&text_obj, position as usize, del as isize, text)?; // Move the diff pointer forward to current position doc.update_diff_cursor(); Ok(()) @@ -59,9 +77,9 @@ impl Document { } pub fn text(&self) -> String { + let text_obj = self.text_object(); let doc = self.doc.borrow(); - let obj = doc.get(automerge::ROOT, DOCUMENT_OBJ_ID); - doc.text(&obj) + doc.text(&text_obj) .expect("text to be given in automerge document") } @@ -84,8 +102,8 @@ impl Default for Document { impl fmt::Debug for Document { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut doc = self.doc.borrow(); - let json = serde_json::to_string_pretty(&AutoSerde::from(doc)) + let doc = self.doc.borrow(); + let json = serde_json::to_string_pretty(&AutoSerde::from(&*doc)) .expect("serialize automerge document to JSON"); write!(f, "{}", json) }