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
use std::fs;
use std::path::Path;
use fluent_bundle::FluentResource;
use ignore::{WalkBuilder, WalkState};
use snafu::*;
pub use unic_langid::{langid, langids, LanguageIdentifier};
use crate::error;
pub fn read_from_file<P: AsRef<Path>>(path: P) -> crate::Result<FluentResource> {
let path = path.as_ref();
resource_from_str(&fs::read_to_string(path).context(error::Fs { path })?)
}
pub fn resource_from_str(src: &str) -> crate::Result<FluentResource> {
FluentResource::try_new(src.to_owned())
.map_err(|(_, errs)| errs)
.context(error::Fluent)
}
pub fn resources_from_vec(srcs: &[String]) -> crate::Result<Vec<FluentResource>> {
let mut vec = Vec::with_capacity(srcs.len());
for src in srcs {
vec.push(resource_from_str(&src)?);
}
Ok(vec)
}
pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> crate::Result<Vec<FluentResource>> {
let (tx, rx) = flume::unbounded();
WalkBuilder::new(path).build_parallel().run(|| {
Box::new(|result| {
let tx = tx.clone();
if let Ok(entry) = result {
if entry
.file_type()
.as_ref()
.map_or(false, fs::FileType::is_file)
&& entry.path().extension().map_or(false, |e| e == "ftl")
{
if let Ok(string) = std::fs::read_to_string(entry.path()) {
let _ = tx.send(string);
} else {
log::warn!("Couldn't read {}", entry.path().display());
}
}
}
WalkState::Continue
})
});
resources_from_vec(&rx.drain().collect::<Vec<_>>())
}
#[cfg(test)]
mod tests {
use super::*;
use fluent_bundle::concurrent::FluentBundle;
use std::error::Error;
#[test]
fn test_load_from_dir() -> Result<(), Box<dyn Error>> {
let dir = tempfile::tempdir()?;
std::fs::write(dir.path().join("core.ftl"), "foo = bar\n".as_bytes())?;
std::fs::write(dir.path().join("other.ftl"), "bar = baz\n".as_bytes())?;
std::fs::write(dir.path().join("invalid.txt"), "baz = foo\n".as_bytes())?;
std::fs::write(dir.path().join(".binary_file.swp"), &[0, 1, 2, 3, 4, 5])?;
let result = read_from_dir(dir.path())?;
assert_eq!(2, result.len());
let mut bundle = FluentBundle::new(&[unic_langid::langid!("en-US")]);
for resource in &result {
bundle.add_resource(resource).unwrap();
}
let mut errors = Vec::new();
assert_eq!(
"bar",
bundle.format_pattern(
bundle.get_message("foo").and_then(|m| m.value).unwrap(),
None,
&mut errors
)
);
assert_eq!(
"baz",
bundle.format_pattern(
bundle.get_message("bar").and_then(|m| m.value).unwrap(),
None,
&mut errors
)
);
assert_eq!(None, bundle.get_message("baz"));
Ok(())
}
}