返回 CodeWhale
config_group.rs
根目录 / crates / tui / src / commands / config_group.rs
1 //! Host registration/action adapter for the portable config group (FEAT-028).
2 //!
3 //! Replaces the group's former concrete-`App` dispatcher. This file is the only
4 //! place that knows the TUI `Command`/`CommandGroup` traits and `AppAction`; the
5 //! movable group (`groups/config/`) imports none of them. It mirrors
6 //! `commands/session_group.rs` and `commands/debug_group.rs`.
7
8 use super::CommandResult;
9 use super::groups::config::portable_handlers;
10 use super::traits::{Command, CommandGroup, ContextualCommand};
11 use crate::tui::app::App;
12 use codewhale_command_contract::handler::CommandHandler;
13 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
14 use codewhale_command_contract::outcome::ConfigCommandResult;
15
16 pub(super) struct ConfigCommands;
17
18 struct Registration<const INDEX: usize>;
19
20 impl<const INDEX: usize> RegisterCommand<CommandResult> for Registration<INDEX> {
21 fn info() -> &'static CommandInfo {
22 portable_handlers()[INDEX].0
23 }
24
25 fn handler() -> CommandHandler<CommandResult> {
26 match portable_handlers()[INDEX].1 {
27 CommandHandler::Contextual { capabilities, .. } => CommandHandler::Contextual {
28 capabilities,
29 handler: |contexts, args| match portable_handlers()[INDEX].1 {
30 CommandHandler::Contextual { handler, .. } => {
31 host_result(handler(contexts, args))
32 }
33 _ => CommandResult::error("config handler shape changed"),
34 },
35 },
36 CommandHandler::Pure(_) => {
37 CommandHandler::Pure(|args| match portable_handlers()[INDEX].1 {
38 CommandHandler::Pure(run) => host_result(run(args)),
39 _ => CommandResult::error("config handler shape changed"),
40 })
41 }
42 }
43 }
44 }
45
46 fn host_result(result: ConfigCommandResult) -> CommandResult {
47 CommandResult {
48 message: result.message,
49 action: result
50 .action
51 .map(super::config_host::config_action_to_app_action),
52 is_error: result.is_error,
53 }
54 }
55
56 impl CommandGroup for ConfigCommands {
57 fn commands(&self) -> &'static [Box<dyn Command>] {
58 static COMMANDS: std::sync::OnceLock<Vec<Box<dyn Command>>> = std::sync::OnceLock::new();
59 COMMANDS
60 .get_or_init(|| {
61 let commands: Vec<Box<dyn Command>> = vec![
62 registration::<0>(), // /config
63 registration::<1>(), // /import-claude
64 registration::<2>(), // /permissions
65 registration::<3>(), // /login
66 registration::<4>(), // /connect
67 registration::<5>(), // /disconnect
68 registration::<6>(), // /auth
69 registration::<7>(), // /workbar (+ /rail, /sidebar)
70 registration::<8>(), // /whale
71 registration::<9>(), // /settings
72 registration::<10>(), // /status
73 registration::<11>(), // /statusline
74 registration::<12>(), // /mode (+ /jihua, /zidong)
75 registration::<13>(), // /theme
76 registration::<14>(), // /verbose
77 registration::<15>(), // /trust (+ /xinren)
78 registration::<16>(), // /logout
79 registration::<17>(),
80 ];
81 assert_eq!(
82 commands
83 .iter()
84 .map(|command| command.info().name)
85 .collect::<Vec<_>>(),
86 portable_handlers()
87 .iter()
88 .map(|(info, _)| info.name)
89 .collect::<Vec<_>>(),
90 "host registry must cover the complete portable config inventory"
91 );
92 commands
93 })
94 .as_slice()
95 }
96 }
97
98 fn registration<const INDEX: usize>() -> Box<dyn Command> {
99 Box::new(
100 ContextualCommand::from_contract::<Registration<INDEX>>().expect("config registration"),
101 )
102 }
103
104 /// Dispatch a `/jihua` (`plan`) or `/zidong` (`yolo`) mode alias through the
105 /// registered `/mode` handler and its declared capability envelope.
106 pub(super) fn mode_alias(app: &mut App, arg: &str) -> CommandResult {
107 match Registration::<12>::handler() {
108 CommandHandler::Contextual {
109 capabilities,
110 handler,
111 } => handler(
112 app.command_contexts_with_config(None)
113 .contexts(capabilities),
114 Some(arg),
115 ),
116 _ => CommandResult::error("mode handler shape changed"),
117 }
118 }
119
119 lines RUST