turbopack/module_options/
transition_rule.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use turbo_tasks::{NonLocalValue, ResolvedVc, trace::TraceRawVcs};
4use turbo_tasks_fs::FileSystemPath;
5use turbopack_core::{reference_type::ReferenceType, source::Source};
6
7use super::{RuleCondition, match_mode::MatchMode};
8use crate::transition::Transition;
9
10#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, NonLocalValue)]
11pub struct TransitionRule {
12    condition: RuleCondition,
13    transition: ResolvedVc<Box<dyn Transition>>,
14    match_mode: MatchMode,
15}
16
17impl TransitionRule {
18    /// Creates a new transition rule. Will not match internal references.
19    pub fn new(condition: RuleCondition, transition: ResolvedVc<Box<dyn Transition>>) -> Self {
20        TransitionRule {
21            condition,
22            transition,
23            match_mode: MatchMode::NonInternal,
24        }
25    }
26
27    /// Creates a new transition rule. Will only match internal references.
28    pub fn new_internal(
29        condition: RuleCondition,
30        transition: ResolvedVc<Box<dyn Transition>>,
31    ) -> Self {
32        TransitionRule {
33            condition,
34            transition,
35            match_mode: MatchMode::Internal,
36        }
37    }
38
39    /// Creates a new transition rule. Will match all references.
40    pub fn new_all(condition: RuleCondition, transition: ResolvedVc<Box<dyn Transition>>) -> Self {
41        TransitionRule {
42            condition,
43            transition,
44            match_mode: MatchMode::All,
45        }
46    }
47
48    pub fn transition(&self) -> ResolvedVc<Box<dyn Transition>> {
49        self.transition
50    }
51
52    pub async fn matches(
53        &self,
54        source: ResolvedVc<Box<dyn Source>>,
55        path: &FileSystemPath,
56        reference_type: &ReferenceType,
57    ) -> Result<bool> {
58        Ok(self.match_mode.matches(reference_type)
59            && self.condition.matches(source, path, reference_type).await?)
60    }
61}