glossa_codegen/
generator.rs1pub(crate) mod flattening;
2mod init_lazy_data;
3pub(crate) mod output_bincode;
4pub(crate) mod output_match;
5pub(crate) mod output_phf;
6use std::{io, path::PathBuf, sync::OnceLock};
7
8use getset::{Getters, MutGetters, WithSetters};
9use glossa_shared::tap::Pipe;
10pub use output_phf::to_lower_snake_case;
11
12use crate::{
13 L10nResources, MiniStr, Visibility,
14 generator::flattening::{L10nDSLMaps, L10nMaps},
15 internal_aliases::HighlightCfgMap,
16};
17
18#[derive(Getters, WithSetters, MutGetters, Debug, Clone)]
19#[getset(get = "pub with_prefix", set_with = "pub", get_mut = "pub with_prefix")]
20pub struct Generator<'h> {
21 #[getset(skip)]
22 #[getset(get = "pub")]
23 resources: Box<L10nResources>,
24
25 #[getset(get_mut)]
26 visibility: Visibility,
27
28 #[getset(skip)]
29 #[getset(get = "pub", get_mut = "pub")]
30 outdir: Option<PathBuf>,
31
32 bincode_suffix: MiniStr,
33 mod_prefix: MiniStr,
34
35 #[getset(skip)]
36 #[getset(get = "pub")]
37 highlight: Option<Box<HighlightCfgMap<'h>>>,
38
39 #[getset(skip)]
40 lazy_maps: Box<LazyMaps>,
42}
43
44#[cfg(feature = "highlight")]
45impl<'h> Generator<'h> {
46 pub fn with_highlight(mut self, highlight: HighlightCfgMap<'h>) -> Self {
47 self.highlight = Some(highlight.into());
48 self.clear_highlight_cache();
49 self
50 }
51
52 pub fn set_highlight(&mut self, highlight: Option<HighlightCfgMap<'h>>) {
53 self.highlight = highlight.map(|data| data.into());
54 self.clear_highlight_cache();
55 }
56 fn clear_highlight_cache(&mut self) {
57 self.lazy_maps.highlight = Default::default();
58 self.lazy_maps.merged = Default::default();
59 }
60}
61
62impl Generator<'_> {
63 pub fn with_outdir<P: Into<PathBuf>>(mut self, outdir: P) -> Self {
64 self.outdir = Some(outdir.into());
65 self
66 }
67}
68
69impl Generator<'_> {
70 pub fn with_resources(mut self, resources: L10nResources) -> Self {
97 self.resources = resources.into();
98 self.lazy_maps = Default::default();
99 self
100 }
101}
102
103#[derive(Default, Debug, Clone)]
104struct LazyMaps {
105 regular: OnceLock<L10nMaps>,
107
108 highlight: OnceLock<Option<L10nMaps>>,
110
111 dsl: OnceLock<L10nDSLMaps>,
113
114 merged: OnceLock<L10nMaps>,
116}
117
118impl Default for Generator<'_> {
119 fn default() -> Self {
130 Self {
131 bincode_suffix: MiniStr::const_new(".bincode"),
132 outdir: Default::default(),
133 resources: Default::default(),
134 mod_prefix: MiniStr::const_new("l10n_"),
137 visibility: Default::default(),
140 highlight: Default::default(),
142 lazy_maps: Default::default(),
143 }
144 }
145}
146
147#[derive(Debug, Clone, Copy)]
148pub enum MapType {
149 Regular,
150 Highlight,
151 RegularAndHighlight,
152 DSL,
153}
154
155impl MapType {
156 fn get_non_dsl_maps<'a>(
157 &self,
158 generator: &'a Generator<'a>,
159 ) -> io::Result<&'a L10nMaps> {
160 use MapType::*;
161 match self {
162 Regular => generator.get_or_init_maps(),
163 Highlight => generator
164 .get_or_init_highlight_maps()
165 .ok_or_else(|| io::Error::other("Failed to get highlight maps"))?,
166 RegularAndHighlight => generator.get_or_init_merged_maps(),
167 _ => return io::Error::other("DSL Maps are not supported.").pipe(Err),
168 }
169 .pipe(Ok)
170 }
171
172 #[must_use]
176 pub fn is_dsl(&self) -> bool {
177 matches!(self, Self::DSL)
178 }
179}
180
181impl Default for MapType {
182 fn default() -> Self {
183 Self::DSL
184 }
185}
186
187#[cfg(test)]
188pub(crate) mod dbg_generator {
189
190 use super::*;
191 use crate::resources::dbg_shared;
192
193 pub(crate) fn new_generator<'h>() -> Generator<'h> {
194 let data = dbg_shared::new_resources();
195 Generator::default()
196 .with_resources(data)
197 .with_outdir("tmp")
198 }
199
200 #[cfg(feature = "highlight")]
201 pub(crate) fn highlight_generator<'h>() -> Generator<'h> {
202 let hmap = crate::highlight::dbg_shared::new_highlight_map();
203 new_generator().with_highlight(hmap)
204 }
205
206 pub(crate) fn en_generator<'h>() -> Generator<'h> {
207 let data = dbg_shared::new_resources().with_include_languages(["en"]);
208 new_generator().with_resources(data)
209 }
210
211 pub(crate) fn en_gb_generator<'h>() -> Generator<'h> {
212 let data = dbg_shared::new_resources().with_include_languages(["en-GB"]);
213 new_generator().with_resources(data)
214 }
215
216 pub(crate) fn es_generator<'h>() -> Generator<'h> {
217 let data = dbg_shared::new_resources().with_include_languages(["es"]);
219 new_generator().with_resources(data)
220 }
221
222 pub(crate) fn de_en_fr_pt_zh_generator<'h>() -> Generator<'h> {
223 let data = dbg_shared::new_resources()
224 .with_include_languages([
225 "de", "zh", "pt", "fr", "en", "en-GB",
227 ])
228 .with_include_map_names(["yes-no"]);
229 new_generator().with_resources(data)
230 }
231}