litellm_rs/utils/event/
types.rs1use std::fmt::Debug;
4use std::time::{SystemTime, UNIX_EPOCH};
5use tokio::sync::mpsc;
6use uuid::Uuid;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum EventType {
11 Created,
13 Updated,
15 Deleted,
17 Custom(u32),
19}
20
21impl EventType {
22 pub fn is_created(&self) -> bool {
24 matches!(self, EventType::Created)
25 }
26
27 pub fn is_updated(&self) -> bool {
29 matches!(self, EventType::Updated)
30 }
31
32 pub fn is_deleted(&self) -> bool {
34 matches!(self, EventType::Deleted)
35 }
36
37 pub fn is_custom(&self) -> bool {
39 matches!(self, EventType::Custom(_))
40 }
41
42 pub fn custom_code(&self) -> Option<u32> {
44 match self {
45 EventType::Custom(code) => Some(*code),
46 _ => None,
47 }
48 }
49}
50
51impl std::fmt::Display for EventType {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self {
54 EventType::Created => write!(f, "Created"),
55 EventType::Updated => write!(f, "Updated"),
56 EventType::Deleted => write!(f, "Deleted"),
57 EventType::Custom(code) => write!(f, "Custom({})", code),
58 }
59 }
60}
61
62#[derive(Debug, Clone)]
64pub struct Event<T>
65where
66 T: Clone + Send + Sync,
67{
68 pub id: String,
70 pub event_type: EventType,
72 pub data: T,
74 pub timestamp: u64,
76 pub source: Option<String>,
78 pub correlation_id: Option<String>,
80}
81
82impl<T> Event<T>
83where
84 T: Clone + Send + Sync,
85{
86 pub fn new(event_type: EventType, data: T) -> Self {
88 Self {
89 id: Uuid::new_v4().to_string(),
90 event_type,
91 data,
92 timestamp: current_timestamp_millis(),
93 source: None,
94 correlation_id: None,
95 }
96 }
97
98 pub fn created(data: T) -> Self {
100 Self::new(EventType::Created, data)
101 }
102
103 pub fn updated(data: T) -> Self {
105 Self::new(EventType::Updated, data)
106 }
107
108 pub fn deleted(data: T) -> Self {
110 Self::new(EventType::Deleted, data)
111 }
112
113 pub fn custom(code: u32, data: T) -> Self {
115 Self::new(EventType::Custom(code), data)
116 }
117
118 pub fn with_source(mut self, source: impl Into<String>) -> Self {
120 self.source = Some(source.into());
121 self
122 }
123
124 pub fn with_correlation_id(mut self, correlation_id: impl Into<String>) -> Self {
126 self.correlation_id = Some(correlation_id.into());
127 self
128 }
129
130 pub fn is_type(&self, event_type: EventType) -> bool {
132 self.event_type == event_type
133 }
134}
135
136#[async_trait::async_trait]
141pub trait Subscriber<T>: Send + Sync
142where
143 T: Clone + Send + Sync + 'static,
144{
145 async fn on_event(&self, event: Event<T>);
150
151 fn should_receive(&self, event: &Event<T>) -> bool {
156 let _ = event;
157 true
158 }
159}
160
161#[derive(Debug)]
165pub struct SubscriptionHandle {
166 pub id: String,
168 active: std::sync::atomic::AtomicBool,
170}
171
172impl SubscriptionHandle {
173 pub fn new() -> Self {
175 Self {
176 id: Uuid::new_v4().to_string(),
177 active: std::sync::atomic::AtomicBool::new(true),
178 }
179 }
180
181 pub fn is_active(&self) -> bool {
183 self.active.load(std::sync::atomic::Ordering::Relaxed)
184 }
185
186 pub fn cancel(&self) {
188 self.active
189 .store(false, std::sync::atomic::Ordering::Relaxed);
190 }
191}
192
193impl Default for SubscriptionHandle {
194 fn default() -> Self {
195 Self::new()
196 }
197}
198
199pub type EventReceiver<T> = mpsc::Receiver<Event<T>>;
201
202pub type EventSender<T> = mpsc::Sender<Event<T>>;
204
205fn current_timestamp_millis() -> u64 {
207 SystemTime::now()
208 .duration_since(UNIX_EPOCH)
209 .unwrap_or_default()
210 .as_millis() as u64
211}