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
|
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,
))
})
}
}
|