Skip to main content

oo_ide/commands/
builder.rs

1use std::borrow::Cow;
2use std::collections::HashMap;
3
4use super::{ArgValue, Command, CommandArg, CommandId, CommandMeta};
5use crate::app_state::AppState;
6use crate::commands::meta;
7use crate::operation::Operation;
8
9pub struct CommandBuilder {
10    id: CommandId,
11    title: Cow<'static, str>,
12    description: Cow<'static, str>,
13    args: Vec<CommandArg>,
14    contexts: Vec<Cow<'static, str>>,
15    visibility: meta::Visibility,
16}
17
18pub fn command(id: impl Into<CommandId>) -> CommandBuilder {
19    CommandBuilder {
20        id: id.into(),
21        title: Cow::Borrowed(""),
22        description: Cow::Borrowed(""),
23        args: vec![],
24        contexts: vec![],
25        visibility: meta::Visibility::UserVisible,
26    }
27}
28
29impl CommandBuilder {
30    pub fn title(mut self, t: impl Into<Cow<'static, str>>) -> Self {
31        self.title = t.into();
32        self
33    }
34    pub fn description(mut self, d: impl Into<Cow<'static, str>>) -> Self {
35        self.description = d.into();
36        self
37    }
38    pub fn context(mut self, c: impl Into<Cow<'static, str>>) -> Self {
39        self.contexts.push(c.into());
40        self
41    }
42    #[allow(dead_code)]
43    pub fn arg(mut self, a: CommandArg) -> Self {
44        self.args.push(a);
45        self
46    }
47    pub fn visibility(mut self, visibility: meta::Visibility) -> Self {
48        self.visibility = visibility;
49        self
50    }
51    pub fn hidden(self) -> Self {
52        self.visibility(meta::Visibility::Hidden)
53    }
54
55    /// Handler receives read-only app state, returns operations.
56    pub fn handler(
57        self,
58        f: impl Fn(&AppState, &HashMap<String, ArgValue>) -> Vec<Operation> + 'static,
59    ) -> Command {
60        Command {
61            meta: CommandMeta {
62                id: self.id,
63                title: self.title,
64                description: self.description,
65                args: self.args,
66                contexts: self.contexts,
67                visibility: self.visibility,
68            },
69            handler: Box::new(f),
70        }
71    }
72}