返回 CodeWhale
voice.rs
根目录 / crates / tui / src / commands / groups / core / voice.rs
1 //! Voice input commands — `/voice`, `/voice-send`, `/voice-control`.
2
3 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
4 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
5 use codewhale_command_contract::outcome::{
6 CoreNavigationAction as Action, CoreNavigationCommandResult as CommandResult,
7 };
8
9 pub const VOICE_INFO: CommandInfo = CommandInfo {
10 name: "voice",
11 aliases: &["yuyin", "语音"],
12 usage: "/voice",
13 description_key: "cmd_voice_description",
14 };
15 pub const VOICE_SEND_INFO: CommandInfo = CommandInfo {
16 name: "voicesend",
17 aliases: &["voice-send", "yuyinsend", "语音发送"],
18 usage: "/voicesend",
19 description_key: "cmd_voice_send_description",
20 };
21 pub const VOICE_CONTROL_INFO: CommandInfo = CommandInfo {
22 name: "voicecontrol",
23 aliases: &["voice-control", "yuyincontrol", "语音控制"],
24 usage: "/voicecontrol",
25 description_key: "cmd_voice_control_description",
26 };
27
28 pub struct VoiceCmd;
29 pub struct VoiceSendCmd;
30 pub struct VoiceControlCmd;
31
32 impl RegisterCommand<CommandResult> for VoiceCmd {
33 fn info() -> &'static CommandInfo {
34 &VOICE_INFO
35 }
36 fn handler() -> CommandHandler<CommandResult> {
37 CommandHandler::Contextual {
38 capabilities: CommandCapabilities::VOICE_INPUT.union(CommandCapabilities::PRESENTATION),
39 handler: voice,
40 }
41 }
42 }
43 impl RegisterCommand<CommandResult> for VoiceSendCmd {
44 fn info() -> &'static CommandInfo {
45 &VOICE_SEND_INFO
46 }
47 fn handler() -> CommandHandler<CommandResult> {
48 CommandHandler::Contextual {
49 capabilities: CommandCapabilities::VOICE_INPUT.union(CommandCapabilities::PRESENTATION),
50 handler: voice_send,
51 }
52 }
53 }
54 impl RegisterCommand<CommandResult> for VoiceControlCmd {
55 fn info() -> &'static CommandInfo {
56 &VOICE_CONTROL_INFO
57 }
58 fn handler() -> CommandHandler<CommandResult> {
59 CommandHandler::Contextual {
60 capabilities: CommandCapabilities::VOICE_INPUT.union(CommandCapabilities::PRESENTATION),
61 handler: voice_control,
62 }
63 }
64 }
65
66 fn voice(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
67 let parts = contexts.into_parts();
68 let Some(voice) = parts.voice_input else {
69 return CommandResult::error("Command capability unavailable: voice_input");
70 };
71 let Some(presentation) = parts.presentation else {
72 return CommandResult::error("Command capability unavailable: presentation");
73 };
74 let enabled = !voice.voice_enabled();
75 if enabled && !voice.recorder_available() {
76 let message = presentation
77 .translate("voice_err_no_recorder", &[])
78 .unwrap_or_else(std::convert::identity);
79 return CommandResult::error(message);
80 }
81 let key = if enabled {
82 "voice_enabled"
83 } else {
84 "voice_disabled"
85 };
86 let message = match presentation.translate(key, &[]) {
87 Ok(message) => message,
88 Err(error) => return CommandResult::error(error),
89 };
90 voice.set_voice_enabled(enabled);
91 if enabled {
92 CommandResult::with_message_and_action(message, Action::VoiceCapture)
93 } else {
94 CommandResult::message(message)
95 }
96 }
97
98 fn voice_send(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
99 let parts = contexts.into_parts();
100 let Some(voice) = parts.voice_input else {
101 return CommandResult::error("Command capability unavailable: voice_input");
102 };
103 let Some(presentation) = parts.presentation else {
104 return CommandResult::error("Command capability unavailable: presentation");
105 };
106 let enabled = !voice.send_enabled();
107 let key = if enabled {
108 "voice_send_enabled"
109 } else {
110 "voice_send_disabled"
111 };
112 let message = match presentation.translate(key, &[]) {
113 Ok(message) => message,
114 Err(error) => return CommandResult::error(error),
115 };
116 voice.set_send_enabled(enabled);
117 CommandResult::message(message)
118 }
119
120 fn voice_control(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
121 let parts = contexts.into_parts();
122 let Some(voice) = parts.voice_input else {
123 return CommandResult::error("Command capability unavailable: voice_input");
124 };
125 let Some(presentation) = parts.presentation else {
126 return CommandResult::error("Command capability unavailable: presentation");
127 };
128 let enabled = !voice.control_enabled();
129 let key = if enabled {
130 "voice_control_enabled"
131 } else {
132 "voice_control_disabled"
133 };
134 let message = match presentation.translate(key, &[]) {
135 Ok(message) => message,
136 Err(error) => return CommandResult::error(error),
137 };
138 voice.set_control_enabled(enabled);
139 CommandResult::message(message)
140 }
141
141 lines RUST