rush_sync_server/commands/lang/
command.rs

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