-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module holds the automerge `AutoCommit` document now with all the necessary methods around it we need. Additionally this commit prepares the use of a hard-coded byte representation to construct the document. This will allow us to independently create documents across peers as all peers will create the _same_ document schema whenever they do it. More about it here: <https://automerge.org/docs/cookbook/modeling-data/#setting-up-an-initial-document-structure>
- Loading branch information
1 parent
9f78995
commit ac6849e
Showing
2 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::fmt; | ||
|
||
use anyhow::Result; | ||
use automerge::{AutoCommit, AutoSerde, Patch}; | ||
|
||
const DOCUMENT_OBJ_ID: &str = "doc"; | ||
|
||
const DOCUMENT_SCHEMA: [u8] = [1, 2, 3]; | ||
|
||
#[derive(Debug)] | ||
pub struct Document { | ||
doc: RefCell<AutoCommit>, | ||
} | ||
|
||
impl Document { | ||
pub fn new() -> Self { | ||
let doc = AutoCommit::new(); | ||
doc.put_object(automerge::ROOT, DOCUMENT_OBJ_ID, ObjType::Text) | ||
.expect("inserting text object '{DOCUMENT_OBJ_ID}' at root"); | ||
Self { | ||
doc: RefCell::new(doc), | ||
} | ||
} | ||
|
||
pub fn from_bytes(bytes: &[u8]) -> Self { | ||
let doc = AutoCommit::load(bytes).expect("load automerge document from bytes"); | ||
Self { | ||
doc: RefCell::new(doc), | ||
} | ||
} | ||
|
||
pub fn update(&mut self, position: i32, del: i32, text: &str) -> Result<()> { | ||
let mut doc = self.doc.borrow_mut(); | ||
doc.splice_text(&root, position as usize, del as isize, text)?; | ||
// Move the diff pointer forward to current position | ||
doc.update_diff_cursor(); | ||
Ok(()) | ||
} | ||
|
||
pub fn load_incremental(&mut self, bytes: &[u8]) -> Result<()> { | ||
let mut doc = self.doc.borrow_mut(); | ||
doc.load_incremental(&bytes)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn diff_incremental(&mut self) -> Vec<Patch> { | ||
let mut doc = self.doc.borrow_mut(); | ||
doc.diff_incremental() | ||
} | ||
|
||
pub fn text(&self) -> String { | ||
let doc = self.doc.borrow(); | ||
let obj = doc.get(automerge::ROOT, DOCUMENT_OBJ_ID); | ||
doc.text(&obj) | ||
.expect("text to be given in automerge document") | ||
} | ||
|
||
pub fn save(&mut self) -> Vec<u8> { | ||
let mut doc = self.doc.borrow_mut(); | ||
doc.save() | ||
} | ||
|
||
pub fn save_incremental(&mut self) -> Vec<u8> { | ||
let mut doc = self.doc.borrow_mut(); | ||
doc.save_incremental() | ||
} | ||
} | ||
|
||
impl Default for Document { | ||
fn default() -> Self { | ||
Self::from_bytes(&DOCUMENT_SCHEMA) | ||
} | ||
} | ||
|
||
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)) | ||
.expect("serialize automerge document to JSON"); | ||
write!(f, "{}", json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters