use crate::events::{EventDecl, Listener};
use crate::host_api::Call;
use crate::mode::{Context, Effect};
use crate::ui::{Panel, Rect};
use serde_json::Value;
pub const API_VERSION: u32 = 1;
pub const API_MIN: u32 = if API_VERSION > 1 { API_VERSION - 1 } else { 1 };
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ApiRange {
pub min: u32,
pub max: u32,
}
impl ApiRange {
pub const CURRENT: ApiRange = ApiRange { min: API_MIN, max: API_VERSION };
}
pub fn negotiate(host: ApiRange, plugin: ApiRange) -> Result<u32, String> {
let lo = host.min.max(plugin.min);
let hi = host.max.min(plugin.max);
if lo <= hi && plugin.min <= plugin.max {
Ok(hi)
} else if plugin.max < host.min {
Err(format!("plugin speaks api {}-{}, Mouser needs {}-{}: update the plugin", plugin.min, plugin.max, host.min, host.max))
} else {
Err(format!("plugin speaks api {}-{}, Mouser speaks {}-{}: update Mouser", plugin.min, plugin.max, host.min, host.max))
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct BindingDecl {
pub id: String,
pub key: String,
pub describe: String,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Provide {
Mode {
hotkey: char,
bindings: Vec<BindingDecl>,
activate_on: Vec<String>,
},
Service,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Hello {
pub api: ApiRange,
pub name: String,
pub version: String,
pub provides: Vec<Provide>,
pub listeners: Vec<Listener>,
pub emits: Vec<EventDecl>,
pub wants_mcp: bool,
pub shm: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct McpGrant {
pub endpoint: String,
pub token: String,
pub client: String,
pub tools: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Accepted {
pub api: u32,
pub session: String,
pub ctx: Context,
pub granted: Vec<String>,
pub denied: Vec<(String, String)>,
pub shm: Option<ShmSpec>,
pub mcp: Option<McpGrant>,
pub restore: Option<Vec<u8>>,
#[serde(default)]
pub features: Vec<String>,
}
impl Accepted {
pub fn has(&self, feature: &str) -> bool {
self.features.iter().any(|f| f == feature)
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Welcome {
Accepted(Box<Accepted>),
Rejected {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ShmSpec {
pub path: String,
pub slots: u32,
pub slot_bytes: u64,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum In {
Event(crate::events::Event),
Response {
id: u64,
result: Result<Value, String>,
},
EffectResult {
ok: bool,
refused: Option<String>,
},
EmitResult(crate::events::EmitResult),
EmitResponses {
id: u64,
results: Vec<(String, Value)>,
timed_out: Vec<String>,
},
Stopping {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ImageRef {
pub key: u64,
pub width: u32,
pub height: u32,
pub src: (u32, u32, u32, u32),
pub rect: Rect,
pub pixels: Pixels,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Pixels {
Cached,
Slot(u32),
Inline {
format: ImageFormat,
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ImageFormat {
Rgba,
Rgb,
Png,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Handled {
Handled,
Pass,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Wants {
pub text: bool,
pub keys: Vec<String>,
pub escape: bool,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Out {
Hello(Hello),
View {
dock: Option<Panel>,
panel: Option<Panel>,
images: Vec<ImageRef>,
fullscreen: bool,
},
Effect(Effect),
Wants(Wants),
Frame {
slot: u32,
key: u64,
width: u32,
height: u32,
pts_us: u64,
},
Emit {
name: String,
payload: Value,
},
EmitRequest {
id: u64,
name: String,
payload: Value,
timeout_ms: u32,
all: bool,
},
Respond {
seq: u64,
value: Value,
},
Request {
id: u64,
call: Call,
},
Reply {
seq: u64,
result: Handled,
},
StateSave(#[serde(with = "serde_bytes")] Vec<u8>),
Subscribe(Listener),
Unsubscribe(String),
Log {
level: LogLevel,
text: String,
},
Wake,
Menu {
x: i32,
y: i32,
entries: Vec<crate::ui::MenuEntry>,
},
Hosted(Vec<crate::hosted::HostedView>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
}