Skip to main content

bonds_core/
events.rs

1use crate::bond::Bond;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4
5/// High-level event categories for bond lifecycle changes.
6#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
7pub enum BondEventKind {
8    Created,
9    Updated,
10    Deleted,
11    MetadataUpdated,
12    BrokenDetected,
13}
14
15/// Why a bond was considered broken during health scanning.
16#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
17pub enum BondBrokenReason {
18    /// Target path is missing or the symlink points to a missing location.
19    MissingTarget,
20    /// Target exists but is not a symlink.
21    TargetNotSymlink,
22}
23
24/// Detailed payload for each event kind.
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
26pub enum BondEventPayload {
27    Created {
28        bond: Bond,
29    },
30    Updated {
31        before: Bond,
32        after: Bond,
33    },
34    Deleted {
35        bond: Bond,
36    },
37    MetadataUpdated {
38        before: Bond,
39        after: Bond,
40    },
41    BrokenDetected {
42        bond: Bond,
43        reason: BondBrokenReason,
44    },
45}
46
47/// Event envelope with timestamp and typed payload.
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
49pub struct BondEvent {
50    pub occurred_at: DateTime<Utc>,
51    pub payload: BondEventPayload,
52}
53
54impl BondEvent {
55    /// Convenience accessor so callers can switch on kind quickly.
56    pub fn kind(&self) -> BondEventKind {
57        match self.payload {
58            BondEventPayload::Created { .. } => BondEventKind::Created,
59            BondEventPayload::Updated { .. } => BondEventKind::Updated,
60            BondEventPayload::Deleted { .. } => BondEventKind::Deleted,
61            BondEventPayload::MetadataUpdated { .. } => BondEventKind::MetadataUpdated,
62            BondEventPayload::BrokenDetected { .. } => BondEventKind::BrokenDetected,
63        }
64    }
65}
66
67/// Hook trait for consumers that want lifecycle notifications.
68pub trait BondEventHook: Send + Sync {
69    fn on_event(&self, event: &BondEvent);
70}
71
72/// Allow closures/functions to be registered directly as hooks.
73impl<F> BondEventHook for F
74where
75    F: Fn(&BondEvent) + Send + Sync,
76{
77    fn on_event(&self, event: &BondEvent) {
78        (self)(event);
79    }
80}