dragonfly_plugin/event/
context.rs

1use crate::types;
2
3/// This enum is used internally by `dispatch_event` to
4/// determine what action to take after an event handler runs.
5#[doc(hidden)]
6pub enum EventResultUpdate {
7    /// Do nothing, let the default server behavior happen. just sends ack.
8    None,
9    /// Cancel the event, stopping default server behavior.
10    Cancelled,
11    /// Mutate the event, which is sent back to the server.
12    Mutated(types::event_result::Update),
13}
14
15/// A smart wrapper for a server event.
16///
17/// This struct provides read-only access to the event's data
18/// and methods to mutate or cancel it.
19pub struct EventContext<'a, T> {
20    pub data: &'a T,
21
22    event_id: &'a str,
23    result: EventResultUpdate,
24}
25
26impl<'a, T> EventContext<'a, T> {
27    #[doc(hidden)]
28    pub fn new(event_id: &'a str, data: &'a T) -> Self {
29        Self {
30            event_id,
31            data,
32            result: EventResultUpdate::None,
33        }
34    }
35
36    /// Consumes the context and returns the final result.
37    #[doc(hidden)]
38    pub fn into_result(self) -> (String, EventResultUpdate) {
39        (self.event_id.to_string(), self.result)
40    }
41
42    /// Cancels the event.
43    ///
44    /// The server's default handler will not run.
45    pub fn cancel(&mut self) {
46        self.result = EventResultUpdate::Cancelled;
47    }
48
49    /// Internal helper to set a mutation.
50    /// This is called by the auto-generated helper methods.
51    #[doc(hidden)]
52    pub fn set_mutation(&mut self, update: types::event_result::Update) {
53        self.result = EventResultUpdate::Mutated(update);
54    }
55}