use crate::HookContext;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HookAction {
Continue(Box<HookContext>),
Skip(Value),
Abort {
reason: String,
},
Replace(Value),
}
impl HookAction {
#[must_use]
pub fn is_continue(&self) -> bool {
matches!(self, Self::Continue(_))
}
#[must_use]
pub fn is_skip(&self) -> bool {
matches!(self, Self::Skip(_))
}
#[must_use]
pub fn is_abort(&self) -> bool {
matches!(self, Self::Abort { .. })
}
#[must_use]
pub fn is_replace(&self) -> bool {
matches!(self, Self::Replace(_))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::HookPoint;
use orcs_types::{ChannelId, ComponentId, Principal};
use serde_json::json;
fn dummy_ctx() -> HookContext {
HookContext::new(
HookPoint::RequestPreDispatch,
ComponentId::builtin("test"),
ChannelId::new(),
Principal::System,
0,
json!(null),
)
}
#[test]
fn continue_variant() {
let action = HookAction::Continue(Box::new(dummy_ctx()));
assert!(action.is_continue());
assert!(!action.is_skip());
assert!(!action.is_abort());
assert!(!action.is_replace());
}
#[test]
fn skip_variant() {
let action = HookAction::Skip(json!({"skipped": true}));
assert!(action.is_skip());
assert!(!action.is_continue());
}
#[test]
fn abort_variant() {
let action = HookAction::Abort {
reason: "policy".into(),
};
assert!(action.is_abort());
assert!(!action.is_continue());
}
#[test]
fn replace_variant() {
let action = HookAction::Replace(json!({"new": "value"}));
assert!(action.is_replace());
assert!(!action.is_continue());
}
}