Skip to content

Commit

Permalink
Make flume dependency optional and cleanup features
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Aug 14, 2024
1 parent e4686fb commit f149f88
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 76 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ jobs:
strategy:
matrix:
os: [ubuntu, macos, windows]
feature: [handlebars, tera]
third-party-integration-feature: [handlebars, tera]
file-walker-feature: [walkdir, ignore]
steps:
- uses: actions/checkout@v2
- run: |
cargo build --features ${{ matrix.feature }}
cargo test --features ${{ matrix.feature }} --verbose
cargo build --features ${{ matrix.third-party-integration-feature }},${{ matrix.file-walker-feature }}
cargo test --verbose --features ${{ matrix.third-party-integration-feature }},${{ matrix.file-walker-feature }}
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ all-features = true
targets = ["x86_64-unknown-linux-gnu"]

[features]
default = ["macros", "use-ignore"]
default = ["macros", "ignore"]
macros = ["fluent-template-macros"]
use-ignore = ["ignore", "fluent-template-macros/ignore"]
ignore = ["dep:ignore", "fluent-template-macros/ignore", "dep:flume", "dep:log"]
walkdir = ["dep:walkdir", "fluent-template-macros/walkdir", "dep:log"]

[dependencies]
handlebars = { version = "5", optional = true }
Expand All @@ -54,9 +55,9 @@ unic-langid = { workspace = true, features = ["macros"] }
thiserror = "1.0.58"
tera = { version = "1.15.0", optional = true, default-features = false }
heck = "0.5.0"
ignore = { workspace = true, optional = true }
flume = { workspace = true }
log = "0.4.14"
ignore = { workspace = true, optional = true }
flume = { workspace = true, optional = true }
log = { version = "0.4", optional = true }
fluent-template-macros = { path = "./macros", optional = true, version = "0.9.4" }
once_cell = { workspace = true }
arc-swap = "1.5.0"
Expand Down
6 changes: 4 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["ignore"]
default = []
ignore = ["dep:ignore", "dep:flume"]
walkdir = ["dep:walkdir"]

[dependencies]
quote = "1.0.15"
syn = { version = "2.0", features = ["full"] }
proc-macro2 = "1.0.36"
once_cell = { workspace = true }
ignore = { workspace = true, optional = true }
flume = { workspace = true }
flume = { workspace = true, optional = true }
unic-langid = { workspace = true }
walkdir = { workspace = true, optional = true }
49 changes: 23 additions & 26 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,33 @@ fn build_resources(dir: impl AsRef<std::path::Path>) -> HashMap<String, Vec<Stri
/// Copied from `fluent_templates::fs` to avoid needing a seperate crate to
/// share the function.
pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> Vec<String> {
let (tx, rx) = flume::unbounded();

#[cfg(not(any(feature = "ignore", feature = "walkdir",)))]
compile_error!("one of the features `ignore` or `walkdir` must be enabled.");

#[cfg(feature = "ignore")]
ignore::WalkBuilder::new(path)
.follow_links(true)
.build_parallel()
.run(|| {
let tx = tx.clone();
Box::new(move |result| {
if let Ok(entry) = result {
if entry.file_type().as_ref().map_or(false, |e| e.is_file())
&& entry.path().extension().map_or(false, |e| e == "ftl")
{
tx.send(entry.path().display().to_string()).unwrap();
{
let (tx, rx) = flume::unbounded();

ignore::WalkBuilder::new(path)
.follow_links(true)
.build_parallel()
.run(|| {
let tx = tx.clone();
Box::new(move |result| {
if let Ok(entry) = result {
if entry.file_type().as_ref().map_or(false, |e| e.is_file())
&& entry.path().extension().map_or(false, |e| e == "ftl")
{
tx.send(entry.path().display().to_string()).unwrap();
}
}
}

ignore::WalkState::Continue
})
});
ignore::WalkState::Continue
})
});

return rx.drain().collect();
}

#[cfg(all(not(feature = "ignore"), feature = "walkdir"))]
walkdir::WalkDir::new(path)
Expand All @@ -148,15 +152,8 @@ pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> Vec<String> {
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().map_or(false, |e| e == "ftl"))
.for_each(|e| {
if let Ok(string) = std::fs::read_to_string(e.path()) {
let _ = tx.send(string);
} else {
log::warn!("Couldn't read {}", e.path().display());
}
});

rx.drain().collect::<Vec<_>>()
.map(|e| e.path().display().to_string())
.collect()
}

/// Loads all of your fluent resources at compile time as `&'static str`s and
Expand Down
82 changes: 44 additions & 38 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,55 @@ pub fn resources_from_vec(srcs: &[String]) -> crate::Result<Vec<FluentResource>>
}

pub(crate) fn read_from_dir<P: AsRef<Path>>(path: P) -> crate::Result<Vec<FluentResource>> {
let (tx, rx) = flume::unbounded();

#[cfg(not(any(feature = "use-ignore", feature = "walkdir",)))]
compile_error!("one of the features `use-ignore` or `walkdir` must be enabled.");

#[cfg(feature = "use-ignore")]
ignore::WalkBuilder::new(path).build_parallel().run(|| {
let tx = tx.clone();
Box::new(move |result| {
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());
#[cfg(not(any(feature = "ignore", feature = "walkdir")))]
compile_error!("one of the features `ignore` or `walkdir` must be enabled.");

#[cfg(feature = "ignore")]
{
let (tx, rx) = flume::unbounded();

ignore::WalkBuilder::new(path).build_parallel().run(|| {
let tx = tx.clone();
Box::new(move |result| {
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());
}
}
}
}

ignore::WalkState::Continue
})
});

#[cfg(all(not(feature = "ignore"), feature = "walkdir"))]
walkdir::WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().map_or(false, |e| e == "ftl"))
.for_each(|e| {
if let Ok(string) = std::fs::read_to_string(e.path()) {
let _ = tx.send(string);
} else {
log::warn!("Couldn't read {}", e.path().display());
}
ignore::WalkState::Continue
})
});

resources_from_vec(&rx.drain().collect::<Vec<_>>())
return resources_from_vec(&rx.drain().collect::<Vec<_>>());
}

#[cfg(all(not(feature = "ignore"), feature = "walkdir"))]
{
let mut srcs = Vec::new();
walkdir::WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().map_or(false, |e| e == "ftl"))
.for_each(|e| {
if let Ok(string) = std::fs::read_to_string(e.path()) {
srcs.push(string);
} else {
log::warn!("Couldn't read {}", e.path().display());
}
});
return resources_from_vec(&srcs);
}
}

#[cfg(test)]
Expand Down
6 changes: 4 additions & 2 deletions src/loader/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub fn lookup_single_language<T: AsRef<str>, R: Borrow<FluentResource>>(
text_id: &str,
args: Option<&HashMap<T, FluentValue>>,
) -> Result<String, LookupError> {
let bundle = bundles.get(lang)
let bundle = bundles
.get(lang)
.ok_or_else(|| LookupError::LangNotLoaded(lang.clone()))?;

let mut errors = Vec::new();
Expand All @@ -30,7 +31,8 @@ pub fn lookup_single_language<T: AsRef<str>, R: Borrow<FluentResource>>(
})?
.value()
} else {
bundle.get_message(text_id)
bundle
.get_message(text_id)
.ok_or_else(message_retrieve_error)?
.value()
.ok_or_else(message_retrieve_error)?
Expand Down

0 comments on commit f149f88

Please sign in to comment.