Skip to main content

s3rm_rs/types/
event_callback.rs

1//! Event callback trait and event data types for deletion pipeline events.
2//!
3//! Adapted from s3sync's `types/event_callback.rs`.
4//! Provides structured event data for monitoring deletion operations.
5
6use async_trait::async_trait;
7use bitflags::bitflags;
8
9bitflags! {
10    /// Event type flags for filtering which events a callback receives.
11    ///
12    /// Adapted from s3sync's EventType bitflags, with sync-specific events
13    /// replaced by deletion-specific events.
14    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
15    pub struct EventType: u64 {
16        const UNDEFINED = 0u64;
17        const PIPELINE_START = 1u64 << 1;
18        const PIPELINE_END = 1u64 << 2;
19        const DELETE_COMPLETE = 1u64 << 3;
20        const DELETE_FAILED = 1u64 << 4;
21        const DELETE_FILTERED = 1u64 << 5;
22        const PIPELINE_ERROR = 1u64 << 6;
23        const DELETE_CANCEL = 1u64 << 7;
24        const STATS_REPORT = 1u64 << 8;
25        const ALL_EVENTS = !0;
26    }
27}
28
29/// Structured event data passed to event callbacks.
30///
31/// Adapted from s3sync's EventData. Simplified for deletion operations
32/// by removing sync-specific fields (upload_id, checksums, etc.) and
33/// adding deletion-specific fields.
34#[derive(Default, Debug, Clone)]
35pub struct EventData {
36    pub event_type: EventType,
37    pub dry_run: bool,
38    pub key: Option<String>,
39    pub version_id: Option<String>,
40    pub size: Option<u64>,
41    pub last_modified: Option<String>,
42    pub e_tag: Option<String>,
43    pub error_message: Option<String>,
44    pub message: Option<String>,
45
46    // Statistics fields (populated in STATS_REPORT events)
47    pub stats_deleted_objects: Option<u64>,
48    pub stats_deleted_bytes: Option<u64>,
49    pub stats_failed_objects: Option<u64>,
50    pub stats_skipped_objects: Option<u64>,
51    pub stats_error_count: Option<u64>,
52    pub stats_duration_sec: Option<f64>,
53    pub stats_objects_per_sec: Option<f64>,
54}
55
56impl EventData {
57    pub fn new(event_type: EventType) -> Self {
58        Self {
59            event_type,
60            ..Default::default()
61        }
62    }
63}
64
65/// Trait for event callbacks that receive pipeline events.
66///
67/// Implementations receive `EventData` for monitoring, logging, or
68/// custom processing of deletion events.
69///
70/// # Notes
71///
72/// - Callbacks are called serially for each event
73/// - Callbacks should return promptly to avoid blocking the pipeline
74/// - Both Lua scripts and Rust closures can implement this trait
75#[async_trait]
76pub trait EventCallback: Send {
77    async fn on_event(&mut self, event_data: EventData);
78}