autosurgeon
Autosurgeon is a Rust library for working with data in automerge documents. See the documentation for a detailed guide.
autosurgeon
requires rust 1.65
or newer.
Add autosurgeon
to your dependencies with cargo add
shell
cargo add autosurgeon
Then we can define a data model which derives Reconcile
and Hydrate
and
start reading and writing from automerge documents
```rust use autosurgeon::{Reconcile, Hydrate, hydrate, reconcile};
// A simple contact document
struct Contact { name: String, address: Address, }
struct Address {
lineone: String,
linetwo: Option
let mut contact = Contact { name: "Sherlock Holmes".tostring(), address: Address{ lineone: "221B Baker St".tostring(), linetwo: None, city: "London".tostring(), postcode: "NW1 6XE".tostring(), }, };
// Put data into a document let mut doc = automerge::AutoCommit::new(); reconcile(&mut doc, &contact).unwrap();
// Get data out of a document let contact2: Contact = hydrate(&doc).unwrap(); assert_eq!(contact, contact2);
// Fork and make changes let mut doc2 = doc.fork().withactor(automerge::ActorId::random()); let mut contact2: Contact = hydrate(&doc2).unwrap(); contact2.name = "Dangermouse".tostring(); reconcile(&mut doc2, &contact2).unwrap();
// Concurrently on doc1 contact.address.lineone = "221C Baker St".tostring(); reconcile(&mut doc, &contact).unwrap();
// Now merge the documents // Reconciled changes will merge in somewhat sensible ways doc.merge(&mut doc2).unwrap();
let merged: Contact = hydrate(&doc).unwrap(); asserteq!(merged, Contact { name: "Dangermouse".tostring(), // This was updated in the first doc address: Address { lineone: "221C Baker St".tostring(), // This was concurrently updated in doc2 linetwo: None, city: "London".tostring(), postcode: "NW1 6XE".to_string(), } }) ```