M0.6: command registry, guard, theme tokens, app.about and app.quit
Command trait with static spec, enabled check and run; Registry rejects duplicate ids; Engine::execute runs every command inside the guard, which turns panics into EngineError::Internal and keeps the engine usable. Effects (Quit) are applied by the host. Theme tokens for Lutra Dark and Lutra Light render as --lt-* CSS variables and a webview init script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
50bcc95006
commit
b382f9fae7
12 changed files with 758 additions and 1 deletions
202
crates/engine/src/engine.rs
Normal file
202
crates/engine/src/engine.rs
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
//! The engine: registry plus state, and the one entry point that runs
|
||||
//! commands.
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{Command, CommandInfo, EngineError, Outcome, Registry, State, cmd, guard};
|
||||
|
||||
/// The command registry together with the workspace state.
|
||||
///
|
||||
/// Hosts keep one engine behind a lock and call [`Engine::execute`] for every
|
||||
/// action, whatever its origin (menu, shortcut, palette, CLI, control
|
||||
/// channel, MCP).
|
||||
pub struct Engine {
|
||||
/// Registered commands.
|
||||
registry: Registry,
|
||||
/// Workspace state.
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
/// An engine with the built-in commands.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns [`EngineError::Internal`] if two built-in commands share an id.
|
||||
pub fn new() -> Result<Self, EngineError> {
|
||||
let mut engine = Self::empty();
|
||||
cmd::register_builtins(&mut engine.registry)?;
|
||||
Ok(engine)
|
||||
}
|
||||
|
||||
/// An engine without any commands, for tests and embedding.
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
registry: Registry::default(),
|
||||
state: State::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a command.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns [`EngineError::Internal`] if the id is already taken.
|
||||
pub fn register(&mut self, command: Box<dyn Command>) -> Result<(), EngineError> {
|
||||
self.registry.register(command)
|
||||
}
|
||||
|
||||
/// Runs a command inside the guard.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::UnknownCommand`] for an unknown id,
|
||||
/// [`EngineError::Disabled`] if the command is disabled, the command's own
|
||||
/// error, or [`EngineError::Internal`] if it panicked.
|
||||
pub fn execute(&mut self, id: &str, params: Value) -> Result<Outcome, EngineError> {
|
||||
let Self { registry, state } = self;
|
||||
let command = registry
|
||||
.get(id)
|
||||
.ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?;
|
||||
let result = guard::run(|| {
|
||||
if !command.enabled(state) {
|
||||
return Err(EngineError::Disabled(id.to_string()));
|
||||
}
|
||||
command.run(state, params)
|
||||
});
|
||||
state.commands_run = state.commands_run.saturating_add(1);
|
||||
result
|
||||
}
|
||||
|
||||
/// Describes every command with its current enabled state. A command whose
|
||||
/// `enabled` check panics is listed as disabled.
|
||||
pub fn list_commands(&self) -> Vec<CommandInfo> {
|
||||
self.registry
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let enabled = guard::run(|| Ok(c.enabled(&self.state))).unwrap_or(false);
|
||||
CommandInfo::new(c.spec(), enabled)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Read access to the state.
|
||||
pub fn state(&self) -> &State {
|
||||
&self.state
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::{CommandSpec, Effect};
|
||||
|
||||
struct Panicking;
|
||||
|
||||
impl Command for Panicking {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "test.panic",
|
||||
label: "Panic",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "none",
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
||||
state.commands_run += 100;
|
||||
panic!("index out of bounds");
|
||||
}
|
||||
}
|
||||
|
||||
struct Disabled;
|
||||
|
||||
impl Command for Disabled {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "test.disabled",
|
||||
label: "Disabled",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "none",
|
||||
}
|
||||
}
|
||||
|
||||
fn enabled(&self, _state: &State) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn run(&self, _state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
||||
Ok(Outcome::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn about_reports_product_information() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
let out = engine.execute("app.about", Value::Null).unwrap();
|
||||
assert_eq!(out.value["name"], "Lutra");
|
||||
assert_eq!(out.value["slogan"], "An otterly perfect IDE");
|
||||
assert_eq!(out.value["version"], env!("CARGO_PKG_VERSION"));
|
||||
assert!(out.effects.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_asks_the_host_to_quit() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
let out = engine.execute("app.quit", json!({})).unwrap();
|
||||
assert_eq!(out.effects, vec![Effect::Quit]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_and_disabled_commands_fail() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine.register(Box::new(Disabled)).unwrap();
|
||||
assert_eq!(
|
||||
engine.execute("nope", Value::Null).unwrap_err().code(),
|
||||
"unknown_command"
|
||||
);
|
||||
assert_eq!(
|
||||
engine
|
||||
.execute("test.disabled", Value::Null)
|
||||
.unwrap_err()
|
||||
.code(),
|
||||
"disabled"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guard_survives_a_panicking_command() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine.register(Box::new(Panicking)).unwrap();
|
||||
let err = engine.execute("test.panic", Value::Null).unwrap_err();
|
||||
assert_eq!(err, EngineError::Internal("index out of bounds".into()));
|
||||
assert!(engine.execute("app.about", Value::Null).is_ok());
|
||||
assert_eq!(engine.state().commands_run, 102);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_ids_are_rejected() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine.register(Box::new(Disabled)).unwrap();
|
||||
assert_eq!(
|
||||
engine.register(Box::new(Disabled)).unwrap_err().code(),
|
||||
"internal"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_commands_with_metadata() {
|
||||
let mut engine = Engine::new().unwrap();
|
||||
engine.register(Box::new(Disabled)).unwrap();
|
||||
let list = engine.list_commands();
|
||||
let ids: Vec<_> = list.iter().map(|c| c.id.as_str()).collect();
|
||||
assert_eq!(ids, vec!["app.about", "app.quit", "test.disabled"]);
|
||||
let quit = &list[1];
|
||||
assert_eq!(quit.default_shortcut.as_deref(), Some("Mod+Q"));
|
||||
assert!(quit.enabled);
|
||||
assert!(!list[2].enabled);
|
||||
let json = serde_json::to_value(quit).unwrap();
|
||||
assert_eq!(json["menuPath"], "File/Quit");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue