rush_sync_server/commands/lang/
command.rs1use super::LanguageService;
6use crate::commands::command::Command;
7use crate::core::prelude::*;
8use std::future::Future;
9use std::pin::Pin;
10
11#[derive(Debug)]
12pub struct LanguageCommand {
13 service: std::sync::Mutex<LanguageService>,
14}
15
16impl LanguageCommand {
17 pub fn new() -> Self {
18 Self {
19 service: std::sync::Mutex::new(LanguageService::new()),
20 }
21 }
22}
23
24impl Command for LanguageCommand {
25 fn name(&self) -> &'static str {
26 "language"
27 }
28
29 fn description(&self) -> &'static str {
30 "Change application language"
31 }
32
33 fn matches(&self, command: &str) -> bool {
34 command.trim().to_lowercase().starts_with("lang")
35 }
36
37 fn execute_sync(&self, args: &[&str]) -> Result<String> {
38 let service = self.service.lock().unwrap();
39
40 match args.first() {
41 None => Ok(service.show_status()),
42 Some(&lang) => match service.switch_language_only(lang) {
43 Ok(()) => {
44 let msg = crate::i18n::get_command_translation(
45 "system.commands.language.changed",
46 &[&lang.to_uppercase()],
47 );
48 Ok(service.create_save_message(lang, &msg))
49 }
50 Err(e) => Ok(crate::i18n::get_command_translation(
51 "system.commands.language.invalid",
52 &[&e.to_string()],
53 )),
54 },
55 }
56 }
57
58 fn execute_async<'a>(
59 &'a self,
60 args: &'a [&'a str],
61 ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>> {
62 Box::pin(async move {
63 let mut service = {
65 let _service_guard = self.service.lock().unwrap();
66
67 LanguageService::new() };
69
70 match args.first() {
71 None => Ok(service.show_status()),
72 Some(&lang) => service.change_language(lang).await,
73 }
74 })
75 }
76
77 fn supports_async(&self) -> bool {
78 true
79 }
80
81 fn priority(&self) -> u8 {
82 70
83 }
84}
85
86impl Default for LanguageCommand {
87 fn default() -> Self {
88 Self::new()
89 }
90}