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 = self.service.lock().unwrap();
35
36        match args.first() {
37            None => Ok(service.show_status()),
38            Some(&lang) => match service.switch_language_only(lang) {
39                Ok(()) => {
40                    let msg = crate::i18n::get_command_translation(
41                        "system.commands.language.changed",
42                        &[&lang.to_uppercase()],
43                    );
44                    Ok(service.create_save_message(lang, &msg))
45                }
46                Err(e) => Ok(crate::i18n::get_command_translation(
47                    "system.commands.language.invalid",
48                    &[&e.to_string()],
49                )),
50            },
51        }
52    }
53
54    fn execute_async<'a>(
55        &'a self,
56        args: &'a [&'a str],
57    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>> {
58        Box::pin(async move {
59            let mut service = LanguageService::new();
60
61            match args.first() {
62                None => Ok(service.show_status()),
63                Some(&lang) => service.change_language(lang).await,
64            }
65        })
66    }
67
68    fn supports_async(&self) -> bool {
69        true
70    }
71
72    fn priority(&self) -> u8 {
73        70
74    }
75}
76
77impl Default for LanguageCommand {
78    fn default() -> Self {
79        Self::new()
80    }
81}