Skip to content

Commit

Permalink
fix error propagation in ContextCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 9, 2023
1 parent e91ad12 commit e0a8a54
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
31 changes: 14 additions & 17 deletions crates/turbopack/src/condition.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use async_recursion::async_recursion;
use futures::{stream, StreamExt};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -32,32 +33,28 @@ impl ContextCondition {

#[async_recursion]
/// Returns true if the condition matches the context.
pub async fn matches(&self, context: &FileSystemPath) -> bool {
pub async fn matches(&self, context: &FileSystemPath) -> Result<bool> {
match self {
ContextCondition::All(conditions) => {
stream::iter(conditions)
.all(|c| async move { c.matches(context).await })
.fold(Ok(true), |acc, c| async move {
Ok(acc? && c.matches(context).await?)
})
.await
}
ContextCondition::Any(conditions) => {
stream::iter(conditions)
.any(|c| async move { c.matches(context).await })
.fold(Ok(true), |acc, c| async move {
Ok(acc? || c.matches(context).await?)
})
.await
}
ContextCondition::Not(condition) => !condition.matches(context).await,
ContextCondition::InPath(path) => {
if let Ok(path) = path.await {
context.is_inside(&path)
} else {
false
}
}
ContextCondition::InDirectory(dir) => {
context.path.starts_with(&format!("{dir}/"))
|| context.path.contains(&format!("/{dir}/"))
|| context.path.ends_with(&format!("/{dir}"))
|| context.path == *dir
}
ContextCondition::Not(condition) => condition.matches(context).await.map(|b| !b),
ContextCondition::InPath(path) => Ok(context.is_inside(&*path.await?)),
ContextCondition::InDirectory(dir) => Ok(context.path.starts_with(&format!("{dir}/"))
|| context.path.contains(&format!("/{dir}/"))
|| context.path.ends_with(&format!("/{dir}"))
|| context.path == *dir),
}
}
}
2 changes: 1 addition & 1 deletion crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl ModuleOptionsVc {
let path_value = path.await?;

for (condition, new_context) in rules.iter() {
if condition.matches(&path_value).await {
if condition.matches(&path_value).await? {
return Ok(ModuleOptionsVc::new(path, *new_context));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub async fn resolve_options(
if !options_context_value.rules.is_empty() {
let context_value = &*context.await?;
for (condition, new_options_context) in options_context_value.rules.iter() {
if condition.matches(context_value).await {
if condition.matches(context_value).await? {
return Ok(resolve_options(context, *new_options_context));
}
}
Expand Down

0 comments on commit e0a8a54

Please sign in to comment.