1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use crate::{lang::Signal, parser::*, session::SessionParserConfig};

pub type HighlightResult = Result<Vec<(String, Style)>, Signal>;
pub trait LocalizedParser: std::marker::Sync {
    fn parse_input_with(&self, input: &str, config: &SessionParserConfig) -> ParseResult;
    fn parse_input(&self, input: &str) -> ParseResult {
        self.parse_input_with(input, &SessionParserConfig::default())
    }
    fn parse_highlight_with(&self, input: &str, config: &SessionParserConfig) -> HighlightResult;
    fn parse_highlight(&self, input: &str) -> HighlightResult {
        self.parse_highlight_with(input, &SessionParserConfig::default())
    }
}

#[cfg(target_family = "wasm")]
use serde::{Deserialize, Serialize};

#[cfg_attr(
    target_family = "wasm",
    wasm_bindgen::prelude::wasm_bindgen,
    derive(Serialize, Deserialize),
    serde(rename_all(serialize = "kebab-case", deserialize = "kebab-case"))
)]
#[derive(Debug, Copy, Clone, Default, PartialEq, clap::ValueEnum, strum::EnumString)]
#[strum(serialize_all = "kebab-case")]
pub enum Localization {
    // ISO-639 codes
    #[default]
    En, // english
    Es, // spanish
    Zh, // chinese
    De, // german
    #[value(skip)]
    Pirate,
    #[value(skip)]
    Emoji,
}

impl LocalizedParser for Localization {
    fn parse_input_with(&self, input: &str, config: &SessionParserConfig) -> ParseResult {
        use Localization::*;
        match self {
            En => LocalizedParser::parse_input_with(&en::Parser, input, config),
            Es => LocalizedParser::parse_input_with(&es::Parser, input, config),
            De => LocalizedParser::parse_input_with(&de::Parser, input, config),
            Zh => LocalizedParser::parse_input_with(&zh::Parser, input, config),
            Pirate => LocalizedParser::parse_input_with(&pirate::Parser, input, config),
            Emoji => LocalizedParser::parse_input_with(&emoji::Parser, input, config),
        }
    }

    fn parse_highlight_with(&self, input: &str, config: &SessionParserConfig) -> HighlightResult {
        use Localization::*;
        match self {
            En => LocalizedParser::parse_highlight_with(&en::Parser, input, config),
            Es => LocalizedParser::parse_highlight_with(&es::Parser, input, config),
            De => LocalizedParser::parse_highlight_with(&de::Parser, input, config),
            Zh => LocalizedParser::parse_highlight_with(&zh::Parser, input, config),
            Pirate => LocalizedParser::parse_highlight_with(&pirate::Parser, input, config),
            Emoji => LocalizedParser::parse_highlight_with(&emoji::Parser, input, config),
        }
    }
}

impl LocalizedParser for SessionParserConfig {
    fn parse_input_with(&self, _input: &str, _config: &SessionParserConfig) -> ParseResult {
        unimplemented!()
    }

    fn parse_input(&self, input: &str) -> ParseResult {
        use Localization::*;
        match self.locale {
            En => LocalizedParser::parse_input_with(&en::Parser, input, self),
            Es => LocalizedParser::parse_input_with(&es::Parser, input, self),
            De => LocalizedParser::parse_input_with(&de::Parser, input, self),
            Zh => LocalizedParser::parse_input_with(&zh::Parser, input, self),
            Pirate => LocalizedParser::parse_input_with(&pirate::Parser, input, self),
            Emoji => LocalizedParser::parse_input_with(&emoji::Parser, input, self),
        }
    }

    fn parse_highlight_with(&self, _input: &str, _config: &SessionParserConfig) -> HighlightResult {
        unimplemented!()
    }

    fn parse_highlight(&self, input: &str) -> HighlightResult {
        use Localization::*;
        match self.locale {
            En => LocalizedParser::parse_highlight_with(&en::Parser, input, self),
            Es => LocalizedParser::parse_highlight_with(&es::Parser, input, self),
            De => LocalizedParser::parse_highlight_with(&de::Parser, input, self),
            Zh => LocalizedParser::parse_highlight_with(&zh::Parser, input, self),
            Pirate => LocalizedParser::parse_highlight_with(&pirate::Parser, input, self),
            Emoji => LocalizedParser::parse_highlight_with(&emoji::Parser, input, self),
        }
    }
}