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#[allow(clippy::derive_partial_eq_without_eq)]
19#[derive(Debug, Clone, PartialEq, Default)]
20pub enum ExpressionHookResult {
21    /// Continue with normal analysis.
22    #[default]
23    Continue,
24    /// Skip normal analysis (expression type will be `mixed`).
25    Skip,
26    /// Skip normal analysis and use the provided type.
27    SkipWithType(TUnion),
28}
29
30impl ExpressionHookResult {
31    /// Returns true if this result indicates the hook wants to skip analysis.
32    #[inline]
33    #[must_use]
34    pub fn should_skip(&self) -> bool {
35        !matches!(self, ExpressionHookResult::Continue)
36    }
37
38    /// Returns the type to use if `SkipWithType`, otherwise `None`.
39    #[inline]
40    #[must_use]
41    pub fn take_type(self) -> Option<TUnion> {
42        match self {
43            ExpressionHookResult::SkipWithType(ty) => Some(ty),
44            _ => None,
45        }
46    }
47}