// Enforce one-level heading descent while allowing returns to outer levels when constructing an outline. use std::cli; pub fn outline_depth_start() -> vector { [0, 0, 0] } pub fn outline_depth_step(snapshot: vector, event: str, value: f64) -> vector { if len(snapshot) != 3 { return []; } let phase = at(snapshot, 0); let saved = at(snapshot, 1); let previous = at(snapshot, 2); if !(phase >= 0.0 && phase < 3.0 && phase % 1.0 == 0.0 && previous >= 0.0 && previous < 4.0 && previous % 1.0 == 0.0 && saved >= 0.0 && saved <= 9007199254740991.0 && saved % 1.0 == 0.0) { return []; } if !(value >= 0.0 && value <= 1000000000.0 && value % 1.0 == 0.0) { return [phase, saved, 0]; } // idle if phase == 0.0 { if event == "heading" && value==1.0 { return [1, value, 1]; // append_heading } } // open if phase == 1.0 { if event == "heading" && value>0.0 && value<=saved+1.0 { return [1, value, 1]; // append_heading } if event == "heading" && value>saved+1.0 { return [1, saved, 2]; // report_skipped_level } if event == "finish" { return [2, saved, 3]; // export_outline } } [phase, saved, 0] } pub fn outline_depth_command(snapshot: vector) -> str { if len(snapshot) != 3 { return "invalid"; } let action = at(snapshot, 2); if action == 0.0 { return "reject"; } if action == 1.0 { return "append_heading"; } if action == 2.0 { return "report_skipped_level"; } if action == 3.0 { return "export_outline"; } "invalid" } // Stop at the first rejected event. No host effects run during replay. pub fn outline_depth_replay(events: arglist, values: vector) -> vector { if args_len(events) != len(values) { return []; } let mut state = outline_depth_start(); let mut i = 0; while i < len(values) { state = outline_depth_step(state, args_at(events, i), at(values, i)); if at(state, 2) == 0.0 { return []; } i += 1; } state }