AutoSurgeon
autosurgeon is a library for interaction with [automerge] documents in Rust with an API
inspired by serde. The core of the library are two traits: [Reconcile], which describes how
to take a rust value and update an automerge document to match the value; and [Hydrate],
which describes how to create a rust value given an automerge document.
Whilst you can implement [Reconcile] and [Hydrate] manually, autosurgeon provides derive
macros to do this work mechanically.
Additionally autosurgeon provides the [Counter] and [Text] data types which implement
[Reconcile] and [Hydrate] for counters and text respectively.
Currently this library does not handle incremental updates, that means that every time you
receive concurrent changes from other documents you will need to re-hydrate your data
structures from your document. This will be addressed in future versions.
Feature Flags
uuid- Includes implementations ofReconcileandHydratefor theUuidcrate which will reconcile to a [automerge::ScalarValue::Bytes]
Example
Imagine we are writing a program to interact with a document containing some contact details.
We start by writing some data types to represent the contact and deriving the [Reconcile] and
[Hydrate] traits.
# use ;
First we create a contact and put it into a document
# use ;
#
#
#
#
let contact = Contact ;
let mut doc = new;
reconcile.unwrap;
Now we can reconstruct the contact from the document
# use ;
#
#
#
#
#
# let mut contact = Contact ;
#
# let mut doc = new;
# reconcile.unwrap;
let contact2: Contact = hydrate.unwrap;
assert_eq!;
reconcile is smart though, it doesn't just update everything in the document, it figures out
what's changed, which means merging modified documents works as you would imagine. Let's fork
our document and make concurrent changes to it, then merge it and see how it looks.
# use ;
#
#
#
#
#
# let mut contact = Contact ;
#
# let mut doc = new;
# reconcile.unwrap;
// Fork and make changes
let mut doc2 = doc.fork.with_actor;
let mut contact2: Contact = hydrate.unwrap;
contact2.name = "Dangermouse".to_string;
reconcile.unwrap;
// Concurrently on doc1
contact.address.line_one = "221C Baker St".to_string;
reconcile.unwrap;
// Now merge the documents
doc.merge.unwrap;
let merged: Contact = hydrate.unwrap;
assert_eq!
Derive Macro
Automerge Representation
The derive macros map rust structs to the automerge structures in a similar manner to serde
let w = W ; // Represented as `{"a":0,"b":0}`
;
let x = X; // Represented as `[0,0]`
;
let y = Y; // Represented as just the inner value `0`
let w = W ; // Represented as `{"W":{"a":0,"b":0}}`
let x = X; // Represented as `{"X":[0,0]}`
let y = Y; // Represented as `{"Y":0}`
let z = Z; // Represented as `"Z"`
The key attribute
autosurgeon will generally do its best to generate smart diffs. But sometimes you know
additional information about your data which can make merges smarter. Consider the following
scenario where we create a product catalog and then make concurrent changes to it.
# use ;
# use ;
let mut catalog = Catalog ;
// Put the catalog into the document
let mut doc = new;
reconcile.unwrap;
// Fork the document and insert a new product at the start of the catalog
let mut doc2 = doc.fork.with_actor;
let mut catalog2 = catalog.clone;
catalog2.products.insert;
reconcile.unwrap;
// Concurrenctly remove a product from the catalog in the original doc
catalog.products.remove;
reconcile.unwrap;
// Merge the two changes
doc.merge.unwrap;
assert_doc!;
This is surprising, we have a bunch of merge conflicts on the fields of the first product in
the list (as signified by the multiple values in the {..} on the inner values of the map!)
and the second product in the list is also a strimmer. This is because autosurgeon has no way
of knowing the difference between "I inserted an item at the front of the products list" and "I
updated the first item in the products list".
But we have an id field on the product, we can make autosurgeon aware of this.
# use ;
# use ;
And with this our concurrent changes look like the following:
# use ;
# use ;
#
#
#
#
# let mut catalog = Catalog ;
# let mut doc = new;
# reconcile.unwrap;
# let mut doc2 = doc.fork.with_actor;
# let mut catalog2 = catalog.clone;
# catalog2.products.insert;
# reconcile.unwrap;
# catalog.products.remove;
# reconcile.unwrap;
doc.merge.unwrap;
assert_doc!;
Providing Implementations for foreign types
Deriving Hydrate and Reconcile is fine for your own types, but sometimes you are using a
type which you did not write. For these situations there are a few attributes you can use.
reconcile=
The value of this attribute must be the name of a function with the same signature as
[Reconcile::reconcile]
# use ;
hydrate=
The value of this attribute must be the name of a function with the same signature as
[Hydrate::hydrate]
# use ;
with=
The value of this attribute must be the name of a module wich has both a reconcile function
and a hydrate function, with the same signatures as [Reconcile::reconcile] and
[Hydrate::hydrate] respectively.
# use ;