use offline_protocol_data::{DataDoc, DataValue};
fn text(value: &str) -> DataValue {
DataValue::Text {
value: value.to_string(),
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ada = DataDoc::new();
ada.map_set("meta", "title", text("Coast road"))?;
ada.list_push("packing", text("water"))?;
let shared = ada.commit()?.expect("a change to share");
let mut bo = DataDoc::new();
bo.import(&shared.bytes)?;
println!("both start at: {}", bo.export_json()?);
ada.map_set("meta", "title", text("Coast road, Friday"))?;
ada.list_push("packing", text("map"))?;
ada.text_insert("notes", 0, "leave at six")?;
ada.counter_increment("edits", 1.0)?;
let from_ada = ada.commit()?.expect("Ada's offline work");
bo.map_set("meta", "title", text("Coast road, Saturday"))?;
bo.list_push("packing", text("torch"))?;
bo.text_insert("notes", 0, "bring the tent, ")?;
bo.counter_increment("edits", 1.0)?;
let from_bo = bo.commit()?.expect("Bo's offline work");
println!("apart, Ada has: {}", ada.export_json()?);
println!("apart, Bo has: {}", bo.export_json()?);
ada.import(&from_bo.bytes)?;
bo.import(&from_ada.bytes)?;
bo.import(&from_ada.bytes)?;
let ada_state = ada.export_json()?;
let bo_state = bo.export_json()?;
println!("together, Ada: {ada_state}");
println!("together, Bo: {bo_state}");
println!("identical: {}", ada_state == bo_state);
println!("packing: {} entries", ada.list_len("packing")?);
println!("notes: {:?}", ada.text_value("notes")?);
println!("edits: {}", ada.counter_value("edits")?);
Ok(())
}