#json #cow #docs

autosurgeon

A library for working with data in automerge documents

16 releases (7 breaking)

0.8.2 Sep 27, 2023
0.8.0 Jul 21, 2023
0.4.0 Mar 28, 2023
0.2.2 Dec 14, 2022
0.2.0 Nov 30, 2022

#192 in Text processing

Download history 489/week @ 2023-08-14 471/week @ 2023-08-21 269/week @ 2023-08-28 245/week @ 2023-09-04 420/week @ 2023-09-11 453/week @ 2023-09-18 530/week @ 2023-09-25 571/week @ 2023-10-02 530/week @ 2023-10-09 666/week @ 2023-10-16 664/week @ 2023-10-23 579/week @ 2023-10-30 415/week @ 2023-11-06 499/week @ 2023-11-13 312/week @ 2023-11-20 564/week @ 2023-11-27

1,901 downloads per month

MIT license

140KB
3K SLoC

autosurgeon

Build crates docs

Autosurgeon is a Rust library for working with data in automerge documents. See the documentation for a detailed guide.

Quickstart

autosurgeon requires rust 1.65 or newer.

Add autosurgeon to your dependencies with cargo add

cargo add autosurgeon

Then we can define a data model which derives Reconcile and Hydrate and start reading and writing from automerge documents

use autosurgeon::{Reconcile, Hydrate, hydrate, reconcile};

// A simple contact document

#[derive(Debug, Clone, Reconcile, Hydrate, PartialEq)]
struct Contact {
    name: String,
    address: Address,
}

#[derive(Debug, Clone, Reconcile, Hydrate, PartialEq)]
struct Address {
   line_one: String,
   line_two: Option<String>,
   city: String,
   postcode: String,
}

let mut contact = Contact {
     name: "Sherlock Holmes".to_string(),
     address: Address{
         line_one: "221B Baker St".to_string(),
         line_two: None,
         city: "London".to_string(),
         postcode: "NW1 6XE".to_string(),
     },
};

// 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().with_actor(automerge::ActorId::random());
let mut contact2: Contact = hydrate(&doc2).unwrap();
contact2.name = "Dangermouse".to_string();
reconcile(&mut doc2, &contact2).unwrap();

// Concurrently on doc1
contact.address.line_one = "221C Baker St".to_string();
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();
assert_eq!(merged, Contact {
    name: "Dangermouse".to_string(), // This was updated in the first doc
    address: Address {
          line_one: "221C Baker St".to_string(), // This was concurrently updated in doc2
          line_two: None,
          city: "London".to_string(),
          postcode: "NW1 6XE".to_string(),
    }
})

Dependencies

~5.5MB
~120K SLoC