subspace_board_command/
composition.rs1use std::process::ExitCode;
2
3use subspace_agent_tools::ToolEnvironment;
4use subspace_product::ProductContext;
5
6use crate::{cli, fail, tree};
7
8pub struct Composition {
13 tree: fn(&ProductContext) -> Result<usage::Spec, String>,
14 completion: fn(&usage::Spec, &[String]) -> Vec<String>,
15 fallback: fn(&ProductContext, &[String]) -> ExitCode,
16 specialized_tools: &'static [&'static str],
17}
18
19impl Composition {
20 pub const fn new(
21 tree: fn(&ProductContext) -> Result<usage::Spec, String>,
22 completion: fn(&usage::Spec, &[String]) -> Vec<String>,
23 fallback: fn(&ProductContext, &[String]) -> ExitCode,
24 ) -> Self {
25 Self {
26 tree,
27 completion,
28 fallback,
29 specialized_tools: &[],
30 }
31 }
32
33 pub fn with_specialized_tools(mut self, names: &'static [&'static str]) -> Self {
36 self.specialized_tools = names;
37 self
38 }
39
40 pub fn run(
45 &self,
46 context: &ProductContext,
47 boards: &[subspace_mcp::Board],
48 environment: &ToolEnvironment,
49 args: &[String],
50 ) -> ExitCode {
51 let (capabilities, notice) = available(cli::capabilities(context, boards, environment));
52 notice.iter().for_each(|notice| eprintln!("{notice}"));
53 self.run_catalog(context, &capabilities, args)
54 }
55
56 pub(crate) fn commands(
57 &self,
58 context: &ProductContext,
59 capabilities: &tree::Capabilities,
60 ) -> Result<tree::Commands, String> {
61 tree::Commands::new(context, capabilities, self.tree, self.specialized_tools)
62 }
63
64 pub(crate) fn run_catalog(
65 &self,
66 context: &ProductContext,
67 capabilities: &tree::Capabilities,
68 args: &[String],
69 ) -> ExitCode {
70 let commands = match self.commands(context, capabilities) {
71 Ok(commands) => commands,
72 Err(error) => return fail(&error, 1),
73 };
74 cli::run_commands(context, &commands, args, self.completion, self.fallback)
75 }
76}
77
78pub(crate) fn available(
81 capabilities: Result<tree::Capabilities, String>,
82) -> (tree::Capabilities, Option<String>) {
83 match capabilities {
84 Ok(capabilities) => (capabilities, None),
85 Err(error) => (
86 tree::Capabilities::default(),
87 Some(format!("Space commands unavailable: {error}")),
88 ),
89 }
90}