19 个版本

0.8.5 2024 年 6 月 27 日
0.8.3 2024 年 1 月 30 日
0.8.2 2023 年 9 月 27 日
0.8.0 2023 年 7 月 21 日
0.2.0 2022 年 11 月 30 日

Rust 模式 中排名 164

Download history 392/week @ 2024-05-03 611/week @ 2024-05-10 480/week @ 2024-05-17 406/week @ 2024-05-24 598/week @ 2024-05-31 417/week @ 2024-06-07 327/week @ 2024-06-14 771/week @ 2024-06-21 397/week @ 2024-06-28 619/week @ 2024-07-05 772/week @ 2024-07-12 660/week @ 2024-07-19 730/week @ 2024-07-26 733/week @ 2024-08-02 512/week @ 2024-08-09 446/week @ 2024-08-16

每月下载量 2,479
用于 automerge_repo

MIT 许可证

145KB
3K SLoC

autosurgeon

Build crates docs

Autosurgeon 是一个 Rust 库,用于处理 automerge 文档中的数据。请参阅文档以获取详细指南。

快速入门

autosurgeon 需要 rust 1.65 或更高版本。

使用 autosurgeon 将其添加到依赖项中

cargo add autosurgeon

然后我们可以定义一个派生自 ReconcileHydrate 的数据模型,并开始从 automerge 文档中读取和写入

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(),
    }
})

依赖项

~8.5MB
~160K SLoC