| 1 | use onestore::{Store, create_section, create_table_of_contents}; |
| 2 | use std::{env, fs, path::PathBuf}; |
| 3 | |
| 4 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 5 | let mut args = env::args().skip(1); |
| 6 | let destination = PathBuf::from(args.next().ok_or("Provide a new destination directory.")?); |
| 7 | let count: usize = args.next().ok_or("Provide a section count.")?.parse()?; |
| 8 | let length: usize = args |
| 9 | .next() |
| 10 | .ok_or("Provide a text length in Unicode scalars.")? |
| 11 | .parse()?; |
| 12 | if args.next().is_some() || !(1..=1024).contains(&count) || !(1..=1_000_000).contains(&length) { |
| 13 | return Err("Use 1–1024 sections and 1–1000000 Unicode scalars per page.".into()); |
| 14 | } |
| 15 | fs::create_dir(&destination)?; |
| 16 | let notebook = destination.join("notebook"); |
| 17 | fs::create_dir(&notebook)?; |
| 18 | let alphabet: Vec<_> = "Repeated text · café 東京 مرحبا 😀 e\u{301} <>&\" " |
| 19 | .chars() |
| 20 | .collect(); |
| 21 | let mut sections = Vec::new(); |
| 22 | let mut expected = Vec::new(); |
| 23 | for index in 0..count { |
| 24 | let name = format!("Section {index:03}.one"); |
| 25 | let text: String = (0..length) |
| 26 | .map(|offset| { |
| 27 | if offset % 250 == 249 { |
| 28 | '\r' |
| 29 | } else { |
| 30 | alphabet[(offset + index) % alphabet.len()] |
| 31 | } |
| 32 | }) |
| 33 | .collect(); |
| 34 | let bytes = create_section(&name, &text, "Fictitious scale fixture")?; |
| 35 | let identity = Store::parse(&bytes)?.header.file_id; |
| 36 | fs::write(notebook.join(&name), bytes)?; |
| 37 | expected.push(serde_json::json!({"section": name, "text": text})); |
| 38 | sections.push((name, identity)); |
| 39 | } |
| 40 | let entries: Vec<_> = sections |
| 41 | .iter() |
| 42 | .rev() |
| 43 | .map(|(name, id)| (name.as_str(), *id)) |
| 44 | .collect(); |
| 45 | fs::write( |
| 46 | notebook.join("Open Notebook.onetoc2"), |
| 47 | create_table_of_contents("Open Notebook.onetoc2", &entries)?, |
| 48 | )?; |
| 49 | serde_json::to_writer( |
| 50 | fs::File::create(destination.join("expected.json"))?, |
| 51 | &expected, |
| 52 | )?; |
| 53 | Ok(()) |
| 54 | } |