Skip to main content

mago_analyzer/plugin/hook/
action.rs

1//! Hook action and result types.
2
3use mago_codex::ttype::union::TUnion;
4
5use crate::plugin::error::PluginError;
6
7pub type HookResult<T> = Result<T, PluginError>;
8
9/// Action to take after a hook runs (for statement hooks).
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum HookAction {
12    #[default]
13    Continue,
14    Skip,
15}
16
17/// Result type for expression hooks that can provide a custom type when skipping.
18#[derive(Debug, Clone, PartialEq, Default)]
19pub enum ExpressionHookResult {
20    /// Continue with normal analysis.
21    #[default]
22    Continue,
23    /// Skip normal analysis (expression type will be `mixed`).
24    Skip,
25    /// Skip normal analysis and use the provided type.
26    SkipWithType(TUnion),
27}
28
29impl ExpressionHookResult {
30    /// Returns true if this result indicates the hook wants to skip analysis.
31    #[inline]
32    #[must_use]
33    pub fn should_skip(&self) -> bool {
34        !matches!(self, ExpressionHookResult::Continue)
35    }
36
37    /// Returns the type to use if `SkipWithType`, otherwise `None`.
38    #[inline]
39    #[must_use]
40    pub fn take_type(self) -> Option<TUnion> {
41        match self {
42            ExpressionHookResult::SkipWithType(ty) => Some(ty),
43            _ => None,
44        }
45    }
46}