glossa-codegen 0.0.9

Generate const language localisation map code at compile time
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::io::{self, Write};

use glossa_shared::{
  ToCompactString, fmt_compact,
  tap::{Pipe, Tap},
};

use crate::{
  MiniStr,
  generator::{Generator, MapType},
};

impl<'h> Generator<'h> {
  /// Generates individual match functions per locale
  ///   => `const fn map(map_name: &[u8], key: &[u8]) -> &'static str`
  pub fn output_match_fn(&self, non_dsl: MapType) -> io::Result<()> {
    const HEADER: &str = r##"const fn map(map_name: &[u8], key: &[u8]) -> &'static str {
    match (map_name, key) {
    "##;
    let new_header = || self.new_fn_header(HEADER);

    // Process non-DSL maps only (DSL MapType not supported)
    non_dsl
      .get_non_dsl_maps(self)?
      .iter()
      // .filter(|(_, data)| !data.is_empty())
      .map(|(lang, map_entry)| {
        let match_fn_string = map_entry
          .iter()
          .fold(
            new_header(), //
            |mut acc, ((map_name, data_k), data_v)| {
              // Build match arms for each entry
              [
                "(",
                key_as_bytes(map_name).as_str(),
                ", ",
                key_as_bytes(data_k).as_str(),
                r#") => r#####"#,
                "\"", // "
                data_v.as_str(),
                "\"",                            // "
                r###########"#####,"###########, // ###,
                "\n",
              ]
              .iter()
              .for_each(|s| acc.push_str(s));
              acc
            },
          )
          .tap_mut(|buf| buf.push_str("    _ => \"\",\n}}"));
        (lang, match_fn_string)
      })
      .try_for_each(|(lang, s)| {
        // Write generated content to module files
        self
          .create_rs_mod_file(lang)?
          .write_all(s.as_bytes())
      })
  }

  /// Generates a consolidated match function containing all localization
  /// mappings
  ///
  /// Generates:
  ///
  /// ```ignore
  /// const fn map(lang: &[u8], map_name: &[u8], key: &[u8])
  ///   -> &'static str {
  ///   match (lang, map_name, key) {...}
  /// }
  /// ```
  ///
  /// ## Parameter
  ///
  /// - `non_dsl`
  ///   - Specifies the type of mapping to process.
  ///   - Note: Does not support DSL MapType
  ///
  /// ## Example
  ///
  /// ```ignore
  /// use glossa_codegen::{L10nResources, Generator, generator::MapType};
  ///
  /// const L10N_DIR: &str = "../../locales/";
  ///
  /// let data = L10nResources::new(L10N_DIR)
  ///   .with_include_languages(["en-GB", "de", "es", "pt",
  /// "zh-pinyin"]);
  ///
  /// let function_data = Generator::default()
  ///   .with_resources(data)
  ///   .output_match_fn_all_in_one(MapType::Regular)?;
  ///
  /// assert_eq!(function_data.trim(), r#######"
  ///   pub(crate) const fn map(lang: &[u8], map_name: &[u8], key: &[u8]) ->
  ///   &'static str {
  ///     match (lang, map_name, key) {
  ///     (b"de", b"error", b"text-not-found") => r#####"Kein lokalisierter Text
  /// gefunden"#####,
  ///     (b"en-GB", b"error", b"text-not-found") => r#####"No
  /// localised text found"#####,
  ///     (b"es", b"error", b"text-not-found") =>
  /// r#####"No se encontró texto localizado"#####,
  ///     (b"pt", b"error",
  /// b"text-not-found") => r#####"Nenhum texto localizado encontrado"#####,
  ///     (b"zh-Latn-CN", b"error", b"text-not-found") => r#####"MeiYou ZhaoDao
  /// BenDiHua WenBen"#####,
  ///     _ => "",
  /// }}
  ///     "#######.trim());
  /// ```
  pub fn output_match_fn_all_in_one(
    &'h self,
    non_dsl: MapType,
  ) -> io::Result<String> {
    const S_HEADER: &str = r##"const fn map(lang: &[u8], map_name: &[u8], key: &[u8])
    -> &'static str {
    match (lang, map_name, key) {
    "##;

    let new_header = || self.new_fn_header(S_HEADER);

    non_dsl
      .get_non_dsl_maps(self)?
      .iter()
      // .filter(|(_, data)| !data.is_empty())
      .flat_map(|(lang, map_entry)| {
        map_entry
          .iter()
          .map(move |e| (lang, e))
      })
      .fold(
        new_header(), //
        |mut acc, (lang, ((name, key), value))| {
          // Build match arms for each localization entry
          [
            "(b\"",
            lang
              .to_compact_string()
              .as_str(),
            "\", ",
            key_as_bytes(name).as_str(),
            ", ",
            key_as_bytes(key).as_str(),
            ") => r#####",
            "\"",
            value.as_str(),
            "\"",
            "#####,",
            "\n",
          ]
          .iter()
          .for_each(|s| acc.push_str(s));
          acc
        },
      )
      .tap_mut(|buf| buf.push_str("    _ => \"\",\n}}"))
      .pipe(Ok)
  }

  /// Generates a function:
  ///   `const fn map(language: &[u8]) -> &'static str { match language {...} }`
  ///
  /// Note: This function is for performance optimization.
  /// **Only** invoke it to generate a new function when both `map_name` and
  /// `key` are guaranteed to be unique.
  /// Otherwise, use [`Self::output_match_fn_all_in_one`].
  pub fn output_match_fn_all_in_one_by_language(
    &'h self,
    non_dsl: MapType,
  ) -> io::Result<String> {
    const S_HEADER: &str = r##"const fn map(language: &[u8]) -> &'static str {
    match language {
    "##;

    let new_header = || self.new_fn_header(S_HEADER);

    non_dsl
      .get_non_dsl_maps(self)?
      .iter()
      .flat_map(|(lang, map_entry)| {
        map_entry
          .values()
          .map(move |v| (lang, v))
      })
      .fold(
        new_header(), //
        |mut acc, (lang, value)| {
          [
            "b\"",
            lang
              .to_compact_string()
              .as_str(),
            "\" => r#####",
            "\"",
            value.as_str(),
            "\"",
            "#####,",
            "\n",
          ]
          .iter()
          .for_each(|s| acc.push_str(s));
          acc
        },
      )
      .tap_mut(|buf| buf.push_str("    _ => \"\",\n}}"))
      .pipe(Ok)
  }

  /// Generates a function:
  ///   `const fn map(language: &[u8], key: &[u8]) -> &'static str { match
  /// (language, key) {...} }`
  ///
  /// # Note
  ///
  /// You can invoke this function to generate a new function **only** when
  /// `map_name` is unique.
  ///
  /// **Example**:
  ///
  /// - `en/yes-no { yes: "Yes", no: "No"}`
  /// - `de/yes-no { yes: "Ja", no: "Nein" }`
  ///
  /// Here, `map_name` is unique (per language), so it can be omitted:
  ///
  /// ```ignore
  /// match (language, key) {
  ///   (b"en", b"yes") => r#####"Yes"#####,
  ///   (b"en", b"no") => r#####"No"#####,
  ///   (b"de", b"yes") => r#####"Ja"#####,
  ///   (b"de", b"no") => r#####"Nein"#####,
  /// }
  /// ```
  ///
  /// If `map_names` are not unique, use [`Self::output_match_fn_all_in_one`]
  /// instead.
  ///
  /// For example, adding a new map: `en/yes-no2 { yes: "YES", no: "NO"}`
  /// would create conflicting keys ("yes", "no") if `map_name` is omitted.
  pub fn output_match_fn_all_in_one_without_map_name(
    &'h self,
    non_dsl: MapType,
  ) -> io::Result<String> {
    const S_HEADER: &str = r##"const fn map(language: &[u8], key: &[u8])
    -> &'static str {
    match (language, key) {
    "##;

    let new_header = || self.new_fn_header(S_HEADER);

    non_dsl
      .get_non_dsl_maps(self)?
      .iter()
      .flat_map(|(lang, map_entry)| {
        map_entry
          .iter()
          .map(move |((_name, key), value)| (lang, key, value))
      })
      .fold(
        new_header(), //
        |mut acc, (lang, key, value)| {
          [
            "(b\"",
            lang
              .to_compact_string()
              .as_str(),
            "\", ",
            key_as_bytes(key).as_str(),
            ") => r#####",
            "\"",
            value.as_str(),
            "\"",
            "#####,",
            "\n",
          ]
          .iter()
          .for_each(|s| acc.push_str(s));
          acc
        },
      )
      .tap_mut(|buf| buf.push_str("    _ => \"\",\n}}"))
      .pipe(Ok)
  }

  /// Creates header for generated functions
  pub fn new_fn_header(&self, header: &str) -> String {
    let vis_fn = self.get_visibility().as_str();
    String::with_capacity(8192).tap_mut(|buf| {
      [vis_fn, " ", header]
        .iter()
        .for_each(|s| buf.push_str(s));
    })
  }

  /// Generates individual match functions per locale
  /// => `const fn map(key: &[u8]) -> &'static str`
  ///
  /// Compared to `output_match_fn`, omits map_name. If you're unsure which one
  /// to use, then use [output_match_fn()](Self::output_match_fn)
  pub fn output_match_fn_without_map_name(
    &'h self,
    non_dsl: MapType,
  ) -> io::Result<()> {
    const HEADER: &str = r##"const fn map(key: &[u8]) -> &'static str {
    match key {
    "##;
    let new_header = || self.new_fn_header(HEADER);

    // Process non-DSL maps only (DSL MapType not supported)
    non_dsl
      .get_non_dsl_maps(self)?
      .iter()
      // .filter(|(_, data)| !data.is_empty())
      .map(|(lang, map_entry)| {
        let match_fn_string = map_entry
          .iter()
          .fold(
            new_header(), //
            |mut acc, ((_, data_k), data_v)| {
              // Build match arms for each entry
              [
                key_as_bytes(data_k).as_str(),
                r#" => r#####"#,
                "\"", // "
                data_v.as_str(),
                "\"",                            // "
                r###########"#####,"###########, // ###,
                "\n",
              ]
              .iter()
              .for_each(|s| acc.push_str(s));
              acc
            },
          )
          .tap_mut(|buf| buf.push_str("    _ => \"\",\n}}"));
        (lang, match_fn_string)
      })
      .try_for_each(|(lang, s)| {
        // Write generated content to module files
        self
          .create_rs_mod_file(lang)?
          .write_all(s.as_bytes())
      })
  }
}

/// Helper function to format keys as byte string literals
pub(crate) fn key_as_bytes(key: &str) -> MiniStr {
  use fmt_compact as fmt;

  match key.is_ascii() {
    true => fmt!("b{key:?}"),
    _ => fmt!("{:?}", key.as_bytes()),
  }
}

#[cfg(test)]
mod tests {
  use glossa_shared::display::puts;

  use super::*;
  use crate::{
    AnyResult,
    generator::dbg_generator::{en_generator, new_generator},
  };

  #[ignore]
  #[test]
  fn test_output_match_fn() -> AnyResult<()> {
    new_generator().output_match_fn(MapType::Regular)?;
    Ok(())
  }

  #[ignore]
  #[test]
  fn test_output_aio_match_fn() -> AnyResult<()> {
    en_generator()
      .output_match_fn_all_in_one(MapType::Regular)?
      .pipe_ref(puts)
      .pipe(Ok)
  }

  const fn map(language: &[u8], map_name: &[u8], key: &[u8]) -> &'static str {
    match (language, map_name, key) {
      (b"de", b"error", b"text-not-found") => {
        r###"Kein lokalisierter Text gefunden"###
      }
      (b"el", b"error", b"text-not-found") => {
        r###"Δεν βρέθηκε κανένα τοπικό κείμενο"###
      }
      (b"en", b"error", b"text-not-found") => r###"No localized text found"###,
      (b"en", b"test", [240, 159, 145, 139, 240, 159, 140, 144]) => {
        r###"hello world"###
      }
      (b"en", b"test", b"hello") => r###"world"###,
      (b"en-GB", b"error", b"text-not-found") => r###"No localised text found"###,
      (b"zh", b"error", b"text-not-found") => r###"未找到本地化文本"###,
      (b"zh", b"test", b"quote") => r###"""no"''""###,
      (b"zh-Hant", b"error", b"text-not-found") => r###"沒有找到本地化文本"###,
      _ => "",
    }
  }

  #[ignore]
  #[test]
  fn test_get_match_map() {
    const S: &str = map(b"de", b"error", "text-not-found".as_bytes());
    assert!(!S.is_empty());
    println!("{S}");
  }

  #[ignore]
  #[test]
  fn doc_test_all_in_one_match_fn() -> io::Result<()> {
    use crate::L10nResources;
    const L10N_DIR: &str = "../../locales/";

    let data = L10nResources::new(L10N_DIR).with_include_languages([
      "en-GB",
      "de",
      "es",
      "pt",
      "zh-pinyin",
    ]);

    let function_data = Generator::default()
      .with_resources(data)
      .output_match_fn_all_in_one(MapType::Regular)?;

    assert_eq!(function_data, r#######"
    pub(crate) const fn map(lang: &[u8], map_name: &[u8], key: &[u8]) ->
  &'static str {
    match (lang, map_name, key) {
    (b"de", b"error", b"text-not-found") => r#####"Kein lokalisierter Text gefunden"#####,
(b"en-GB", b"error", b"text-not-found") => r#####"No localised text found"#####,
(b"es", b"error", b"text-not-found") => r#####"No se encontró texto localizado"#####,
(b"pt", b"error", b"text-not-found") => r#####"Nenhum texto localizado encontrado"#####,
(b"zh-Latn-CN", b"error", b"text-not-found") => r#####"MeiYou ZhaoDao BenDiHua WenBen"#####,
    _ => "",
}}
    "#######.trim());

    println!("{function_data}");
    Ok(())
  }
}