1use std::{borrow::Cow, collections::HashMap, fmt, mem, ops, path::PathBuf, sync::Arc};
2
3use fluent::types::FluentNumber;
4use once_cell::sync::Lazy;
5use semver::Version;
6use zng_ext_fs_watcher::WatcherReadStatus;
7use zng_layout::context::LayoutDirection;
8use zng_txt::{ToTxt, Txt};
9use zng_var::{ArcEq, IntoVar, Var, VarValue, const_var, context_var, impl_from_and_into_var};
10
11use crate::{L10N, lang, service::L10N_SV};
12
13#[derive(Clone, Debug)]
15pub struct LangResources(pub Vec<LangResource>);
16impl ops::Deref for LangResources {
17 type Target = Vec<LangResource>;
18
19 fn deref(&self) -> &Self::Target {
20 &self.0
21 }
22}
23impl ops::DerefMut for LangResources {
24 fn deref_mut(&mut self) -> &mut Self::Target {
25 &mut self.0
26 }
27}
28impl LangResources {
29 pub async fn wait(&self) {
31 for res in &self.0 {
32 res.wait().await;
33 }
34 }
35
36 pub fn perm(self) {
38 for res in self.0 {
39 res.perm()
40 }
41 }
42}
43
44#[derive(Clone)]
46#[must_use = "resource can unload if dropped"]
47pub struct LangResource {
48 pub(super) res: Var<Option<ArcEq<fluent::FluentResource>>>,
49 pub(super) status: Var<LangResourceStatus>,
50}
51
52impl fmt::Debug for LangResource {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 f.debug_struct("LangResource")
55 .field("status", &self.status.get())
56 .finish_non_exhaustive()
57 }
58}
59impl LangResource {
60 pub fn resource(&self) -> &Var<Option<ArcEq<fluent::FluentResource>>> {
62 &self.res
63 }
64
65 pub fn status(&self) -> &Var<LangResourceStatus> {
67 &self.status
68 }
69
70 pub fn perm(self) {
72 L10N_SV.write().push_perm_resource(self);
73 }
74
75 pub async fn wait(&self) {
77 while matches!(self.status.get(), LangResourceStatus::Loading) {
78 self.status.wait_update().await;
79 }
80 }
81}
82
83#[derive(Clone, Debug)]
85pub enum LangResourceStatus {
86 NotAvailable,
90 Loading,
92 Loaded,
94 Errors(StatusError),
100}
101impl fmt::Display for LangResourceStatus {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 LangResourceStatus::NotAvailable => write!(f, "not available"),
105 LangResourceStatus::Loading => write!(f, "loading…"),
106 LangResourceStatus::Loaded => write!(f, "loaded"),
107 LangResourceStatus::Errors(e) => {
108 writeln!(f, "errors:")?;
109 for e in e {
110 writeln!(f, " {e}")?;
111 }
112 Ok(())
113 }
114 }
115 }
116}
117impl PartialEq for LangResourceStatus {
118 fn eq(&self, other: &Self) -> bool {
119 match (self, other) {
120 (Self::Errors(a), Self::Errors(b)) => a.is_empty() && b.is_empty(),
121 _ => core::mem::discriminant(self) == core::mem::discriminant(other),
122 }
123 }
124}
125impl Eq for LangResourceStatus {}
126impl WatcherReadStatus<StatusError> for LangResourceStatus {
127 fn idle() -> Self {
128 Self::Loaded
129 }
130
131 fn reading() -> Self {
132 Self::Loading
133 }
134
135 fn read_error(e: StatusError) -> Self {
136 Self::Errors(e)
137 }
138}
139impl WatcherReadStatus<LangResourceStatus> for LangResourceStatus {
140 fn idle() -> Self {
141 Self::Loaded
142 }
143
144 fn reading() -> Self {
145 Self::Loading
146 }
147
148 fn read_error(e: LangResourceStatus) -> Self {
149 e
150 }
151}
152
153type StatusError = Vec<Arc<dyn std::error::Error + Send + Sync>>;
154
155pub struct L10nMessageBuilder {
161 pub(super) file: LangFilePath,
162 pub(super) id: Txt,
163 pub(super) attribute: Txt,
164 pub(super) fallback: Txt,
165 pub(super) args: Vec<(Txt, Var<L10nArgument>)>,
166}
167impl L10nMessageBuilder {
168 pub fn arg(mut self, name: Txt, value: impl IntoVar<L10nArgument>) -> Self {
170 self.args.push((name, value.into_var()));
171 self
172 }
173 #[doc(hidden)]
174 pub fn l10n_arg(self, name: &'static str, value: Var<L10nArgument>) -> Self {
175 self.arg(Txt::from_static(name), value)
176 }
177
178 pub fn build_for(self, lang: impl Into<Langs>) -> Var<Txt> {
180 L10N_SV
181 .write()
182 .localized_message(lang.into(), self.file, self.id, self.attribute, self.fallback, self.args)
183 }
184
185 pub fn build(self) -> Var<Txt> {
187 let Self {
188 file,
189 id,
190 attribute,
191 fallback,
192 args,
193 } = self;
194 LANG_VAR.flat_map(move |l| {
195 L10N_SV.write().localized_message(
196 l.clone(),
197 file.clone(),
198 id.clone(),
199 attribute.clone(),
200 fallback.clone(),
201 args.clone(),
202 )
203 })
204 }
205}
206
207#[derive(Clone, Debug, PartialEq)]
211pub enum L10nArgument {
212 Txt(Txt),
214 Number(FluentNumber),
216}
217impl_from_and_into_var! {
218 fn from(txt: Txt) -> L10nArgument {
219 L10nArgument::Txt(txt)
220 }
221 fn from(txt: &'static str) -> L10nArgument {
222 L10nArgument::Txt(Txt::from_static(txt))
223 }
224 fn from(txt: String) -> L10nArgument {
225 L10nArgument::Txt(Txt::from(txt))
226 }
227 fn from(t: char) -> L10nArgument {
228 L10nArgument::Txt(Txt::from_char(t))
229 }
230 fn from(number: FluentNumber) -> L10nArgument {
231 L10nArgument::Number(number)
232 }
233 fn from(b: bool) -> L10nArgument {
234 b.to_txt().into()
235 }
236}
237macro_rules! impl_from_and_into_var_number {
238 ($($literal:tt),+) => {
239 impl_from_and_into_var! {
240 $(
241 fn from(number: $literal) -> L10nArgument {
242 FluentNumber::from(number).into()
243 }
244 )+
245 }
246 }
247}
248impl_from_and_into_var_number! { u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize, f32, f64 }
249impl L10nArgument {
250 pub fn fluent_value(&self) -> fluent::FluentValue<'_> {
252 match self {
253 L10nArgument::Txt(t) => fluent::FluentValue::String(Cow::Borrowed(t.as_str())),
254 L10nArgument::Number(n) => fluent::FluentValue::Number(n.clone()),
255 }
256 }
257 pub fn to_fluent_value(&self) -> fluent::FluentValue<'static> {
259 match self {
260 L10nArgument::Txt(t) => fluent::FluentValue::String(Cow::Owned(t.to_string())),
261 L10nArgument::Number(n) => fluent::FluentValue::Number(n.clone()),
262 }
263 }
264}
265
266#[doc(hidden)]
267pub struct L10nSpecialize<T>(pub Option<T>);
268#[doc(hidden)]
269pub trait IntoL10nVar {
270 fn to_l10n_var(&mut self) -> Var<L10nArgument>;
271}
272
273impl<T: Into<L10nArgument>> IntoL10nVar for L10nSpecialize<T> {
274 fn to_l10n_var(&mut self) -> Var<L10nArgument> {
275 const_var(self.0.take().unwrap().into())
276 }
277}
278impl<T: VarValue + Into<L10nArgument>> IntoL10nVar for &mut L10nSpecialize<Var<T>> {
279 fn to_l10n_var(&mut self) -> Var<L10nArgument> {
280 self.0.take().unwrap().map_into()
281 }
282}
283impl IntoL10nVar for &mut &mut L10nSpecialize<Var<L10nArgument>> {
284 fn to_l10n_var(&mut self) -> Var<L10nArgument> {
285 self.0.take().unwrap()
286 }
287}
288
289context_var! {
290 pub static LANG_VAR: Langs = L10N.app_lang();
296}
297
298#[derive(PartialEq, Eq, Hash, Clone, Default, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
307#[serde(transparent)]
308pub struct Lang(pub unic_langid::LanguageIdentifier);
309impl Lang {
310 pub fn direction(&self) -> LayoutDirection {
312 crate::from_unic_char_direction(self.0.character_direction())
313 }
314
315 pub fn matches(&self, other: &Self, self_as_range: bool, other_as_range: bool) -> bool {
353 if !self.0.language.matches(other.0.language, self_as_range, other_as_range) {
354 return false;
355 }
356
357 fn tag_matches<P: PartialEq>(tag1: &Option<P>, tag2: &Option<P>, as_range1: bool, as_range2: bool) -> bool {
358 (as_range1 && tag1.is_none()) || (as_range2 && tag2.is_none()) || tag1 == tag2
359 }
360 if !tag_matches(&self.0.script, &other.0.script, self_as_range, other_as_range)
361 || !tag_matches(&self.region, &other.region, self_as_range, other_as_range)
362 {
363 return false;
364 }
365
366 (self_as_range && self.variants_no_machine().next().is_none())
367 || (other_as_range && other.variants_no_machine().next().is_none())
368 || self.variants_no_machine().eq(other.variants_no_machine())
369 }
370 fn variants_no_machine(&self) -> impl Iterator<Item = &unic_langid::subtags::Variant> {
371 self.0.variants().filter(|v| v.as_str() != "machine")
372 }
373
374 pub fn is_machine_translation(&self) -> bool {
380 self.0.variants().any(|v| v == "machine")
381 }
382
383 #[cfg(feature = "lang_autonym")]
392 pub fn autonym(&self) -> Option<LangAutonym> {
393 let lang = self.0.language.as_str();
394 let script = self.0.script.as_ref().map(|s| s.as_str()).unwrap_or("");
395 let region = self.0.region.as_ref().map(|r| r.as_str()).unwrap_or("");
396 let (name, region) = match (lang, script, region) {
397 ("af", "", "") => ("Afrikaans", ""),
400 ("am", "", "") => ("አማርኛ", ""),
401 ("ar", "", "") => ("العربية", ""),
402 ("as", "", "") => ("অসমীয়া", ""),
403 ("az", "", "") => ("Azərbaycan", ""),
404 ("be", "", "") => ("Беларуская", ""),
405 ("bg", "", "") => ("Български", ""),
406 ("bn", "", "") => ("বাংলা", ""),
407 ("bs", "", "") => ("Bosanski", ""),
408 ("ca", "", "") => ("Català", ""),
409 ("cs", "", "") => ("Čeština", ""),
410 ("cy", "", "") => ("Cymraeg", ""),
411 ("da", "", "") => ("Dansk", ""),
412 ("de", "", "") => ("Deutsch", ""),
413 ("el", "", "") => ("Ελληνικά", ""),
414 ("en", "", "") => ("English", ""),
415 ("en", "", "GB") => ("English", "United Kingdom"),
416 ("en", "", "US") => ("English", "United States"),
417 ("es", "", "") => ("Español", ""),
418 ("es", "", "419") => ("Español", "Latinoamérica"),
419 ("es", "", "ES") => ("Español", "España"),
420 ("et", "", "") => ("Eesti", ""),
421 ("eu", "", "") => ("Euskara", ""),
422 ("fa", "", "") => ("فارسی", ""),
423 ("fi", "", "") => ("Suomi", ""),
424 ("fil", "", "") => ("Filipino", ""),
425 ("fr", "", "") => ("Français", ""),
426 ("fr", "", "CA") => ("Français", "Canada"),
427 ("ga", "", "") => ("Gaeilge", ""),
428 ("gd", "", "") => ("Gàidhlig", ""),
429 ("gl", "", "") => ("Galego", ""),
430 ("gu", "", "") => ("ગુજરાતી", ""),
431 ("he", "", "") => ("עברית", ""),
432 ("hi", "", "") => ("हिन्दी", ""),
433 ("hr", "", "") => ("Hrvatski", ""),
434 ("hu", "", "") => ("Magyar", ""),
435 ("hy", "", "") => ("Հայերեն", ""),
436 ("id", "", "") => ("Indonesia", ""),
437 ("is", "", "") => ("Íslenska", ""),
438 ("it", "", "") => ("Italiano", ""),
439 ("ja", "", "") => ("日本語", ""),
440 ("ka", "", "") => ("ქართული", ""),
441 ("kk", "", "") => ("Қазақ тілі", ""),
442 ("km", "", "") => ("ខ្មែរ", ""),
443 ("kn", "", "") => ("ಕನ್ನಡ", ""),
444 ("ko", "", "") => ("한국어", ""),
445 ("ky", "", "") => ("Кыргызча", ""),
446 ("lo", "", "") => ("ລາວ", ""),
447 ("lt", "", "") => ("Lietuvių", ""),
448 ("lv", "", "") => ("Latviešu", ""),
449 ("mk", "", "") => ("Македонски", ""),
450 ("ml", "", "") => ("മലയാളം", ""),
451 ("mn", "", "") => ("Монгол", ""),
452 ("mr", "", "") => ("मराठी", ""),
453 ("ms", "", "") => ("Melayu", ""),
454 ("my", "", "") => ("မြန်မာ", ""),
455 ("nb", "", "") => ("Norsk bokmål", ""),
456 ("ne", "", "") => ("नेपाली", ""),
457 ("nl", "", "") => ("Nederlands", ""),
458 ("nn", "", "") => ("Norsk nynorsk", ""),
459 ("or", "", "") => ("ଓଡ଼ିଆ", ""),
460 ("pa", "", "") => ("ਪੰਜਾਬੀ", ""),
461 ("pl", "", "") => ("Polski", ""),
462 ("ps", "", "") => ("پښتو", ""),
463 ("pt", "", "") => ("Português", ""),
464 ("pt", "", "BR") => ("Português", "Brasil"),
465 ("pt", "", "PT") => ("Português", "Portugal"),
466 ("ro", "", "") => ("Română", ""),
467 ("ru", "", "") => ("Русский", ""),
468 ("si", "", "") => ("සිංහල", ""),
469 ("sk", "", "") => ("Slovenčina", ""),
470 ("sl", "", "") => ("Slovenščina", ""),
471 ("sq", "", "") => ("Shqip", ""),
472 ("sr", "", "") => ("Српски", ""),
473 ("sr", "Latn", "") => ("Srpski", ""),
474 ("sv", "", "") => ("Svenska", ""),
475 ("sw", "", "") => ("Kiswahili", ""),
476 ("ta", "", "") => ("தமிழ்", ""),
477 ("te", "", "") => ("తెలుగు", ""),
478 ("th", "", "") => ("ไทย", ""),
479 ("tr", "", "") => ("Türkçe", ""),
480 ("uk", "", "") => ("Українська", ""),
481 ("ur", "", "") => ("اردو", ""),
482 ("uz", "", "") => ("O‘zbek", ""),
483 ("vi", "", "") => ("Tiếng việt", ""),
484 ("zh", "", "") => ("中文", ""),
485 ("zh", "Hans", "") => ("简体中文", ""),
486 ("zh", "Hant", "") => ("繁體中文", ""),
487 ("zh", "", "TW") => ("繁體中文", "台灣"),
488 ("zu", "", "") => ("Isizulu", ""),
489 _ => {
490 if self.0.region.is_some() {
491 let q = Lang(unic_langid::LanguageIdentifier::from_parts(
492 self.0.language,
493 self.0.script,
494 None,
495 &[],
496 ));
497 return q.autonym();
498 } else {
499 return None;
500 }
501 }
502 };
503 Some(LangAutonym {
504 language: name,
505 region: if region.is_empty() { None } else { Some(region) },
506 })
507 }
508
509 pub fn cmp_display(&self, other: &Self) -> std::cmp::Ordering {
513 let (name, region) = self.name_region_str();
514 let (other_name, other_region) = other.name_region_str();
515 name.cmp(other_name).then_with(|| region.cmp(&other_region))
516 }
517
518 fn name_region_str(&self) -> (&str, Option<&str>) {
519 #[cfg(feature = "lang_autonym")]
520 if let Some(a) = self.autonym() {
521 let region = a.region.or_else(|| self.0.region.as_ref().map(|r| r.as_str()));
522 return (a.language, region);
523 }
524 (self.0.language.as_str(), self.0.region.as_ref().map(|r| r.as_str()))
525 }
526}
527impl ops::Deref for Lang {
528 type Target = unic_langid::LanguageIdentifier;
529
530 fn deref(&self) -> &Self::Target {
531 &self.0
532 }
533}
534impl fmt::Debug for Lang {
535 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
536 write!(f, "{}", self.0)
537 }
538}
539impl fmt::Display for Lang {
543 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544 let (name, region) = self.name_region_str();
545 if f.alternate()
546 && let Some(r) = region
547 {
548 write!(f, "{name} ({r})")
549 } else {
550 write!(f, "{name}")
551 }
552 }
553}
554impl std::str::FromStr for Lang {
555 type Err = unic_langid::LanguageIdentifierError;
556
557 fn from_str(s: &str) -> Result<Self, Self::Err> {
558 let s = s.trim();
559 if s.is_empty() {
560 return Ok(lang!(und));
561 }
562 unic_langid::LanguageIdentifier::from_str(s).map(Lang)
563 }
564}
565
566#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
568#[cfg(feature = "lang_autonym")]
569pub struct LangAutonym {
570 pub language: &'static str,
572 pub region: Option<&'static str>,
576}
577#[cfg(feature = "lang_autonym")]
578impl fmt::Debug for LangAutonym {
579 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
580 if f.alternate() {
581 f.debug_struct("LangAutonym")
582 .field("language", &self.language)
583 .field("region", &self.region)
584 .finish()
585 } else {
586 write!(f, "{self}")
587 }
588 }
589}
590#[cfg(feature = "lang_autonym")]
592impl fmt::Display for LangAutonym {
593 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594 write!(f, "{}", self.language)?;
595 if let Some(r) = self.region
596 && !f.alternate()
597 {
598 write!(f, " ({r})")?;
599 }
600 Ok(())
601 }
602}
603
604#[derive(Clone, PartialEq, Eq, Default, Hash, serde::Serialize, serde::Deserialize)]
606#[serde(transparent)]
607pub struct Langs(pub Vec<Lang>);
608impl fmt::Debug for Langs {
609 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
610 struct DisplayLangs<'a>(&'a [Lang]);
611 impl fmt::Debug for DisplayLangs<'_> {
612 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
613 f.debug_list().entries(self.0.iter()).finish()
614 }
615 }
616 if f.alternate() {
617 f.debug_tuple("Langs").field(&DisplayLangs(&self.0)).finish()
618 } else {
619 fmt::Debug::fmt(&DisplayLangs(&self.0), f)
620 }
621 }
622}
623impl Langs {
624 pub fn best(&self) -> &Lang {
626 static NONE: Lazy<Lang> = Lazy::new(|| lang!(und));
627 self.first().unwrap_or(&NONE)
628 }
629}
630impl ops::Deref for Langs {
631 type Target = Vec<Lang>;
632
633 fn deref(&self) -> &Self::Target {
634 &self.0
635 }
636}
637impl ops::DerefMut for Langs {
638 fn deref_mut(&mut self) -> &mut Self::Target {
639 &mut self.0
640 }
641}
642impl_from_and_into_var! {
643 fn from(lang: Lang) -> Langs {
644 Langs(vec![lang])
645 }
646 fn from(lang: Option<Lang>) -> Langs {
647 Langs(lang.into_iter().collect())
648 }
649}
650impl fmt::Display for Langs {
651 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
652 let mut sep = "";
653 for l in self.iter() {
654 write!(f, "{sep}{l}")?;
655 sep = ", ";
656 }
657 Ok(())
658 }
659}
660impl std::str::FromStr for Langs {
661 type Err = unic_langid::LanguageIdentifierError;
662
663 fn from_str(s: &str) -> Result<Self, Self::Err> {
664 if s.trim().is_empty() {
665 return Ok(Langs(vec![]));
666 }
667 let mut r = Self(vec![]);
668 for lang in s.split(',') {
669 r.0.push(lang.trim().parse()?)
670 }
671 Ok(r)
672 }
673}
674
675#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
677#[serde(transparent)]
678pub struct LangMap<V> {
679 inner: Vec<(Lang, V)>,
680}
681impl<V> Default for LangMap<V> {
682 fn default() -> Self {
683 Self { inner: Default::default() }
684 }
685}
686impl<V> LangMap<V> {
687 pub fn new() -> Self {
689 LangMap::default()
690 }
691
692 pub fn with_capacity(capacity: usize) -> Self {
694 LangMap {
695 inner: Vec::with_capacity(capacity),
696 }
697 }
698
699 fn exact_i(&self, lang: &Lang) -> Option<usize> {
700 for (i, (key, _)) in self.inner.iter().enumerate() {
701 if key == lang {
702 return Some(i);
703 }
704 }
705 None
706 }
707
708 fn best_i(&self, lang: &Lang) -> Option<usize> {
709 let mut best = None;
710 let mut best_weight = 0;
711
712 for (i, (key, _)) in self.inner.iter().enumerate() {
713 if lang.matches(key, true, true) {
714 let mut weight = 1;
715 let mut eq = 0;
716
717 if key.language == lang.language {
719 weight += 200;
720 eq += 1;
721 }
722 if key.script == lang.script {
724 weight += 100;
725 eq += 1;
726 }
727 if key.region == lang.region {
729 weight += 30;
730 eq += 1;
731 }
732
733 if eq == 3 && lang.variants().eq(key.variants()) {
734 return Some(i);
736 }
737
738 if !key.is_machine_translation() {
740 weight += 60;
741 }
742
743 if best_weight < weight {
744 best_weight = weight;
745 best = Some(i);
746 }
747 }
748 }
749
750 best
751 }
752
753 pub fn best_match(&self, lang: &Lang) -> Option<&Lang> {
755 if let Some(i) = self.best_i(lang) {
756 Some(&self.inner[i].0)
757 } else {
758 None
759 }
760 }
761
762 pub fn get(&self, lang: &Lang) -> Option<&V> {
764 if let Some(i) = self.best_i(lang) {
765 Some(&self.inner[i].1)
766 } else {
767 None
768 }
769 }
770
771 pub fn get_exact(&self, lang: &Lang) -> Option<&V> {
773 if let Some(i) = self.exact_i(lang) {
774 Some(&self.inner[i].1)
775 } else {
776 None
777 }
778 }
779
780 pub fn get_mut(&mut self, lang: &Lang) -> Option<&mut V> {
782 if let Some(i) = self.best_i(lang) {
783 Some(&mut self.inner[i].1)
784 } else {
785 None
786 }
787 }
788
789 pub fn get_exact_mut(&mut self, lang: &Lang) -> Option<&mut V> {
791 if let Some(i) = self.exact_i(lang) {
792 Some(&mut self.inner[i].1)
793 } else {
794 None
795 }
796 }
797
798 pub fn get_exact_or_insert(&mut self, lang: Lang, new: impl FnOnce() -> V) -> &mut V {
800 if let Some(i) = self.exact_i(&lang) {
801 return &mut self.inner[i].1;
802 }
803 let i = self.inner.len();
804 self.inner.push((lang, new()));
805 &mut self.inner[i].1
806 }
807
808 pub fn insert(&mut self, lang: Lang, value: V) -> Option<V> {
812 if let Some(i) = self.exact_i(&lang) {
813 Some(mem::replace(&mut self.inner[i].1, value))
814 } else {
815 self.inner.push((lang, value));
816 None
817 }
818 }
819
820 pub fn remove(&mut self, lang: &Lang) -> Option<V> {
822 if let Some(i) = self.exact_i(lang) {
823 Some(self.inner.swap_remove(i).1)
824 } else {
825 None
826 }
827 }
828
829 pub fn remove_all(&mut self, lang: &Lang) -> usize {
833 let mut count = 0;
834 self.inner.retain(|(key, _)| {
835 let rmv = lang.matches(key, true, false);
836 if rmv {
837 count += 1
838 }
839 !rmv
840 });
841 count
842 }
843
844 pub fn pop(&mut self) -> Option<(Lang, V)> {
846 self.inner.pop()
847 }
848
849 pub fn is_empty(&self) -> bool {
851 self.inner.is_empty()
852 }
853
854 pub fn len(&self) -> usize {
856 self.inner.len()
857 }
858
859 pub fn clear(&mut self) {
861 self.inner.clear()
862 }
863
864 pub fn keys(&self) -> impl std::iter::ExactSizeIterator<Item = &Lang> {
866 self.inner.iter().map(|(k, _)| k)
867 }
868
869 pub fn values(&self) -> impl std::iter::ExactSizeIterator<Item = &V> {
871 self.inner.iter().map(|(_, v)| v)
872 }
873
874 pub fn values_mut(&mut self) -> impl std::iter::ExactSizeIterator<Item = &mut V> {
876 self.inner.iter_mut().map(|(_, v)| v)
877 }
878
879 pub fn into_values(self) -> impl std::iter::ExactSizeIterator<Item = V> {
881 self.inner.into_iter().map(|(_, v)| v)
882 }
883
884 pub fn iter(&self) -> impl std::iter::ExactSizeIterator<Item = (&Lang, &V)> {
886 self.inner.iter().map(|(k, v)| (k, v))
887 }
888
889 pub fn iter_mut(&mut self) -> impl std::iter::ExactSizeIterator<Item = (&Lang, &mut V)> {
891 self.inner.iter_mut().map(|(k, v)| (&*k, v))
892 }
893}
894impl<V> LangMap<HashMap<LangFilePath, V>> {
895 pub fn get_file(&self, lang: &Lang, file: &LangFilePath) -> Option<&V> {
897 let files = self.get(lang)?;
898 if let Some(exact) = files.get(file) {
899 return Some(exact);
900 }
901 Self::best_file(files, file).map(|(_, v)| v)
902 }
903
904 pub fn best_file_match(&self, lang: &Lang, file: &LangFilePath) -> Option<&LangFilePath> {
906 let files = self.get(lang)?;
907 if let Some((exact, _)) = files.get_key_value(file) {
908 return Some(exact);
909 }
910 Self::best_file(files, file).map(|(k, _)| k)
911 }
912
913 fn best_file<'a>(files: &'a HashMap<LangFilePath, V>, file: &LangFilePath) -> Option<(&'a LangFilePath, &'a V)> {
914 let mut best = None;
915 let mut best_dist = u64::MAX;
916 for (k, v) in files {
917 if let Some(d) = k.matches(file)
918 && d < best_dist
919 {
920 best = Some((k, v));
921 best_dist = d;
922 }
923 }
924 best
925 }
926}
927impl<V> IntoIterator for LangMap<V> {
928 type Item = (Lang, V);
929
930 type IntoIter = std::vec::IntoIter<(Lang, V)>;
931
932 fn into_iter(self) -> Self::IntoIter {
933 self.inner.into_iter()
934 }
935}
936impl<V: PartialEq> PartialEq for LangMap<V> {
937 fn eq(&self, other: &Self) -> bool {
938 if self.len() != other.len() {
939 return false;
940 }
941 for (k, v) in &self.inner {
942 if other.get_exact(k) != Some(v) {
943 return false;
944 }
945 }
946 true
947 }
948}
949impl<V: Eq> Eq for LangMap<V> {}
950
951#[derive(Clone, Debug)]
953pub struct FluentParserErrors(pub Vec<fluent_syntax::parser::ParserError>);
954impl fmt::Display for FluentParserErrors {
955 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
956 let mut sep = "";
957 for e in &self.0 {
958 write!(f, "{sep}{e}")?;
959 sep = "\n";
960 }
961 Ok(())
962 }
963}
964impl std::error::Error for FluentParserErrors {
965 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
966 if self.0.len() == 1 { Some(&self.0[0]) } else { None }
967 }
968}
969
970#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
978#[non_exhaustive]
979pub struct LangFilePath {
980 pub pkg_name: Txt,
982 pub pkg_version: Version,
984 pub file: Txt,
986}
987impl Ord for LangFilePath {
988 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
989 let self_pkg = self.actual_pkg_data();
990 let other_pkg = other.actual_pkg_data();
991 match self_pkg.0.cmp(other_pkg.0) {
992 core::cmp::Ordering::Equal => {}
993 ord => return ord,
994 }
995 match self_pkg.1.cmp(other_pkg.1) {
996 core::cmp::Ordering::Equal => {}
997 ord => return ord,
998 }
999 self.file().cmp(&other.file())
1000 }
1001}
1002impl PartialOrd for LangFilePath {
1003 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1004 Some(self.cmp(other))
1005 }
1006}
1007impl std::hash::Hash for LangFilePath {
1008 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1009 self.actual_pkg_data().hash(state);
1010 self.file().hash(state);
1011 }
1012}
1013impl Eq for LangFilePath {}
1014impl PartialEq for LangFilePath {
1015 fn eq(&self, other: &Self) -> bool {
1016 self.actual_pkg_data() == other.actual_pkg_data() && self.file() == other.file()
1017 }
1018}
1019impl LangFilePath {
1020 pub fn new(pkg_name: impl Into<Txt>, pkg_version: Version, file: impl Into<Txt>) -> Self {
1022 let r = Self {
1023 pkg_name: pkg_name.into(),
1024 pkg_version,
1025 file: file.into(),
1026 };
1027 debug_assert!(
1029 r.file
1030 .rsplit_once('.')
1031 .map(|(_, ext)| !ext.eq_ignore_ascii_case("ftl"))
1032 .unwrap_or(true),
1033 "file `{}` must not have extension",
1034 r.file
1035 );
1036 debug_assert!(r.file != "_", "file `_` should be an empty string");
1037 r
1038 }
1039
1040 pub fn current_app(file: impl Into<Txt>) -> LangFilePath {
1047 let about = zng_env::about();
1048 Self::new(about.pkg_name.clone(), about.version.clone(), file.into())
1049 }
1050
1051 pub fn is_current_app(&self) -> bool {
1055 self.is_current_app_no_check() || {
1056 let about = zng_env::about();
1057 self.pkg_name == about.pkg_name && self.pkg_version == about.version
1058 }
1059 }
1060
1061 fn is_current_app_no_check(&self) -> bool {
1062 self.pkg_name.is_empty() || self.pkg_version.pre.as_str() == "local"
1063 }
1064
1065 fn actual_pkg_data(&self) -> (&Txt, &Version) {
1066 if self.is_current_app_no_check() {
1067 let about = zng_env::about();
1068 (&about.pkg_name, &about.version)
1069 } else {
1070 (&self.pkg_name, &self.pkg_version)
1071 }
1072 }
1073
1074 pub fn pkg_name(&self) -> Txt {
1080 self.actual_pkg_data().0.clone()
1081 }
1082
1083 pub fn pkg_version(&self) -> Version {
1089 self.actual_pkg_data().1.clone()
1090 }
1091
1092 pub fn file(&self) -> Txt {
1096 if self.file.is_empty() {
1097 Txt::from_char('_')
1098 } else {
1099 self.file.clone()
1100 }
1101 }
1102
1103 pub fn to_path(&self, lang: &Lang) -> PathBuf {
1111 let mut file = self.file.as_str();
1112 if file.is_empty() {
1113 file = "_";
1114 }
1115 if self.is_current_app() {
1116 format!("{lang}/{file}.ftl")
1117 } else {
1118 format!("{lang}/deps/{}/{}/{file}.ftl", self.pkg_name, self.pkg_version)
1119 }
1120 .into()
1121 }
1122
1123 pub fn matches(&self, search: &Self) -> Option<u64> {
1135 let (self_name, self_version) = self.actual_pkg_data();
1136 let (search_name, search_version) = search.actual_pkg_data();
1137
1138 if self_name != search_name {
1139 return None;
1140 }
1141
1142 if self.file != search.file {
1143 let file_a = self.file.rsplit_once('.').map(|t| t.0).unwrap_or(self.file.as_str());
1144 let file_b = search.file.rsplit_once('.').map(|t| t.0).unwrap_or(search.file.as_str());
1145 if file_a != file_b {
1146 let is_empty_a = file_a == "_" || file_a.is_empty();
1147 let is_empty_b = file_b == "_" || file_b.is_empty();
1148 if !(is_empty_a && is_empty_b) {
1149 return None;
1150 }
1151 }
1152 tracing::warn!(
1153 "fallback matching `{}` with `{}`, file was not expected to have extension",
1154 self.file,
1155 search.file
1156 )
1157 }
1158
1159 fn dist(a: u64, b: u64, shift: u64) -> u64 {
1160 let (l, s) = match a.cmp(&b) {
1161 std::cmp::Ordering::Equal => return 0,
1162 std::cmp::Ordering::Less => (b, a),
1163 std::cmp::Ordering::Greater => (a, b),
1164 };
1165
1166 (l - s).min(u16::MAX as u64) << (16 * shift)
1167 }
1168
1169 let mut d = 0;
1170 if self_version.build != search_version.build {
1171 d = 1;
1172 }
1173 if self_version.pre != search_version.pre {
1174 d |= 0b10;
1175 }
1176
1177 d |= dist(self_version.patch, search_version.patch, 1);
1178 d |= dist(self_version.minor, search_version.minor, 2);
1179 d |= dist(self_version.major, search_version.major, 3);
1180
1181 Some(d)
1182 }
1183}
1184impl_from_and_into_var! {
1185 fn from(file: Txt) -> LangFilePath {
1186 LangFilePath::current_app(file)
1187 }
1188
1189 fn from(file: &'static str) -> LangFilePath {
1190 LangFilePath::current_app(file)
1191 }
1192
1193 fn from(file: String) -> LangFilePath {
1194 LangFilePath::current_app(file)
1195 }
1196}
1197
1198#[cfg(test)]
1199mod tests {
1200 use super::*;
1201
1202 #[test]
1203 fn file_matches() {
1204 fn check(a: &str, b: &str, c: &str) {
1205 let ap = LangFilePath::new("name", a.parse().unwrap(), "file");
1206 let bp = LangFilePath::new("name", b.parse().unwrap(), "file");
1207 let cp = LangFilePath::new("name", c.parse().unwrap(), "file");
1208
1209 let ab = ap.matches(&bp);
1210 let ac = ap.matches(&cp);
1211
1212 assert!(ab < ac, "expected {a}.matches({b}) < {a}.matches({c})")
1213 }
1214
1215 check("0.0.0", "0.0.1", "0.1.0");
1216 check("0.0.1", "0.1.0", "1.0.0");
1217 check("0.0.0-pre", "0.0.0-pre+build", "0.0.0-other+build");
1218 check("0.0.0+build", "0.0.0+build", "0.0.0+other");
1219 check("0.0.1", "0.0.2", "0.0.3");
1220 check("0.1.0", "0.2.0", "0.3.0");
1221 check("1.0.0", "2.0.0", "3.0.0");
1222 check("1.0.0", "1.1.0", "2.0.0");
1223 }
1224
1225 #[test]
1226 fn file_name_mismatches() {
1227 let ap = LangFilePath::new("name", "1.0.0".parse().unwrap(), "file-a");
1228 let bp = LangFilePath::new("name", "1.0.0".parse().unwrap(), "file-b");
1229
1230 assert!(ap.matches(&bp).is_none());
1231 }
1232}