Doc, Table, Show start past unreadable settings; the newest author name signs, restarts too (X-02)

This commit is contained in:
ZacharyZhang-NY committed 2026-10-09 11:59:26 -04:00
1 parent 8a915ea354
commit ec87e28a46
11 files changed
+180 -61

No files matched your search

+9 -39
View File
@@ -16,6 +16,7 @@ mod doc;
mod feed;
mod frames;
mod install;
mod load;
pub use install::{install_dir, templates};
use install::worker_bin;
pub use doc::{Pdf, Sheet, Sheets};
@@ -37,6 +38,8 @@ pub struct Worker {
session: u64,
stderr: ely_ipc::Relay,
check: Check,
/// The name new comments sign with, once set.
author: Option<String>,
}
/// The start-up check: to run, passed, or why it failed.
@@ -188,6 +191,7 @@ impl Worker {
session: SESSIONS.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
stderr,
check: Check::Pending(profile_dir.to_path_buf()),
author: None,
};
Ok(worker)
}
@@ -201,49 +205,15 @@ impl Worker {
/// The name new comments and tracked changes carry.
pub fn set_author(&mut self, name: &str) -> io::Result<()> {
log::info!("lok: the author is set");
// Wanted even if this worker fails; a restart sends it.
self.author = Some(name.to_string());
self.request(tag::SET_AUTHOR, &Pack::new().str(name).bytes())?;
Ok(())
}
/// Loads `url`; macros never run (LOK-11).
pub fn load(&mut self, url: &str) -> io::Result<Doc> {
self.unlock(url, "")
}
/// Loads text `url` as a sheet, read as `how` says.
pub fn import(&mut self, url: &str, how: &crate::sheet::csv::Import) -> io::Result<Doc> {
use crate::sheet::csv::{COLUMNS, ENCODINGS};
let at = |found: Option<usize>| found.expect("a listed choice") as u64;
let pack = Pack::new()
.str(url)
.u64(u64::from(how.separator))
.u64(how.quote.map_or(0, u64::from))
.u64(at(ENCODINGS.iter().position(|e| *e == how.encoding)))
.u64(how.columns.len() as u64);
let pack = how.columns.iter().fold(pack, |pack, column| {
pack.u64(at(COLUMNS.iter().position(|c| c == column)))
});
let payload = self.request(tag::IMPORT, &pack.bytes())?;
let mut reply = Unpack::new(&payload);
let (id, kind) = (reply.u64()?, reply.u64()?);
self.feed.opened(id);
Ok(Doc { worker: self.session, id, rev: 0, kind })
}
/// Loads `url` with its password; a locked one is PermissionDenied.
pub fn unlock(&mut self, url: &str, password: &str) -> io::Result<Doc> {
let request = Pack::new().str(url).str(password).bytes();
let payload = self.request(tag::LOAD, &request).map_err(|error| {
if error.to_string() == crate::ipc::LOCKED {
io::Error::new(io::ErrorKind::PermissionDenied, error.to_string())
} else {
error
}
})?;
let mut reply = Unpack::new(&payload);
let (id, kind) = (reply.u64()?, reply.u64()?);
self.feed.opened(id);
Ok(Doc { worker: self.session, id, rev: 0, kind })
/// The name last asked for; a restart signs with it.
pub fn author(&self) -> Option<&str> {
self.author.as_deref()
}
/// Runs a macro by its URL (LOK-11 probe).
+51
View File
@@ -0,0 +1,51 @@
//! Loading documents: by URL, with a password, or as text.
use std::io;
use super::{Doc, Worker};
use crate::ipc::{Pack, Unpack, tag};
impl Worker {
/// Loads `url`; macros never run (LOK-11).
pub fn load(&mut self, url: &str) -> io::Result<Doc> {
self.unlock(url, "")
}
/// Loads text `url` as a sheet, read as `how` says.
pub fn import(&mut self, url: &str, how: &crate::sheet::csv::Import) -> io::Result<Doc> {
use crate::sheet::csv::{COLUMNS, ENCODINGS};
let at = |found: Option<usize>| found.expect("a listed choice") as u64;
let pack = Pack::new()
.str(url)
.u64(u64::from(how.separator))
.u64(how.quote.map_or(0, u64::from))
.u64(at(ENCODINGS.iter().position(|e| *e == how.encoding)))
.u64(how.columns.len() as u64);
let pack = how.columns.iter().fold(pack, |pack, column| {
pack.u64(at(COLUMNS.iter().position(|c| c == column)))
});
let payload = self.request(tag::IMPORT, &pack.bytes())?;
self.opened(&payload)
}
/// Loads `url` with its password; a locked one is PermissionDenied.
pub fn unlock(&mut self, url: &str, password: &str) -> io::Result<Doc> {
let request = Pack::new().str(url).str(password).bytes();
let payload = self.request(tag::LOAD, &request).map_err(|error| {
if error.to_string() == crate::ipc::LOCKED {
io::Error::new(io::ErrorKind::PermissionDenied, error.to_string())
} else {
error
}
})?;
self.opened(&payload)
}
/// A load's reply: the document's number and kind.
fn opened(&mut self, payload: &[u8]) -> io::Result<Doc> {
let mut reply = Unpack::new(payload);
let (id, kind) = (reply.u64()?, reply.u64()?);
self.feed.opened(id);
Ok(Doc { worker: self.session, id, rev: 0, kind })
}
}
+25 -11
View File
@@ -77,9 +77,14 @@ impl Engine for Lok {
}
fn restart(app: AppId, worker: &mut Worker) -> io::Result<()> {
*worker = spawn(app)?;
worker.ready()?;
worker.set_author(&ely_platform::user::name()?)
let author = worker.author().map(str::to_string);
let author = author.ok_or_else(|| io::Error::other("the ended worker had no author"))?;
// The ended worker stays until the new one is ready.
let mut fresh = spawn(app)?;
fresh.ready()?;
fresh.set_author(&author)?;
*worker = fresh;
Ok(())
}
fn settled(view: &LokView) -> bool {
@@ -129,10 +134,13 @@ pub fn start(app: AppId, cx: &mut App) -> io::Result<(Arc<Mutex<Worker>>, PathBu
let worker = Arc::new(Mutex::new(spawn(app)?));
// The window opens now; the kernel finishes starting behind it.
let starting = Arc::clone(&worker);
// Settings as read at launch; defaults if unreadable.
let wanted = Arc::new(Mutex::new(ely_ui::settings::current(cx).author.clone()));
let first = Arc::clone(&wanted);
let started = cx.background_executor().spawn(async move {
let mut worker = starting.lock().expect("the worker lock");
worker.ready()?;
worker.set_author(&ely_platform::user::name()?)
sign(&mut worker, &first)
});
cx.spawn(async move |_| {
if let Err(error) = started.await {
@@ -140,10 +148,16 @@ pub fn start(app: AppId, cx: &mut App) -> io::Result<(Arc<Mutex<Worker>>, PathBu
}
})
.detach();
follow_author(&worker, cx);
follow_author(&worker, wanted, cx);
Ok((worker, work))
}
/// Sends the newest wanted name, read under the worker lock.
fn sign(worker: &mut Worker, wanted: &Mutex<String>) -> io::Result<()> {
let author = wanted.lock().expect("the author lock").clone();
worker.set_author(&ely_platform::user::named(&author)?)
}
/// A worker process on the app's profile, not yet checked.
fn spawn(app: AppId) -> io::Result<Worker> {
let data = ely_platform::data_dir(app).map_err(io::Error::other)?;
@@ -154,19 +168,19 @@ fn spawn(app: AppId) -> io::Result<Worker> {
}
/// A new author name signs from the next comment on.
fn follow_author(worker: &Arc<Mutex<Worker>>, cx: &mut App) {
fn follow_author(worker: &Arc<Mutex<Worker>>, wanted: Arc<Mutex<String>>, cx: &mut App) {
let worker = Arc::clone(worker);
let mut signed = ely_ui::settings::current(cx).author.clone();
cx.observe_global::<ely_ui::settings::Current>(move |cx| {
let author = ely_ui::settings::current(cx).author.clone();
if author == signed {
if *wanted.lock().expect("the author lock") == author {
return;
}
signed = author;
let worker = Arc::clone(&worker);
*wanted.lock().expect("the author lock") = author;
let (worker, wanted) = (Arc::clone(&worker), Arc::clone(&wanted));
cx.background_executor()
.spawn(async move {
let set = ely_platform::user::name().and_then(|name| worker.lock().expect("the worker lock").set_author(&name));
// Any order of these tasks ends on the newest name.
let set = sign(&mut worker.lock().expect("the worker lock"), &wanted);
if let Err(error) = set {
log::error!("lok: the new author name is not set: {error}");
}
+36
View File
@@ -0,0 +1,36 @@
//! A failed engine restart keeps the author for retries.
use ely_office::{Engine as _, Lok};
use ely_platform::AppId;
#[test]
fn a_restart_signs_with_the_newest_name_and_survives_a_failure() {
let Ok(install) = ely_lok::install_dir() else {
eprintln!("SKIP: no LOK build");
return;
};
let home = tempfile::tempdir().expect("a temp dir");
// The one test here: nothing else reads HOME.
unsafe { std::env::set_var("HOME", home.path()) };
let profile = ely_platform::data_dir(AppId::Doc).expect("a data dir").join("lok-profile");
std::fs::create_dir_all(&profile).expect("the profile");
let mut worker = ely_lok::Worker::launch(&install, &profile).expect("the worker");
worker.ready().expect("checks");
worker.set_author("Ada").expect("signs");
// The new worker's check cannot write its sample here.
let blocker = profile.join("lok-verify.fodt");
if blocker.exists() {
std::fs::remove_file(&blocker).expect("the last sample goes");
}
std::fs::create_dir(&blocker).expect("a folder in the way");
assert!(Lok::restart(AppId::Doc, &mut worker).is_err(), "the check fails");
assert_eq!(worker.author(), Some("Ada"), "the author survives a failed restart");
std::fs::remove_dir(&blocker).expect("cleared");
Lok::restart(AppId::Doc, &mut worker).expect("the next try restarts");
assert_eq!(worker.author(), Some("Ada"));
// A name asked while down signs after the restart.
worker.shutdown().expect("stops");
assert!(worker.set_author("Latest").is_err(), "a stopped engine cannot take it");
Lok::restart(AppId::Doc, &mut worker).expect("restarts");
assert_eq!(worker.author(), Some("Latest"), "the newest name signs");
}
+9 -8
View File
@@ -2,11 +2,10 @@
use std::io;
/// The author name from settings, else the account's.
pub fn name() -> io::Result<String> {
let set = crate::settings::Shared::load()?.author;
if !set.is_empty() {
return Ok(set);
/// `author` as the settings give it, else the account's name.
pub fn named(author: &str) -> io::Result<String> {
if !author.is_empty() {
return Ok(author.to_string());
}
account()
}
@@ -61,8 +60,10 @@ fn full_name() -> io::Result<String> {
#[cfg(test)]
mod tests {
#[test]
fn the_account_has_a_name() {
let name = super::name().expect("a name");
assert!(!name.is_empty());
fn a_set_name_signs_else_the_account() {
assert_eq!(super::named("Ada").expect("a name"), "Ada");
let account = super::named("").expect("a name");
assert!(!account.is_empty());
assert_eq!(account, super::account().expect("the account"));
}
}