返回 CodeWhale
translate.rs
根目录 / crates / tui / src / commands / groups / core / translate.rs
1 //! `/translate` command.
2
3 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
4 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
5 use codewhale_command_contract::outcome::CoreNavigationCommandResult as CommandResult;
6
7 pub const COMMAND_INFO: CommandInfo = CommandInfo {
8 name: "translate",
9 aliases: &["translation", "transale"],
10 usage: "/translate",
11 description_key: "cmd_translate_description",
12 };
13
14 pub struct TranslateCmd;
15
16 impl RegisterCommand<CommandResult> for TranslateCmd {
17 fn info() -> &'static CommandInfo {
18 &COMMAND_INFO
19 }
20 fn handler() -> CommandHandler<CommandResult> {
21 CommandHandler::Contextual {
22 capabilities: CommandCapabilities::CONFIG.union(CommandCapabilities::PRESENTATION),
23 handler: translate,
24 }
25 }
26 }
27
28 fn translate(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
29 let parts = contexts.into_parts();
30 let Some(config) = parts.config else {
31 return CommandResult::error("Command capability unavailable: config");
32 };
33 let Some(presentation) = parts.presentation else {
34 return CommandResult::error("Command capability unavailable: presentation");
35 };
36 let enabled = !config.translation_enabled();
37 let key = if enabled {
38 "cmd_translate_on"
39 } else {
40 "cmd_translate_off"
41 };
42 let message = match presentation.translate(key, &[]) {
43 Ok(message) => message,
44 Err(error) => return CommandResult::error(error),
45 };
46 config.set_translation_enabled(enabled);
47 CommandResult::message(message)
48 }
49
49 lines RUST