|
|
use serde::{Deserialize, Serialize}; |
|
|
use turbo_tasks::{NonLocalValue, trace::TraceRawVcs}; |
|
|
use turbo_tasks_fs::FileSystemPath; |
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, NonLocalValue)] |
|
|
pub enum ContextCondition { |
|
|
All(Vec<ContextCondition>), |
|
|
Any(Vec<ContextCondition>), |
|
|
Not(Box<ContextCondition>), |
|
|
InDirectory(String), |
|
|
InPath(FileSystemPath), |
|
|
} |
|
|
|
|
|
impl ContextCondition { |
|
|
|
|
|
pub fn all(conditions: Vec<ContextCondition>) -> ContextCondition { |
|
|
ContextCondition::All(conditions) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn any(conditions: Vec<ContextCondition>) -> ContextCondition { |
|
|
ContextCondition::Any(conditions) |
|
|
} |
|
|
|
|
|
|
|
|
#[allow(clippy::should_implement_trait)] |
|
|
pub fn not(condition: ContextCondition) -> ContextCondition { |
|
|
ContextCondition::Not(Box::new(condition)) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn matches(&self, path: &FileSystemPath) -> bool { |
|
|
match self { |
|
|
ContextCondition::All(conditions) => conditions.iter().all(|c| c.matches(path)), |
|
|
ContextCondition::Any(conditions) => conditions.iter().any(|c| c.matches(path)), |
|
|
ContextCondition::Not(condition) => !condition.matches(path), |
|
|
ContextCondition::InPath(other_path) => path.is_inside_or_equal_ref(other_path), |
|
|
ContextCondition::InDirectory(dir) => { |
|
|
|
|
|
if let Some(pos) = path.path.find(dir) { |
|
|
let end = pos + dir.len(); |
|
|
(pos == 0 || path.path.as_bytes()[pos - 1] == b'/') |
|
|
&& (end == path.path.len() || path.path.as_bytes()[end] == b'/') |
|
|
} else { |
|
|
false |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|