Skip to main content

mod_events/
priority.rs

1//! Priority system for event listeners
2
3/// Priority levels for event listeners
4///
5/// Listeners with higher priority are executed first.
6/// This allows for controlling the execution order of event handlers.
7///
8/// # Example
9///
10/// ```rust
11/// use mod_events::{EventDispatcher, Priority, Event};
12///
13/// #[derive(Debug, Clone)]
14/// struct MyEvent {
15///     message: String,
16/// }
17///
18/// impl Event for MyEvent {
19///     fn as_any(&self) -> &dyn std::any::Any {
20///         self
21///     }
22/// }
23///
24/// let dispatcher = EventDispatcher::new();
25///
26/// // This will execute first
27/// dispatcher.subscribe_with_priority(|event: &MyEvent| {
28///     println!("High priority handler");
29///     Ok(())
30/// }, Priority::High);
31///
32/// // This will execute second
33/// dispatcher.subscribe_with_priority(|event: &MyEvent| {
34///     println!("Normal priority handler");
35///     Ok(())
36/// }, Priority::Normal);
37/// ```
38#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
39#[non_exhaustive]
40pub enum Priority {
41    /// Lowest priority (0)
42    Lowest = 0,
43    /// Low priority (25)
44    Low = 25,
45    /// Normal priority (50) - default
46    #[default]
47    Normal = 50,
48    /// High priority (75)
49    High = 75,
50    /// Highest priority (100)
51    Highest = 100,
52    /// Critical priority (125) - use sparingly
53    Critical = 125,
54}
55
56impl Priority {
57    /// Get all priority levels in order
58    pub fn all() -> &'static [Priority] {
59        &[
60            Priority::Critical,
61            Priority::Highest,
62            Priority::High,
63            Priority::Normal,
64            Priority::Low,
65            Priority::Lowest,
66        ]
67    }
68}