use rkyv::{Archive, Deserialize, Serialize};
#[derive(Archive, Serialize, Deserialize, Debug, Clone)]
#[rkyv(derive(Debug))]
pub enum Notification {
EventApplied {
issue_id: String,
event_id: String,
ts_unix_ms: u64,
},
WalSynced {
wal_head: String,
remote: String,
},
LockChanged {
resource: String,
owner: String,
expires_unix_ms: u64,
},
SnapshotCreated {
snapshot_ref: String,
},
WorkerStarted {
repo_root: String,
actor_id: String,
},
WorkerStopped {
repo_root: String,
actor_id: String,
reason: String,
},
}
impl Notification {
pub fn notification_type(&self) -> &'static str {
match self {
Notification::EventApplied { .. } => "EventApplied",
Notification::WalSynced { .. } => "WalSynced",
Notification::LockChanged { .. } => "LockChanged",
Notification::SnapshotCreated { .. } => "SnapshotCreated",
Notification::WorkerStarted { .. } => "WorkerStarted",
Notification::WorkerStopped { .. } => "WorkerStopped",
}
}
pub fn event_applied(issue_id: String, event_id: String, ts_unix_ms: u64) -> Self {
Notification::EventApplied {
issue_id,
event_id,
ts_unix_ms,
}
}
pub fn wal_synced(wal_head: String, remote: String) -> Self {
Notification::WalSynced { wal_head, remote }
}
pub fn lock_changed(resource: String, owner: String, expires_unix_ms: u64) -> Self {
Notification::LockChanged {
resource,
owner,
expires_unix_ms,
}
}
pub fn snapshot_created(snapshot_ref: String) -> Self {
Notification::SnapshotCreated { snapshot_ref }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_notification_type() {
let n = Notification::event_applied("issue1".to_string(), "event1".to_string(), 1000);
assert_eq!(n.notification_type(), "EventApplied");
let n = Notification::wal_synced("abc123".to_string(), "origin".to_string());
assert_eq!(n.notification_type(), "WalSynced");
}
#[test]
fn test_rkyv_roundtrip() {
let notification = Notification::EventApplied {
issue_id: "issue123".to_string(),
event_id: "event456".to_string(),
ts_unix_ms: 1700000000000,
};
let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(¬ification).unwrap();
let archived = rkyv::access::<ArchivedNotification, rkyv::rancor::Error>(&bytes).unwrap();
match archived {
ArchivedNotification::EventApplied {
issue_id,
event_id,
ts_unix_ms,
} => {
assert_eq!(issue_id.as_str(), "issue123");
assert_eq!(event_id.as_str(), "event456");
assert_eq!(*ts_unix_ms, 1700000000000);
}
_ => panic!("Wrong variant"),
}
}
}