summaryrefslogtreecommitdiffstats
path: root/intl/l10n/rust/l10nregistry-rs/src/source/mod.rs
blob: 0408727fb79a36a078f17655c71d25870bd0b06c (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
mod fetcher;
pub use fetcher::FileFetcher;
pub use fluent_fallback::types::{ResourceId, ToResourceId};

use crate::env::ErrorReporter;
use crate::errors::L10nRegistryError;
use crate::fluent::FluentResource;

use std::{
    borrow::Borrow,
    cell::RefCell,
    fmt,
    hash::{Hash, Hasher},
    pin::Pin,
    rc::Rc,
    task::Poll,
};

use futures::{future::Shared, Future, FutureExt};
use rustc_hash::FxHashMap;
use unic_langid::LanguageIdentifier;

pub type RcResource = Rc<FluentResource>;

/// An option type whose None variant is either optional or required.
///
/// This behaves similarly to the standard-library [`Option`] type
/// except that there are two [`None`]-like variants:
/// [`ResourceOption::MissingOptional`] and [`ResourceOption::MissingRequired`].
#[derive(Clone, Debug)]
pub enum ResourceOption {
    /// An available resource.
    Some(RcResource),
    /// A missing optional resource.
    MissingOptional,
    /// A missing required resource.
    MissingRequired,
}

impl ResourceOption {
    /// Creates a resource option that is either [`ResourceOption::MissingRequired`]
    /// or [`ResourceOption::MissingOptional`] based on whether the given [`ResourceId`]
    /// is required or optional.
    pub fn missing_resource(resource_id: &ResourceId) -> Self {
        if resource_id.is_required() {
            Self::MissingRequired
        } else {
            Self::MissingOptional
        }
    }

    /// Returns [`true`] if this option contains a recource, otherwise [`false`].
    pub fn is_some(&self) -> bool {
        matches!(self, Self::Some(_))
    }

    /// Resource [`true`] if this option is missing a resource of any type, otherwise [`false`].
    pub fn is_none(&self) -> bool {
        matches!(self, Self::MissingOptional | Self::MissingRequired)
    }

    /// Returns [`true`] if this option is missing a required resource, otherwise [`false`].
    pub fn is_required_and_missing(&self) -> bool {
        matches!(self, Self::MissingRequired)
    }
}

impl From<ResourceOption> for Option<RcResource> {
    fn from(other: ResourceOption) -> Self {
        match other {
            ResourceOption::Some(id) => Some(id),
            _ => None,
        }
    }
}

pub type ResourceFuture = Shared<Pin<Box<dyn Future<Output = ResourceOption>>>>;

#[derive(Debug, Clone)]
pub enum ResourceStatus {
    /// The resource is missing.  Don't bother trying to fetch.
    MissingRequired,
    MissingOptional,
    /// The resource is loading and future will deliver the result.
    Loading(ResourceFuture),
    /// The resource is loaded and parsed.
    Loaded(RcResource),
}

impl From<ResourceOption> for ResourceStatus {
    fn from(input: ResourceOption) -> Self {
        match input {
            ResourceOption::Some(res) => Self::Loaded(res),
            ResourceOption::MissingOptional => Self::MissingOptional,
            ResourceOption::MissingRequired => Self::MissingRequired,
        }
    }
}

impl Future for ResourceStatus {
    type Output = ResourceOption;

    fn poll(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
        use ResourceStatus::*;

        let this = &mut *self;

        match this {
            MissingRequired => ResourceOption::MissingRequired.into(),
            MissingOptional => ResourceOption::MissingOptional.into(),
            Loaded(res) => ResourceOption::Some(res.clone()).into(),
            Loading(res) => Pin::new(res).poll(cx),
        }
    }
}

/// `FileSource` provides a generic fetching and caching of fluent resources.
/// The user of `FileSource` provides a [`FileFetcher`](trait.FileFetcher.html)
/// implementation and `FileSource` takes care of the rest.
#[derive(Clone)]
pub struct FileSource {
    /// Name of the FileSource, e.g. "browser"
    pub name: String,
    /// Pre-formatted path for the FileSource, e.g. "/browser/data/locale/{locale}/"
    pub pre_path: String,
    /// Metasource name for the FileSource, e.g. "app", "langpack"
    /// Only sources from the same metasource are passed into the solver.
    pub metasource: String,
    /// The locales for which data is present in the FileSource, e.g. ["en-US", "pl"]
    locales: Vec<LanguageIdentifier>,
    shared: Rc<Inner>,
    index: Option<Vec<String>>,
    pub options: FileSourceOptions,
}

struct Inner {
    fetcher: Box<dyn FileFetcher>,
    error_reporter: Option<RefCell<Box<dyn ErrorReporter>>>,
    entries: RefCell<FxHashMap<String, ResourceStatus>>,
}

impl fmt::Display for FileSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl PartialEq<FileSource> for FileSource {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.metasource == other.metasource
    }
}

impl Eq for FileSource {}

impl Hash for FileSource {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state)
    }
}

#[derive(PartialEq, Clone, Debug, Default)]
pub struct FileSourceOptions {
    pub allow_override: bool,
}

impl FileSource {
    /// Create a `FileSource` using the provided [`FileFetcher`](../trait.FileFetcher.html).
    pub fn new(
        name: String,
        metasource: Option<String>,
        locales: Vec<LanguageIdentifier>,
        pre_path: String,
        options: FileSourceOptions,
        fetcher: impl FileFetcher + 'static,
    ) -> Self {
        FileSource {
            name,
            metasource: metasource.unwrap_or_default(),
            pre_path,
            locales,
            index: None,
            shared: Rc::new(Inner {
                entries: RefCell::new(FxHashMap::default()),
                fetcher: Box::new(fetcher),
                error_reporter: None,
            }),
            options,
        }
    }

    pub fn new_with_index(
        name: String,
        metasource: Option<String>,
        locales: Vec<LanguageIdentifier>,
        pre_path: String,
        options: FileSourceOptions,
        fetcher: impl FileFetcher + 'static,
        index: Vec<String>,
    ) -> Self {
        FileSource {
            name,
            metasource: metasource.unwrap_or_default(),
            pre_path,
            locales,
            index: Some(index),
            shared: Rc::new(Inner {
                entries: RefCell::new(FxHashMap::default()),
                fetcher: Box::new(fetcher),
                error_reporter: None,
            }),
            options,
        }
    }

    pub fn set_reporter(&mut self, reporter: impl ErrorReporter + 'static) {
        let shared = Rc::get_mut(&mut self.shared).unwrap();
        shared.error_reporter = Some(RefCell::new(Box::new(reporter)));
    }
}

fn calculate_pos_in_source(source: &str, idx: usize) -> (usize, usize) {
    let mut ptr = 0;
    let mut result = (1, 1);
    for line in source.lines() {
        let bytes = line.as_bytes().len();
        if ptr + bytes < idx {
            ptr += bytes + 1;
            result.0 += 1;
        } else {
            result.1 = idx - ptr + 1;
            break;
        }
    }
    result
}

impl FileSource {
    fn get_path(&self, locale: &LanguageIdentifier, resource_id: &ResourceId) -> String {
        format!(
            "{}{}",
            self.pre_path.replace("{locale}", &locale.to_string()),
            resource_id.value,
        )
    }

    fn fetch_sync(&self, resource_id: &ResourceId) -> ResourceOption {
        self.shared
            .fetcher
            .fetch_sync(resource_id)
            .ok()
            .map(|source| match FluentResource::try_new(source) {
                Ok(res) => ResourceOption::Some(Rc::new(res)),
                Err((res, errors)) => {
                    if let Some(reporter) = &self.shared.error_reporter {
                        reporter.borrow().report_errors(
                            errors
                                .into_iter()
                                .map(|e| L10nRegistryError::FluentError {
                                    resource_id: resource_id.clone(),
                                    loc: Some(calculate_pos_in_source(res.source(), e.pos.start)),
                                    error: e.into(),
                                })
                                .collect(),
                        );
                    }
                    ResourceOption::Some(Rc::new(res))
                }
            })
            .unwrap_or_else(|| ResourceOption::missing_resource(resource_id))
    }

    /// Attempt to synchronously fetch resource for the combination of `locale`
    /// and `path`. Returns `Some(ResourceResult)` if the resource is available,
    /// else `None`.
    pub fn fetch_file_sync(
        &self,
        locale: &LanguageIdentifier,
        resource_id: &ResourceId,
        overload: bool,
    ) -> ResourceOption {
        use ResourceStatus::*;

        if self.has_file(locale, resource_id) == Some(false) {
            return ResourceOption::missing_resource(resource_id);
        }

        let full_path_id = self
            .get_path(locale, resource_id)
            .to_resource_id(resource_id.resource_type);

        let res = self.shared.lookup_resource(full_path_id.clone(), || {
            self.fetch_sync(&full_path_id).into()
        });

        match res {
            MissingRequired => ResourceOption::MissingRequired,
            MissingOptional => ResourceOption::MissingOptional,
            Loaded(res) => ResourceOption::Some(res),
            Loading(..) if overload => {
                // A sync load has been requested for the same resource that has
                // a pending async load in progress. How do we handle this?
                //
                // Ideally, we would sync load and resolve all the pending
                // futures with the result. With the current Futures and
                // combinators, it's unclear how to proceed. One potential
                // solution is to store a oneshot::Sender and
                // Shared<oneshot::Receiver>. When the async loading future
                // resolves it would check that the state is still `Loading`,
                // and if so, send the result. The sync load would do the same
                // send on the oneshot::Sender.
                //
                // For now, we warn and return the resource, paying the cost of
                // duplication of the resource.
                self.fetch_sync(&full_path_id)
            }
            Loading(..) => {
                panic!("[l10nregistry] Attempting to synchronously load file {} while it's being loaded asynchronously.", &full_path_id.value);
            }
        }
    }

    /// Attempt to fetch resource for the combination of `locale` and `path`.
    /// Returns [`ResourceStatus`](enum.ResourceStatus.html) which is
    /// a `Future` that can be polled.
    pub fn fetch_file(
        &self,
        locale: &LanguageIdentifier,
        resource_id: &ResourceId,
    ) -> ResourceStatus {
        use ResourceStatus::*;

        if self.has_file(locale, resource_id) == Some(false) {
            return ResourceOption::missing_resource(resource_id).into();
        }

        let full_path_id = self
            .get_path(locale, resource_id)
            .to_resource_id(resource_id.resource_type);

        self.shared.lookup_resource(full_path_id.clone(), || {
            let shared = self.shared.clone();
            Loading(read_resource(full_path_id, shared).boxed_local().shared())
        })
    }

    /// Determine if the `FileSource` has a loaded resource for the combination
    /// of `locale` and `path`. Returns `Some(true)` if the file is loaded, else
    /// `Some(false)`. `None` is returned if there is an outstanding async fetch
    /// pending and the status is yet to be determined.
    pub fn has_file<L: Borrow<LanguageIdentifier>>(
        &self,
        locale: L,
        path: &ResourceId,
    ) -> Option<bool> {
        let locale = locale.borrow();
        if !self.locales.contains(locale) {
            Some(false)
        } else {
            let full_path = self.get_path(locale, path);
            if let Some(index) = &self.index {
                return Some(index.iter().any(|p| p == &full_path));
            }
            self.shared.has_file(&full_path)
        }
    }

    pub fn locales(&self) -> &[LanguageIdentifier] {
        &self.locales
    }

    pub fn get_index(&self) -> Option<&Vec<String>> {
        self.index.as_ref()
    }
}

impl std::fmt::Debug for FileSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
        if let Some(index) = &self.index {
            f.debug_struct("FileSource")
                .field("name", &self.name)
                .field("metasource", &self.metasource)
                .field("locales", &self.locales)
                .field("pre_path", &self.pre_path)
                .field("index", index)
                .finish()
        } else {
            f.debug_struct("FileSource")
                .field("name", &self.name)
                .field("metasource", &self.metasource)
                .field("locales", &self.locales)
                .field("pre_path", &self.pre_path)
                .finish()
        }
    }
}

impl Inner {
    fn lookup_resource<F>(&self, resource_id: ResourceId, f: F) -> ResourceStatus
    where
        F: FnOnce() -> ResourceStatus,
    {
        let mut lock = self.entries.borrow_mut();
        lock.entry(resource_id.value).or_insert_with(f).clone()
    }

    fn update_resource(&self, resource_id: ResourceId, resource: ResourceOption) -> ResourceOption {
        let mut lock = self.entries.borrow_mut();
        let entry = lock.get_mut(&resource_id.value);
        match entry {
            Some(entry) => *entry = resource.clone().into(),
            _ => panic!("Expected "),
        }
        resource
    }

    pub fn has_file(&self, full_path: &str) -> Option<bool> {
        match self.entries.borrow().get(full_path) {
            Some(ResourceStatus::MissingRequired) => Some(false),
            Some(ResourceStatus::MissingOptional) => Some(false),
            Some(ResourceStatus::Loaded(_)) => Some(true),
            Some(ResourceStatus::Loading(_)) | None => None,
        }
    }
}

async fn read_resource(resource_id: ResourceId, shared: Rc<Inner>) -> ResourceOption {
    let resource = shared
        .fetcher
        .fetch(&resource_id)
        .await
        .ok()
        .map(|source| match FluentResource::try_new(source) {
            Ok(res) => ResourceOption::Some(Rc::new(res)),
            Err((res, errors)) => {
                if let Some(reporter) = &shared.error_reporter {
                    reporter.borrow().report_errors(
                        errors
                            .into_iter()
                            .map(|e| L10nRegistryError::FluentError {
                                resource_id: resource_id.clone(),
                                loc: Some(calculate_pos_in_source(res.source(), e.pos.start)),
                                error: e.into(),
                            })
                            .collect(),
                    );
                }
                ResourceOption::Some(Rc::new(res))
            }
        })
        .unwrap_or_else(|| ResourceOption::missing_resource(&resource_id));
    // insert the resource into the cache
    shared.update_resource(resource_id, resource)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn calculate_source_pos() {
        let source = r#"
key = Value

key2 = Value 2
"#
        .trim();
        let result = calculate_pos_in_source(source, 0);
        assert_eq!(result, (1, 1));

        let result = calculate_pos_in_source(source, 1);
        assert_eq!(result, (1, 2));

        let result = calculate_pos_in_source(source, 12);
        assert_eq!(result, (2, 1));

        let result = calculate_pos_in_source(source, 13);
        assert_eq!(result, (3, 1));
    }
}