Skip to main content

fluent_templates/
fs.rs

1use std::fs;
2use std::path::Path;
3
4use fluent_bundle::FluentResource;
5pub use unic_langid::{langid, langids, LanguageIdentifier};
6
7use crate::error;
8
9pub fn read_from_file<P: AsRef<Path>>(path: P) -> crate::Result<FluentResource> {
10    let path = path.as_ref();
11    resource_from_str(
12        &fs::read_to_string(path).map_err(|source| error::LoaderError::Fs {
13            path: path.into(),
14            source,
15        })?,
16    )
17}
18
19pub fn resource_from_str(src: &str) -> crate::Result<FluentResource> {
20    FluentResource::try_new(src.to_owned())
21        .map_err(|(_, errs)| error::FluentError::from(errs).into())
22}
23
24pub fn resources_from_vec(srcs: &[String]) -> crate::Result<Vec<FluentResource>> {
25    let mut vec = Vec::with_capacity(srcs.len());
26
27    for src in srcs {
28        vec.push(resource_from_str(src)?);
29    }
30
31    Ok(vec)
32}
33
34pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> crate::Result<Vec<FluentResource>> {
35    #[cfg(not(any(feature = "ignore", feature = "walkdir")))]
36    compile_error!("one of the features `ignore` or `walkdir` must be enabled.");
37
38    #[cfg(feature = "ignore")]
39    {
40        let (tx, rx) = flume::unbounded();
41
42        ignore::WalkBuilder::new(path).build_parallel().run(|| {
43            let tx = tx.clone();
44            Box::new(move |result| {
45                if let Ok(entry) = result {
46                    if entry
47                        .file_type()
48                        .as_ref()
49                        .is_some_and(fs::FileType::is_file)
50                        && entry.path().extension().is_some_and(|e| e == "ftl")
51                    {
52                        if let Ok(string) = std::fs::read_to_string(entry.path()) {
53                            let _ = tx.send(string);
54                        } else {
55                            log::warn!("Couldn't read {}", entry.path().display());
56                        }
57                    }
58                }
59
60                ignore::WalkState::Continue
61            })
62        });
63
64        resources_from_vec(&rx.drain().collect::<Vec<_>>())
65    }
66
67    #[cfg(all(not(feature = "ignore"), feature = "walkdir"))]
68    {
69        let mut srcs = Vec::new();
70        walkdir::WalkDir::new(path)
71            .into_iter()
72            .filter_map(|e| e.ok())
73            .filter(|e| e.file_type().is_file())
74            .filter(|e| e.path().extension().map_or(false, |e| e == "ftl"))
75            .for_each(|e| {
76                if let Ok(string) = std::fs::read_to_string(e.path()) {
77                    srcs.push(string);
78                } else {
79                    log::warn!("Couldn't read {}", e.path().display());
80                }
81            });
82        return resources_from_vec(&srcs);
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use crate::FluentBundle;
90    use std::error::Error;
91
92    #[test]
93    fn test_load_from_dir() -> Result<(), Box<dyn Error>> {
94        let dir = tempfile::tempdir()?;
95        std::fs::write(dir.path().join("core.ftl"), "foo = bar\n".as_bytes())?;
96        std::fs::write(dir.path().join("other.ftl"), "bar = baz\n".as_bytes())?;
97        std::fs::write(dir.path().join("invalid.txt"), "baz = foo\n".as_bytes())?;
98        std::fs::write(dir.path().join(".binary_file.swp"), [0, 1, 2, 3, 4, 5])?;
99
100        let result = read_from_dir(dir.path())?;
101        assert_eq!(2, result.len()); // Doesn't include the binary file or the txt file
102
103        let mut bundle = FluentBundle::new_concurrent(vec![unic_langid::langid!("en-US")]);
104        for resource in &result {
105            bundle.add_resource(resource).unwrap();
106        }
107
108        let mut errors = Vec::new();
109
110        // Ensure the correct files were loaded
111        assert_eq!(
112            "bar",
113            bundle.format_pattern(
114                bundle.get_message("foo").and_then(|m| m.value()).unwrap(),
115                None,
116                &mut errors
117            )
118        );
119
120        assert_eq!(
121            "baz",
122            bundle.format_pattern(
123                bundle.get_message("bar").and_then(|m| m.value()).unwrap(),
124                None,
125                &mut errors
126            )
127        );
128        assert_eq!(None, bundle.get_message("baz")); // The extension was txt
129
130        Ok(())
131    }
132}