pub struct Generator<'h> { /* private fields */ }Implementations§
Source§impl Generator<'_>
impl Generator<'_>
Sourcepub fn get_or_init_dsl_maps(
&self,
) -> &BTreeMap<LangID, BTreeMap<KString, OrderedAST>>
pub fn get_or_init_dsl_maps( &self, ) -> &BTreeMap<LangID, BTreeMap<KString, OrderedAST>>
Initializes and retrieves DSL-based localization maps
§Behavior
- Performs lazy initialization of DSL maps on first access
- Subsequent calls return cached reference
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
Sourcepub fn get_or_init_highlight_maps(
&'h self,
) -> Option<&'h BTreeMap<LangID, BTreeMap<(KString, KString), MiniStr>>>
pub fn get_or_init_highlight_maps( &'h self, ) -> Option<&'h BTreeMap<LangID, BTreeMap<(KString, KString), MiniStr>>>
Initializes and retrieves highlighted localization maps
§Behavior
- Lazy initialization of special highlight entries
- Only initializes if highlight configuration exists
Sourcepub fn get_or_init_merged_maps(
&'h self,
) -> &'h BTreeMap<LangID, BTreeMap<(KString, KString), MiniStr>>
pub fn get_or_init_merged_maps( &'h self, ) -> &'h BTreeMap<LangID, BTreeMap<(KString, KString), MiniStr>>
Provides merged view of localization data
Merges Self::get_or_init_maps() & Self::get_or_init_highlight_maps()
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
Sourcepub fn output_bincode_all_in_one(&'h self, map_type: MapType) -> AnyResult<()>
pub fn output_bincode_all_in_one(&'h self, map_type: MapType) -> AnyResult<()>
Generates consolidated binary file containing all localization data
>“outdir()/all{bincode_suffix}”
§Behavior
- For DSL maps: Creates “all{bincode_suffix}” containing DSL data
- For regular maps: Creates “all{bincode_suffix}” containing all language data
§Errors
Returns AnyResult with error details for:
- File I/O failures
- Serialization errors
Sourcepub fn output_bincode(&'h self, map_type: MapType) -> AnyResult<()>
pub fn output_bincode(&'h self, map_type: MapType) -> AnyResult<()>
Generates individual binary files per language
>“outdir()/{language}{bincode_suffix}”
§Behavior
- For DSL maps: Creates separate files for each glossa-DSL content
- For regular maps: Creates separate files for each language
- Skips empty datasets
§Example
“../../locales/en/unread.dsl.toml”:
num-to-en = """
$num ->
[0] zero
[1] one
[2] two
[3] three
*[other] {$num}
"""
unread = "unread message"
unread-count = """
$num ->
[0] No {unread}s.
[1] You have { num-to-en } {unread}.
*[other] You have { num-to-en } {unread}s.
"""
show-unread-messages-count = "{unread-count}"rs_code:
use glossa_codegen::{L10nResources, Generator, generator::MapType};
use std::path::Path;
let resources = L10nResources::new("../../locales/");
// Output to tmp/{language}.tmpl.bincode
Generator::default()
.with_resources(resources)
.with_outdir("tmp")
.with_bincode_suffix(".tmpl.bincode".into())
.output_bincode(MapType::DSL)?;
let file = Path::new("tmp").join("en.tmpl.bincode");
let tmpl_maps = glossa_shared::decode::file::decode_single_file_to_dsl_map(file)?;
let unread_tmpl = tmpl_maps
.get("unread")
.expect("Failed to get DSL-AST (map_name: unread)");
let get_text = |num_str| {
unread_tmpl.get_with_context("show-unread-messages-count", &[("num", num_str)])
};
let one = get_text("1")?;
assert_eq!(one, "You have one unread message.");
let zero = get_text("0")?;
assert_eq!(zero, "No unread messages.");
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
Sourcepub fn output_match_fn_all_in_one(&'h self, non_dsl: MapType) -> Result<String>
pub fn output_match_fn_all_in_one(&'h self, non_dsl: MapType) -> Result<String>
Generates a consolidated match function containing all localization mappings
Generates:
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
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());Sourcepub fn output_match_fn_all_in_one_by_language(
&'h self,
non_dsl: MapType,
) -> Result<String>
pub fn output_match_fn_all_in_one_by_language( &'h self, non_dsl: MapType, ) -> Result<String>
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.
Sourcepub fn output_match_fn_all_in_one_by_language_and_key(
&'h self,
non_dsl: MapType,
) -> Result<String>
pub fn output_match_fn_all_in_one_by_language_and_key( &'h self, non_dsl: MapType, ) -> Result<String>
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:
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.
Sourcepub fn output_locales_fn(
&'h self,
map_type: MapType,
const_lang_id: bool,
) -> AnyResult<String>
pub fn output_locales_fn( &'h self, map_type: MapType, const_lang_id: bool, ) -> AnyResult<String>
Generates a function listing all available locales
§Parameters
map_type- Mapping type to process
const_lang_id- Whether to generate lang_id constants
§Example
let function_data = new_generator()
.with_visibility(crate::Visibility::Pub)
.output_locales_fn(
MapType::Regular,
false,
)?;function_data:
pub const fn all_locales() -> [lang_id::LangID; 107] {
use lang_id::consts::*;
[
lang_id_af(),
lang_id_am(),
lang_id_ar(),
lang_id_az(),
lang_id_be(),
lang_id_bg(),
lang_id_bn(),
lang_id_bs(),
lang_id_ca(),
lang_id_ceb(),
lang_id_co(),
...
]}Sourcepub fn output_match_fn(&'h self, non_dsl: MapType) -> Result<()>
pub fn output_match_fn(&'h self, non_dsl: MapType) -> Result<()>
Generates individual match functions per locale
=> const fn map(map_name: &[u8], key: &[u8]) -> &'static str
Sourcepub fn output_match_fn_by_key(&'h self, non_dsl: MapType) -> Result<()>
pub fn output_match_fn_by_key(&'h self, non_dsl: MapType) -> Result<()>
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()
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
Sourcepub fn output_phf_all_in_one(&'h self, non_dsl: MapType) -> Result<String>
pub fn output_phf_all_in_one(&'h self, non_dsl: MapType) -> Result<String>
Collect all localized resources into a const phf::OrderedMap
function, i.e., a single table can accommodate different language,
map_name, and map_key.
§Example
use glossa_codegen::{L10nResources, Generator, generator::MapType};
use glossa_shared::tap::Pipe;
let data = L10nResources::new("../../locales/")
.with_include_map_names(["error"])
.with_include_languages([
"de",
"zh-pinyin",
"zh",
"pt",
"es",
"en",
"en-GB",
]);
let function_data = Generator::default()
.with_resources(data)
.output_phf_all_in_one(MapType::Regular)?;
§function data:
// glossa_shared::{phf, PhfL10nAllInOneMap, PhfTripleKey};
pub(crate) const fn map() -> super::PhfL10nAllInOneMap {
use super::PhfTripleKey as Key;
super::phf::OrderedMap {
key: 12913932095322966823,
disps: &[(2, 3), (2, 0)],
idxs: &[5, 4, 0, 6, 3, 2, 1],
entries: &[
(
Key(r#"de"#, r##"error"##, r###"text-not-found"###),
r#####"Kein lokalisierter Text gefunden"#####,
),
(
Key(r#"en"#, r##"error"##, r###"text-not-found"###),
r#####"No localized text found"#####,
),
(
Key(r#"en-GB"#, r##"error"##, r###"text-not-found"###),
r#####"No localised text found"#####,
),
(
Key(r#"es"#, r##"error"##, r###"text-not-found"###),
r#####"No se encontró texto localizado"#####,
),
(
Key(r#"pt"#, r##"error"##, r###"text-not-found"###),
r#####"Nenhum texto localizado encontrado"#####,
),
(
Key(r#"zh"#, r##"error"##, r###"text-not-found"###),
r#####"未找到本地化文本"#####,
),
(
Key(r#"zh-Latn-CN"#, r##"error"##, r###"text-not-found"###),
r#####"MeiYou ZhaoDao BenDiHua WenBen"#####,
),
],
}
}§Get Text
use glossa_shared::PhfTripleKey;
fn test_get_text() {
let map = map();
let get_text =
|language| map.get(&PhfTripleKey(language, "error", "text-not-found"));
let zh_text = get_text("zh");
assert_eq!(zh_text, Some(&"未找到本地化文本"));
let language_chain = ["gsw", "de-CH", "de", "en"];
let text = language_chain
.into_iter()
.find_map(get_text);
assert_eq!(text, Some(&"Kein lokalisierter Text gefunden"));
}Sourcepub fn output_phf(&'h self, non_dsl: MapType) -> Result<()>
pub fn output_phf(&'h self, non_dsl: MapType) -> Result<()>
Generates Perfect Hash Function (PHF) maps for localization data.
=> const fn map() -> super::PhfL10nOrderedMap
§Behavior
- Processes non-DSL maps in parallel
- Filters out empty localization datasets
- Generates PHF maps preserving insertion order
- Creates individual Rust module files per language
§Errors
Returns io::Result for file I/O operations failures
Sourcepub fn output_phf_by_key(&'h self, non_dsl: MapType) -> Result<()>
pub fn output_phf_by_key(&'h self, non_dsl: MapType) -> Result<()>
Generates Perfect Hash Function (PHF) maps for localization data.
=> const fn map() -> super::OrderedMap<&'static str, &'static str>
Note: The generated PHF map is only for localization data where map_name can be ignored. If map_name cannot be ignored, please use output_phf.
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
pub fn get_resources(&self) -> &Box<L10nResources>
pub fn get_visibility(&self) -> &Visibility
pub fn get_outdir(&self) -> &Option<PathBuf>
pub fn get_bincode_suffix(&self) -> &MiniStr
pub fn get_mod_prefix(&self) -> &MiniStr
pub fn get_highlight(&self) -> &Option<Box<HighlightCfgMap<'h>>>
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
pub fn with_visibility(self, val: Visibility) -> Self
pub fn with_bincode_suffix(self, val: MiniStr) -> Self
pub fn with_mod_prefix(self, val: MiniStr) -> Self
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
pub fn get_outdir_mut(&mut self) -> &mut Option<PathBuf>
pub fn get_bincode_suffix_mut(&mut self) -> &mut MiniStr
pub fn get_mod_prefix_mut(&mut self) -> &mut MiniStr
Source§impl<'h> Generator<'h>
impl<'h> Generator<'h>
pub fn with_highlight(self, highlight: HighlightCfgMap<'h>) -> Self
pub fn set_highlight(&mut self, highlight: Option<HighlightCfgMap<'h>>)
Source§impl Generator<'_>
impl Generator<'_>
Sourcepub fn with_resources(self, resources: L10nResources) -> Self
pub fn with_resources(self, resources: L10nResources) -> Self
Configures the generator with localization resources, resetting cached mappings.
This method performs two primary actions:
- Replaces the current
L10nResourceswith the provided instance - Resets the internal
lazy_mapsto their default empty state
§Side Effects
The cache clearance ensures any existing lazy-loaded mappings won’t conflict with new resources. Subsequent operations will rebuild mappings from the updated resources.
§Example
use glossa_codegen::{Generator, L10nResources};
// Initialize resources from a localization directory
let resources = L10nResources::new("../../locales/");
// Create generator with configured resources
let _generator = Generator::default()
.with_resources(resources);Trait Implementations§
Auto Trait Implementations§
impl<'h> Freeze for Generator<'h>
impl<'h> RefUnwindSafe for Generator<'h>
impl<'h> Send for Generator<'h>
impl<'h> Sync for Generator<'h>
impl<'h> Unpin for Generator<'h>
impl<'h> UnwindSafe for Generator<'h>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.