oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1//! Builds a document in Rust and writes a v1 update to stdout, so the JS
2//! side can verify it reads what Rust writes. This is the direction Task 3
3//! cannot test: a Rust implementation that reads JS updates but writes
4//! something JS cannot read would have passed Task 3 completely.
5use std::io::Write;
6use yrs::types::Attrs;
7use yrs::{Doc, Options, ReadTxn, Text, Transact};
8
9fn main() {
10 // Both values fixed: never drawn from yrs (Global Constraints -- see
11 // core/src/lib.rs and clippy.toml, which bans the alternatives that draw
12 // entropy).
13 let guid: yrs::Uuid = "emit-update-guid".into();
14 let doc = Doc::with_options(Options::with_guid_and_client_id(guid, 7001));
15 {
16 let text = doc.get_or_insert_text("content");
17 let mut txn = doc.transact_mut();
18 text.insert(&mut txn, 0, "from rust 🦀 café");
19 // Format part of the inserted text so the emitted update carries
20 // real formatting bytes, not just plain-text inserts. A plain-text
21 // check alone cannot distinguish a correct implementation from one
22 // that dropped formatting (see fixtures/verify-rust.mjs).
23 let attrs: Attrs = Attrs::from([("bold".into(), true.into())]);
24 text.format(&mut txn, 5, 4, attrs);
25 }
26 let txn = doc.transact();
27 let update = txn.encode_state_as_update_v1(&yrs::StateVector::default());
28 std::io::stdout().write_all(&update).unwrap();
29}
30