1//! Text typed as ops through a `Section`, for the examples that edit a section file.
2#![allow(dead_code)]
3
4use onestore::{
5 Arena, ExGuid, Section, Transaction,
6 op::{Edit, Op, OpError, PageOp},
7};
8use std::{
9 ops::Range,
10 time::{SystemTime, UNIX_EPOCH},
11};
12
13/// FILETIME now, as an edit's `at`.
14pub fn now() -> u64 {
15 let unix = SystemTime::now()
16 .duration_since(UNIX_EPOCH)
17 .unwrap_or_default();
18 (unix.as_secs() + 11_644_473_600) * 10_000_000 + u64::from(unix.subsec_nanos() / 100)
19}
20
21/// The op replacing `range` of the text object `text` in `space` with `with`.
22pub fn text(space: ExGuid, text: ExGuid, range: Range<u32>, with: &str) -> Edit {
23 let op = PageOp::Text {
24 text,
25 range,
26 with: with.to_owned(),
27 };
28 Edit {
29 at: now(),
30 ops: vec![Op::Page { space, op }],
31 }
32}
33
34/// The transaction `edit` seals into on `image`, as `author` made it; none when it stores
35/// nothing.
36pub fn sealed(image: &[u8], author: &str, edit: &Edit) -> Result<Option<Transaction>, OpError> {
37 let arena = Arena::default();
38 let mut section = Section::open(&arena, image.to_vec()).map_err(OpError::Failed)?;
39 section.apply(author, edit)?;
40 section.seal().map_err(OpError::Failed)
41}