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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::{
  ffi::OsStr,
  fs,
  path::{Path, PathBuf},
  sync::OnceLock,
};

pub type L10nResMap = HashMap<KString, Vec<L10nMapEntry>>;

use anyhow::bail;
use dashmap::DashSet;
use getset::{Getters, WithSetters};
use glossa_dsl::Resolver;
use glossa_shared::{
  ToCompactString,
  small_list::SmallList,
  tap::{Pipe, TapFallible, TryConv},
  type_aliases::ahash::HashMap,
};
use kstring::KString;
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use walkdir::{DirEntry, WalkDir};

use crate::{AnyResult, MiniStr, to_kstr};

#[derive(Getters, WithSetters, Debug, Clone)]
#[getset(get = "pub with_prefix", set_with = "pub")]
pub struct L10nResources {
  dir: PathBuf,
  dsl_suffix: MiniStr,

  #[getset(skip)]
  #[getset(get = "pub")]
  include_languages: SmallList<3>,

  #[getset(skip)]
  #[getset(get = "pub")]
  include_map_names: SmallList<2>,

  #[getset(skip)]
  #[getset(get = "pub")]
  exclude_languages: SmallList<1>,

  #[getset(skip)]
  #[getset(get = "pub")]
  exclude_map_names: SmallList<1>,

  #[getset(get)]
  /// get data: [Self::get_or_init_data]
  lazy_data: OnceLock<L10nResMap>,
}

impl Default for L10nResources {
  /// Default:
  ///
  /// ```ignore
  /// {
  ///   dsl_suffix: ".dsl"
  ///   ..Default::default()
  /// }
  /// ```
  fn default() -> Self {
    Self {
      dsl_suffix: ".dsl".into(),
      dir: Default::default(),
      include_languages: Default::default(),
      include_map_names: Default::default(),
      exclude_languages: Default::default(),
      exclude_map_names: Default::default(),
      lazy_data: Default::default(),
    }
  }
}

fn walk_file<P: AsRef<Path>>(dir: P) -> Option<impl Iterator<Item = DirEntry>> {
  dir
    .pipe(WalkDir::new)
    .follow_links(true)
    .into_iter()
    .filter_map(Result::ok)
    .filter(is_supported_config_file)
    .pipe(Some)
}

fn is_supported_config_file(e: &DirEntry) -> bool {
  let f = e.path();
  f.is_file()
    && f
      .extension()
      .is_some_and(is_supported_cfg_format)
}

fn is_supported_cfg_format<S: AsRef<OsStr>>(o: S) -> bool {
  ["toml", "ron", "json", "json5", "yml", "yaml"]
    .iter()
    .map(OsStr::new)
    .any(|a| o.as_ref() == a)
}

fn dir_name_to_opt_lang(dir: &Path) -> Option<KString> {
  dir
    .file_name()?
    .to_str()?
    .pipe(KString::from_ref)
    .pipe(Some)
}

impl L10nResources {
  /// Constructs a new `L10nResources` instance with localization directory.
  ///
  /// The provided path should point to a directory containing
  /// localization files.
  ///
  /// ## Example
  ///
  /// ```
  /// use glossa_codegen::L10nResources;
  ///
  /// let _res = L10nResources::new("../../locales/");
  /// ```
  pub fn new<P: Into<PathBuf>>(dir: P) -> Self {
    Self {
      dir: dir.into(),
      ..Default::default()
    }
  }

  pub fn get_or_init_data(&self) -> &L10nResMap {
    self
      .get_lazy_data()
      .get_or_init(|| {
        self
          .collect_localized_files()
          .expect("Failed to init L10nResources Data")
      })
  }

  fn walk_dir(&self) -> Option<impl ParallelIterator<Item = PathBuf>> {
    self
      .get_dir()
      .pipe(fs::read_dir)
      .ok()?
      .par_bridge()
      .filter_map(Result::ok)
      .map(|e| e.path())
      .filter(|d| d.is_dir())
      .into()
  }

  /// Processes localization file and converts to L10nMapEntry
  ///
  /// ## Implementation Notes
  ///
  /// 1. Deserialization Precedence: File contents must be deserialized and
  ///    validated BEFORE registering the file stem in the tracking set. This
  ///    ensures only files with valid data claim priority.
  /// 2. Empty Data Handling: HashMap data with empty content after
  ///    deserialization are intentionally excluded to prevent empty entries
  ///    from shadowing valid data files.
  /// 3. File Stem Collisions: The first valid file for each stem establishes
  ///    priority. Subsequent files with the same stem (different extensions)
  ///    will be ignored, even if they contain data.
  ///
  /// ## Edge Case Example
  ///
  /// When both `test.json` (empty) and `test.ron` (valid data) exist:
  /// - Bad order: Processing `test.json` first would permanently block
  ///   `test.ron`
  /// - Correct order: Deserialize first, then `test.json` gets filtered out by
  ///   empty check, allowing `test.ron` to claim the stem when processed.
  fn process_file<P: AsRef<Path> + core::fmt::Debug>(
    &self,
    file: P,
    file_stem: &MiniStr,
    stem_set: &DashSet<MiniStr>,
  ) -> Option<L10nMapEntry> {
    let data = deser_config_file(&file)
      .tap_err(|e| eprintln!("[WARN] Deserialization error for {file:?}: {e}"))
      .ok()?;

    // Reject empty datasets to prevent empty entries from reserving file stems
    (!data.is_empty()).then_some(())?;

    // let file_stem = get_file_stem(file)?;

    stem_set
      .insert(file_stem.clone())
      .then_some(())?;

    let suffix = self.get_dsl_suffix().as_str();

    let (tmpl_data, data) = match file_stem.ends_with(suffix) && !suffix.is_empty() {
      true => (data.try_conv::<Resolver>().ok(), None),
      _ => (None, Some(data)),
    };

    let map_name = file_stem
      .trim_end_matches(suffix)
      .into();

    L10nMapEntry {
      map_name,
      data,
      tmpl_data,
    }
    .pipe(Some)
  }

  fn collect_localized_files(&self) -> Option<L10nResMap> {
    self
      .walk_dir()?
      .filter(|dir| self.filter_include_languages(dir))
      .filter(|dir| self.filter_exclude_languages(dir))
      .filter_map(|ref dir| {
        let entries = self.parallel_collect_l10n_entries(dir)?;
        let lang = dir_name_to_opt_lang(dir)?;
        (!entries.is_empty()).then_some((lang, entries))
      })
      .collect::<HashMap<_, _>>()
      .into()
  }

  fn parallel_collect_l10n_entries(&self, dir: &Path) -> Option<Vec<L10nMapEntry>> {
    let stem_set = DashSet::with_capacity(64);

    dir
      .pipe(walk_file)?
      .par_bridge()
      .filter_map(annotate_entry_with_stem)
      .filter(|(_, map_name)| self.filter_include_map_names(map_name))
      .filter(|(_, map_name)| self.filter_exclude_map_names(map_name))
      .filter_map(|(file, file_stem)| {
        self.process_file(file.path(), &file_stem, &stem_set)
      })
      .collect::<Vec<_>>()
      .into()
  }

  fn filter_include_map_names(&self, map_name: &MiniStr) -> bool {
    match self.include_map_names.as_ref() {
      [] => true,
      list => contain_map_name(list, map_name),
    }
  }

  fn filter_exclude_map_names(&self, map_name: &MiniStr) -> bool {
    match self.exclude_map_names.as_ref() {
      [] => true,
      list => !contain_map_name(list, map_name),
    }
  }

  fn filter_exclude_languages(&self, dir: &Path) -> bool {
    match self.exclude_languages.as_ref() {
      [] => true,
      list => match dir.file_name() {
        Some(dirname) => !contain_language(list, dirname),
        _ => true,
      },
    }
  }

  fn filter_include_languages(&self, dir: &Path) -> bool {
    match self.include_languages.as_ref() {
      [] => true,
      list => dir
        .file_name()
        .is_some_and(|dirname| contain_language(list, dirname)),
    }
  }

  pub fn with_include_languages<S: Into<MiniStr>>(
    mut self,
    include_languages: impl IntoIterator<Item = S>,
  ) -> Self {
    self.include_languages = include_languages
      .into_iter()
      .collect();
    self
  }

  pub fn with_include_map_names<S: Into<MiniStr>>(
    mut self,
    include_map_names: impl IntoIterator<Item = S>,
  ) -> Self {
    self.include_map_names = include_map_names
      .into_iter()
      .collect();
    self
  }

  pub fn with_exclude_languages<S: Into<MiniStr>>(
    mut self,
    exclude_languages: impl IntoIterator<Item = S>,
  ) -> Self {
    self.exclude_languages = exclude_languages
      .into_iter()
      .collect();
    self
  }

  pub fn with_exclude_map_names<S: Into<MiniStr>>(
    mut self,
    exclude_map_names: impl IntoIterator<Item = S>,
  ) -> Self {
    self.exclude_map_names = exclude_map_names
      .into_iter()
      .collect();
    self
  }
}

fn contain_language(list: &[MiniStr], language: &OsStr) -> bool {
  list
    .iter()
    .any(|item| language.eq_ignore_ascii_case(item))
}

fn contain_map_name(list: &[MiniStr], map_name: &MiniStr) -> bool {
  list
    .iter()
    .any(|item| map_name.eq_ignore_ascii_case(item))
}

fn annotate_entry_with_stem(p: DirEntry) -> Option<(DirEntry, MiniStr)> {
  p.path()
    .pipe(get_file_stem)
    .map(|stem| (p, stem))
}

fn get_file_stem<P: AsRef<Path>>(file: P) -> Option<MiniStr> {
  file
    .as_ref()
    .file_stem()?
    .to_str()?
    .to_compact_string()
    .pipe(Some)
}

fn deser_config_file<P: AsRef<Path>>(
  file: P,
) -> AnyResult<HashMap<KString, MiniStr>> {
  let cfg_text = file.pipe_ref(fs::read_to_string)?;

  if cfg_text.trim().is_empty() {
    bail!("Empty File Content")
  }

  let new_err = || "Failed to deserialize config file.".pipe(anyhow::Error::msg);

  let data = match file
    .as_ref()
    .extension()
    .map(|x| x.to_string_lossy())
    .ok_or_else(new_err)?
    .as_ref()
  {
    #[cfg(feature = "json")]
    "json" => match serde_json::from_str(&cfg_text) {
      Ok(m) => m,
      #[cfg(not(feature = "json5"))]
      e => e?,
      #[cfg(feature = "json5")]
      _ => serde_json5::from_str(&cfg_text)?,
    },
    #[cfg(feature = "json5")]
    "json5" => serde_json5::from_str(&cfg_text)?,
    #[cfg(feature = "ron")]
    "ron" => ron::from_str(&cfg_text)?,
    #[cfg(feature = "toml")]
    "toml" => toml::from_str(&cfg_text)?,
    #[cfg(feature = "yaml")]
    "yaml" | "yml" => serde_yml::from_str(&cfg_text)?,
    _ => bail!("Skip unsupported file"),
  };

  Ok(data)
}

#[derive(Getters, WithSetters, Debug, Clone, Default, Serialize, Deserialize)]
#[getset(get = "pub(crate) with_prefix", set_with = "pub(crate)")]
pub struct L10nMapEntry {
  map_name: MiniStr,
  data: Option<HashMap<KString, MiniStr>>,
  tmpl_data: Option<Resolver>,
}

impl L10nMapEntry {
  pub(crate) fn map_name_to_kstring(&self) -> KString {
    self
      .get_map_name()
      .pipe(to_kstr)
  }
}

#[cfg(test)]
pub(crate) mod dbg_shared {
  use crate::L10nResources;

  pub(crate) const DIR: &str = "../../locales/";

  pub(crate) fn new_resources() -> L10nResources {
    // L10nResources::default().with_dir(DIR.into())
    L10nResources::new(DIR)
    // .with_dsl_suffix(".dsl".into())
  }
}

#[cfg(test)]
mod tests {
  use std::{collections::BTreeMap, fs, io};

  use testutils::simple_benchmark;

  use super::*;
  use crate::resources::dbg_shared::new_resources;

  #[ignore]
  #[test]
  fn test_read_dir() -> io::Result<()> {
    for (idx, path) in dbg_shared::DIR
      .pipe(fs::read_dir)?
      .filter_map(Result::ok)
      .map(|x| x.path())
      .filter(|e| e.is_dir())
      .enumerate()
    {
      dbg!(path.file_name(), idx);
    }
    Ok(())
  }

  #[ignore]
  #[test]
  fn bench_init_res_data() {
    simple_benchmark(|| {
      dbg_shared::new_resources();
    });
  }

  #[ignore]
  #[test]
  fn test_init_res_data() {
    let res = dbg_shared::new_resources();
    let map = res
      .get_or_init_data()
      .iter()
      .collect::<BTreeMap<_, _>>();
    dbg!(map);
    // HashMap<("en", "map_name", map)>
  }

  #[ignore]
  #[test]
  fn test_only_includes_en() {
    let res = new_resources()
      .with_include_languages(["zh", "en"])
      // .with_include_map_names(["hi.tmpl"])
      .with_exclude_map_names(["hi.tmpl", "test", "unread.tmpl"])
      .with_exclude_languages(["zh"]);
    let map = res.get_or_init_data();
    // println!("{map:?}")
    dbg!(map);
  }

  #[ignore]
  #[test]
  fn test_only_includes_de_and_und() {
    let res = new_resources()
      .with_include_languages(["de", "und", "es"])
      .with_exclude_languages(["es"]);
    let map = res.get_or_init_data();
    // println!("{map:?}")
    dbg!(map);
  }
}