Skip to main content

cmd

Macro cmd 

Source
macro_rules! cmd {
    ($id:literal, $label:literal, [$($m:literal),* $(,)?], $sc:expr, [$($p:expr),* $(,)?], $en:expr, $run:expr $(,)?) => { ... };
    ($id:literal, $label:literal, [$($m:literal),* $(,)?], $sc:expr, $params:literal, $en:expr, $run:expr $(,)?) => { ... };
}
Expand description

A mutating command: cmd!(id, label, [menu…], shortcut, params, enabled, run).

params is a legacy text doc (r#"{"name":str}"#) or a typed list ([Param::string("name").required()], built in a const so the table can be static). run returns the host’s crate::Host::Error.

use ashlar_command::{CommandError, CommandSpec, Host, Journal, Param, always, cmd};
struct Doc { title: String, journal: Journal }
impl Host for Doc {
    type Error = CommandError;
    fn journal(&mut self) -> &mut Journal { &mut self.journal }
}
let spec: CommandSpec<Doc> = cmd!("doc.rename", "Rename…", ["Edit"], Some("Mod+R"), [Param::string("title").required()], always, |d, p| {
    d.title = p["title"].as_str().unwrap_or_default().to_string();
    Ok(serde_json::Value::Null)
});
assert!(spec.mutates);