| 1 | #[path = "support/typing.rs"] |
| 2 | mod typing; |
| 3 | |
| 4 | use onestore::{ |
| 5 | ExGuid, RevisionIndex, Store, |
| 6 | document::{Document, Kind}, |
| 7 | }; |
| 8 | use std::{fs, io, path::PathBuf}; |
| 9 | |
| 10 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 11 | let mut args = std::env::args_os().skip(1); |
| 12 | let output = PathBuf::from( |
| 13 | args.next() |
| 14 | .ok_or(io::Error::from(io::ErrorKind::InvalidInput))?, |
| 15 | ); |
| 16 | let current = args |
| 17 | .next() |
| 18 | .map(|value| value.into_string()) |
| 19 | .transpose() |
| 20 | .map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?; |
| 21 | if args.next().is_some() { |
| 22 | return Err(io::Error::from(io::ErrorKind::InvalidInput).into()); |
| 23 | } |
| 24 | fs::create_dir(&output)?; |
| 25 | let mut bytes = onestore::create_section("synthetic.one", "Maintenance baseline.", "Fixture")?; |
| 26 | for revision in 0..25 { |
| 27 | let store = Store::parse(&bytes)?; |
| 28 | let index = RevisionIndex::parse(&store)?; |
| 29 | let document = Document::parse(&index)?; |
| 30 | let (sid, oid, end) = document |
| 31 | .spaces |
| 32 | .iter() |
| 33 | .find_map(|(sid, space)| { |
| 34 | space.revisions[&space.contexts[&ExGuid::default()]] |
| 35 | .nodes |
| 36 | .iter() |
| 37 | .find_map(|(oid, node)| { |
| 38 | if let Kind::RichText { text, .. } = &node.kind { |
| 39 | return Some((*sid, *oid, text.encode_utf16().count() as u32)); |
| 40 | } |
| 41 | None |
| 42 | }) |
| 43 | }) |
| 44 | .ok_or(io::Error::from(io::ErrorKind::InvalidData))?; |
| 45 | let text = if revision == 24 { |
| 46 | current |
| 47 | .clone() |
| 48 | .unwrap_or_else(|| "Maintenance current.".to_owned()) |
| 49 | } else { |
| 50 | format!("Revision {revision}: {}", "x".repeat(65536)) |
| 51 | }; |
| 52 | let edit = typing::text(sid, oid, 0..end, &text); |
| 53 | if let Some(transaction) = typing::sealed(&bytes, "Fixture", &edit)? { |
| 54 | transaction.apply(&mut bytes)?; |
| 55 | } |
| 56 | } |
| 57 | let store = Store::parse(&bytes)?; |
| 58 | let toc = onestore::create_table_of_contents( |
| 59 | "Open Notebook.onetoc2", |
| 60 | &[("synthetic.one", store.header.file_id)], |
| 61 | )?; |
| 62 | fs::write(output.join("synthetic.one"), &bytes)?; |
| 63 | fs::write(output.join("Open Notebook.onetoc2"), toc)?; |
| 64 | println!( |
| 65 | "{}", |
| 66 | serde_json::json!({"bytes":bytes.len(),"transactions":store.header.transaction_count}) |
| 67 | ); |
| 68 | Ok(()) |
| 69 | } |