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
82
crates/engine/src/error.rs
Normal file
82
crates/engine/src/error.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
//! The error type every engine entry point returns.
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
/// Why a command could not run or failed.
|
||||
///
|
||||
/// Each variant has a stable [`code`](EngineError::code) that the control
|
||||
/// channel, MCP and the frontend use to tell errors apart.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum EngineError {
|
||||
/// No command is registered under the id.
|
||||
#[error("unknown command: {0}")]
|
||||
UnknownCommand(String),
|
||||
/// The command exists but is disabled in the current state.
|
||||
#[error("command is not available right now: {0}")]
|
||||
Disabled(String),
|
||||
/// The params do not match the command's documented schema.
|
||||
#[error("invalid params for {command}: {message}")]
|
||||
InvalidParams {
|
||||
/// The command id.
|
||||
command: String,
|
||||
/// What is wrong with the params.
|
||||
message: String,
|
||||
},
|
||||
/// The command ran and failed, e.g. a file could not be read.
|
||||
#[error("{0}")]
|
||||
Failed(String),
|
||||
/// A bug: a command panicked or reached a state it should not. The guard
|
||||
/// reports panics with this variant.
|
||||
#[error("internal error: {0}")]
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
impl EngineError {
|
||||
/// Stable machine-readable code of the error.
|
||||
pub fn code(&self) -> &'static str {
|
||||
match self {
|
||||
Self::UnknownCommand(_) => "unknown_command",
|
||||
Self::Disabled(_) => "disabled",
|
||||
Self::InvalidParams { .. } => "invalid_params",
|
||||
Self::Failed(_) => "failed",
|
||||
Self::Internal(_) => "internal",
|
||||
}
|
||||
}
|
||||
|
||||
/// The error as a `{ code, message }` payload for IPC and the control
|
||||
/// channel.
|
||||
pub fn payload(&self) -> ErrorPayload {
|
||||
ErrorPayload {
|
||||
code: self.code().to_string(),
|
||||
message: self.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialisable form of an [`EngineError`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, serde::Deserialize)]
|
||||
pub struct ErrorPayload {
|
||||
/// Stable code, see [`EngineError::code`].
|
||||
pub code: String,
|
||||
/// Message for the user.
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn codes_are_stable() {
|
||||
assert_eq!(
|
||||
EngineError::UnknownCommand("x".into()).code(),
|
||||
"unknown_command"
|
||||
);
|
||||
let e = EngineError::InvalidParams {
|
||||
command: "a.b".into(),
|
||||
message: "missing path".into(),
|
||||
};
|
||||
assert_eq!(e.payload().code, "invalid_params");
|
||||
assert_eq!(e.payload().message, "invalid params for a.b: missing path");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue