Skip to content

Commit

Permalink
Hard-coded document schema
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Dec 10, 2024
1 parent 760e5fe commit e4349f8
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -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".
///
Expand All @@ -12,21 +13,28 @@ use automerge::{AutoCommit, AutoSerde, Patch};
///
/// Read more here:
/// <https://automerge.org/docs/cookbook/modeling-data/#setting-up-an-initial-document-structure>
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<AutoCommit>,
}

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),
}
Expand All @@ -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(())
Expand All @@ -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")
}

Expand All @@ -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)
}
Expand Down

0 comments on commit e4349f8

Please sign in to comment.