M1.9a: engine dirs and settings backend

EngineConfig and Engine::with_config; the desktop resolves Tauri's app
config and data dirs. Settings model (appearance.theme, editor.fontSize,
ui.language) with typed validation, persistence in settings.json via
write_atomic (1 MiB cap, unknown keys kept, corrupt file backed up with a
warning notification), settings.list/get/set/reset, settingsChanged and
notification events. Property test over random settings.json content,
control-channel test for persistence and corrupt-file recovery.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 15:23:43 +02:00
parent 851d438e2a
commit f9357ee4b5
17 changed files with 1406 additions and 41 deletions

View file

@ -1,9 +1,24 @@
//! The engine: registry plus state, and the one entry point that runs
//! commands.
use std::path::PathBuf;
use serde_json::Value;
use crate::{Command, CommandInfo, EngineError, Outcome, Registry, State, cmd, guard};
use crate::settings::Settings;
use crate::{
Command, CommandInfo, EngineError, NotificationLevel, Outcome, Registry, State, cmd, guard,
};
/// Where an engine stores its files. The desktop passes Tauri's app config
/// and app data directories; tests pass temp directories.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EngineConfig {
/// User configuration: `settings.json`.
pub config_dir: PathBuf,
/// Application data, such as the crash-recovery journal.
pub data_dir: PathBuf,
}
/// The command registry together with the workspace state.
///
@ -18,7 +33,33 @@ pub struct Engine {
}
impl Engine {
/// An engine with the built-in commands.
/// An engine with the built-in commands that stores its files in
/// `config`'s directories, and loads the settings. A missing settings
/// file gives the defaults. A corrupt or unreadable one never fails
/// construction: the defaults are used and a warning
/// [`crate::Event::Notification`] is queued; hosts take it with
/// [`Engine::take_events`].
///
/// # Errors
/// Returns [`EngineError::Internal`] if two built-in commands share an id.
pub fn with_config(config: EngineConfig) -> Result<Self, EngineError> {
let mut engine = Self::new()?;
let (settings, problem) = Settings::load(&config.config_dir);
engine.state.settings = settings;
engine.state.config = Some(config);
if let Some(problem) = problem {
engine.state.notify(
NotificationLevel::Warning,
Some(problem.message_key()),
Some(problem.message()),
Some(problem.params()),
);
}
Ok(engine)
}
/// An engine with the built-in commands and no persistence: settings
/// live in memory only. For tests and the control-channel test backend.
///
/// # Errors
/// Returns [`EngineError::Internal`] if two built-in commands share an id.
@ -242,6 +283,10 @@ mod tests {
"project.close",
"project.open",
"project.openDialog",
"settings.get",
"settings.list",
"settings.reset",
"settings.set",
"test.disabled",
]
);