rush_sync_server/commands/lang/
command.rs

1use super::LanguageService;
2use crate::commands::command::Command;
3use crate::core::prelude::*;
4
5#[derive(Debug)]
6pub struct LanguageCommand {
7    service: std::sync::Mutex<LanguageService>,
8}
9
10impl LanguageCommand {
11    pub fn new() -> Self {
12        Self {
13            service: std::sync::Mutex::new(LanguageService::new()),
14        }
15    }
16}
17
18impl Command for LanguageCommand {
19    fn name(&self) -> &'static str {
20        "language"
21    }
22
23    fn description(&self) -> &'static str {
24        "Change application language"
25    }
26
27    fn matches(&self, command: &str) -> bool {
28        command.trim().to_lowercase().starts_with("lang")
29    }
30
31    fn execute_sync(&self, args: &[&str]) -> Result<String> {
32        let service = match self.service.lock() {
33            Ok(guard) => guard,
34            Err(poisoned) => {
35                log::error!("Mutex poisoned, recovering...");
36                poisoned.into_inner()
37            }
38        };
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 priority(&self) -> u8 {
59        70
60    }
61}
62
63impl Default for LanguageCommand {
64    fn default() -> Self {
65        Self::new()
66    }
67}