summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fluent-fallback/src/localization.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/fluent-fallback/src/localization.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/fluent-fallback/src/localization.rs')
-rw-r--r--third_party/rust/fluent-fallback/src/localization.rs137
1 files changed, 137 insertions, 0 deletions
diff --git a/third_party/rust/fluent-fallback/src/localization.rs b/third_party/rust/fluent-fallback/src/localization.rs
new file mode 100644
index 0000000000..5424bcf311
--- /dev/null
+++ b/third_party/rust/fluent-fallback/src/localization.rs
@@ -0,0 +1,137 @@
+use crate::{
+ bundles::Bundles,
+ env::LocalesProvider,
+ generator::{BundleGenerator, BundleIterator, BundleStream},
+ types::ResourceId,
+};
+use once_cell::sync::OnceCell;
+use rustc_hash::FxHashSet;
+use std::rc::Rc;
+
+pub struct Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter>,
+ P: LocalesProvider,
+{
+ bundles: OnceCell<Rc<Bundles<G>>>,
+ generator: G,
+ provider: P,
+ sync: bool,
+ res_ids: FxHashSet<ResourceId>,
+}
+
+impl<G, P> Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter> + Default,
+ P: LocalesProvider + Default,
+{
+ pub fn new<I>(res_ids: I, sync: bool) -> Self
+ where
+ I: IntoIterator<Item = ResourceId>,
+ {
+ Self {
+ bundles: OnceCell::new(),
+ generator: G::default(),
+ provider: P::default(),
+ sync,
+ res_ids: FxHashSet::from_iter(res_ids.into_iter()),
+ }
+ }
+}
+
+impl<G, P> Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter>,
+ P: LocalesProvider,
+{
+ pub fn with_env<I>(res_ids: I, sync: bool, provider: P, generator: G) -> Self
+ where
+ I: IntoIterator<Item = ResourceId>,
+ {
+ Self {
+ bundles: OnceCell::new(),
+ generator,
+ provider,
+ sync,
+ res_ids: FxHashSet::from_iter(res_ids.into_iter()),
+ }
+ }
+
+ pub fn is_sync(&self) -> bool {
+ self.sync
+ }
+
+ pub fn add_resource_id<T: Into<ResourceId>>(&mut self, res_id: T) {
+ self.res_ids.insert(res_id.into());
+ self.on_change();
+ }
+
+ pub fn add_resource_ids(&mut self, res_ids: Vec<ResourceId>) {
+ self.res_ids.extend(res_ids);
+ self.on_change();
+ }
+
+ pub fn remove_resource_id<T: PartialEq<ResourceId>>(&mut self, res_id: T) -> usize {
+ self.res_ids.retain(|x| !res_id.eq(x));
+ self.on_change();
+ self.res_ids.len()
+ }
+
+ pub fn remove_resource_ids(&mut self, res_ids: Vec<ResourceId>) -> usize {
+ self.res_ids.retain(|x| !res_ids.contains(x));
+ self.on_change();
+ self.res_ids.len()
+ }
+
+ pub fn set_async(&mut self) {
+ if self.sync {
+ self.sync = false;
+ self.on_change();
+ }
+ }
+
+ pub fn on_change(&mut self) {
+ self.bundles.take();
+ }
+}
+
+impl<G, P> Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter>,
+ G::Iter: BundleIterator,
+ P: LocalesProvider,
+{
+ pub fn prefetch_sync(&mut self) {
+ let bundles = self.bundles();
+ bundles.prefetch_sync();
+ }
+}
+
+impl<G, P> Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter>,
+ G::Stream: BundleStream,
+ P: LocalesProvider,
+{
+ pub async fn prefetch_async(&mut self) {
+ let bundles = self.bundles();
+ bundles.prefetch_async().await
+ }
+}
+
+impl<G, P> Localization<G, P>
+where
+ G: BundleGenerator<LocalesIter = P::Iter>,
+ P: LocalesProvider,
+{
+ pub fn bundles(&self) -> &Rc<Bundles<G>> {
+ self.bundles.get_or_init(|| {
+ Rc::new(Bundles::new(
+ self.sync,
+ self.res_ids.clone(),
+ &self.generator,
+ &self.provider,
+ ))
+ })
+ }
+}