zng_ext_l10n/sources/
dir.rs

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
use std::{collections::HashMap, io, path::PathBuf, str::FromStr as _, sync::Arc};

use semver::Version;
use zng_clone_move::clmv;
use zng_ext_fs_watcher::WATCHER;
use zng_txt::Txt;
use zng_var::{types::WeakArcVar, var, ArcEq, ArcVar, BoxedVar, BoxedWeakVar, LocalVar, Var as _, WeakVar as _};

use crate::{FluentParserErrors, L10nSource, Lang, LangFilePath, LangMap, LangResourceStatus};

/// Represents localization resources synchronized from files in a directory.
///
/// The expected directory layout is `{dir}/{lang}/{file}.ftl` app files and `{dir}/{lang}/deps/{pkg-name}/{pkg-version}/{file}.ftl`
/// for dependencies.
pub struct L10nDir {
    dir_watch: BoxedVar<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>>,
    dir_watch_status: BoxedVar<LangResourceStatus>,
    res: HashMap<(Lang, LangFilePath), L10nFile>,
}
impl L10nDir {
    /// Start watching the `dir` for localization files.
    pub fn open(dir: impl Into<PathBuf>) -> Self {
        Self::new(dir.into())
    }
    fn new(dir: PathBuf) -> Self {
        let (dir_watch, status) = WATCHER.read_dir_status(
            dir.clone(),
            true,
            Arc::default(),
            clmv!(|d| {
                let mut set: LangMap<HashMap<LangFilePath, PathBuf>> = LangMap::new();
                let mut errors: Vec<Arc<dyn std::error::Error + Send + Sync>> = vec![];
                let mut dir = None;
                for entry in d.min_depth(0).max_depth(5) {
                    let entry = match entry {
                        Ok(e) => e,
                        Err(e) => {
                            errors.push(Arc::new(e));
                            continue;
                        }
                    };
                    let ty = entry.file_type();

                    if dir.is_none() {
                        // get the watched dir (first because of min_depth(0))
                        if !ty.is_dir() {
                            tracing::error!("L10N path not a directory");
                            return Err(LangResourceStatus::NotAvailable);
                        }
                        dir = Some(entry.path().to_owned());
                        continue;
                    }

                    const EXT: unicase::Ascii<&'static str> = unicase::Ascii::new("ftl");

                    let is_ftl = ty.is_file()
                        && entry
                            .file_name()
                            .to_str()
                            .and_then(|n| n.rsplit_once('.'))
                            .map(|(_, ext)| ext.is_ascii() && unicase::Ascii::new(ext) == EXT)
                            .unwrap_or(false);

                    if !is_ftl {
                        continue;
                    }

                    let mut utf8_path = [""; 5];
                    for (i, part) in entry.path().iter().rev().take(entry.depth()).enumerate() {
                        match part.to_str() {
                            Some(p) => utf8_path[entry.depth() - i - 1] = p,
                            None => continue,
                        }
                    }

                    let (lang, mut file) = match entry.depth() {
                        // lang/file.ftl
                        2 => {
                            let lang = utf8_path[0];
                            let file = Txt::from_str(utf8_path[1].rsplit_once('.').unwrap().0);
                            (lang, LangFilePath::current_app(file))
                        }
                        // lang/deps/pkg-name/pkg-version/file.ftl
                        5 => {
                            if utf8_path[1] != "deps" {
                                continue;
                            }
                            let lang = utf8_path[0];
                            let pkg_name = Txt::from_str(utf8_path[2]);
                            let pkg_version: Version = match utf8_path[3].parse() {
                                Ok(v) => v,
                                Err(e) => {
                                    errors.push(Arc::new(e));
                                    continue;
                                }
                            };
                            let file = Txt::from_str(utf8_path[4]);

                            (lang, LangFilePath::new(pkg_name, pkg_version, file))
                        }
                        _ => {
                            continue;
                        }
                    };

                    let lang = match Lang::from_str(lang) {
                        Ok(l) => l,
                        Err(e) => {
                            errors.push(Arc::new(e));
                            continue;
                        }
                    };

                    if file.file == "_" {
                        file.file = "".into();
                    }

                    set.get_exact_or_insert(lang, Default::default)
                        .insert(file, entry.path().to_owned());
                }

                if errors.is_empty() {
                    // Loaded set by `dir_watch` to avoid race condition in wait.
                } else {
                    let s = LangResourceStatus::Errors(errors);
                    tracing::error!("'loading available' {s}");
                    return Err(s);
                }

                Ok(Some(Arc::new(set)))
            }),
        );

        Self {
            dir_watch: dir_watch.boxed(),
            dir_watch_status: status.read_only().boxed(),
            res: HashMap::new(),
        }
    }
}
impl L10nSource for L10nDir {
    fn available_langs(&mut self) -> BoxedVar<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>> {
        self.dir_watch.clone()
    }
    fn available_langs_status(&mut self) -> BoxedVar<LangResourceStatus> {
        self.dir_watch_status.clone()
    }

    fn lang_resource(&mut self, lang: Lang, file: LangFilePath) -> BoxedVar<Option<ArcEq<fluent::FluentResource>>> {
        match self.res.entry((lang, file)) {
            std::collections::hash_map::Entry::Occupied(mut e) => {
                if let Some(out) = e.get().res.upgrade() {
                    out
                } else {
                    let (lang, file) = e.key();
                    let out = resource_var(&self.dir_watch, e.get().status.clone(), lang.clone(), file.clone());
                    e.get_mut().res = out.downgrade();
                    out
                }
            }
            std::collections::hash_map::Entry::Vacant(e) => {
                let mut f = L10nFile::new();
                let (lang, file) = e.key();
                let out = resource_var(&self.dir_watch, f.status.clone(), lang.clone(), file.clone());
                f.res = out.downgrade();
                e.insert(f);
                out
            }
        }
    }

    fn lang_resource_status(&mut self, lang: Lang, file: LangFilePath) -> BoxedVar<LangResourceStatus> {
        self.res
            .entry((lang, file))
            .or_insert_with(L10nFile::new)
            .status
            .read_only()
            .boxed()
    }
}
struct L10nFile {
    res: BoxedWeakVar<Option<ArcEq<fluent::FluentResource>>>,
    status: ArcVar<LangResourceStatus>,
}
impl L10nFile {
    fn new() -> Self {
        Self {
            res: WeakArcVar::default().boxed(),
            status: var(LangResourceStatus::Loading),
        }
    }
}

fn resource_var(
    dir_watch: &BoxedVar<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>>,
    status: ArcVar<LangResourceStatus>,
    lang: Lang,
    file: LangFilePath,
) -> BoxedVar<Option<ArcEq<fluent::FluentResource>>> {
    dir_watch
        .map(move |w| w.get_file(&lang, &file).cloned())
        .flat_map(move |p| match p {
            Some(p) => {
                status.set(LangResourceStatus::Loading);

                let r = WATCHER.read(
                    p.clone(),
                    None,
                    clmv!(status, |file| {
                        status.set(LangResourceStatus::Loading);

                        match file.and_then(|mut f| f.string()) {
                            Ok(flt) => match fluent::FluentResource::try_new(flt) {
                                Ok(flt) => {
                                    // ok
                                    // Loaded set by `r` to avoid race condition in waiter.
                                    return Some(Some(ArcEq::new(flt)));
                                }
                                Err(e) => {
                                    let e = FluentParserErrors(e.1);
                                    tracing::error!("error parsing fluent resource, {e}");
                                    status.set(LangResourceStatus::Errors(vec![Arc::new(e)]));
                                }
                            },
                            Err(e) => {
                                if matches!(e.kind(), io::ErrorKind::NotFound) {
                                    status.set(LangResourceStatus::NotAvailable);
                                } else {
                                    tracing::error!("error loading fluent resource, {e}");
                                    status.set(LangResourceStatus::Errors(vec![Arc::new(e)]));
                                }
                            }
                        }
                        // not ok
                        Some(None)
                    }),
                );
                r.bind_map(&status, |_| LangResourceStatus::Loaded).perm();
                r.boxed()
            }
            None => LocalVar(None).boxed(),
        })
}