<h1 align="center">
<img width="108px" height="auto" src="https://raw.githubusercontent.com/jamesgober/jamesgober/main/media/icons/hexagon-3.svg" alt="Triple Hexagon">
<br>
<strong>Mod Events</strong>
<sup><br><sup>QUICK START GUIDE</sup></sup>
</h1>
This guide will get you up and running with mod-events in under 5 minutes.
## Installation
Add mod-events to your `Cargo.toml`:
```toml
[dependencies]
mod-events = "0.2.1"
# For async support (default)
mod-events = { version = "0.2.1", features = ["async"] }
# Sync-only build
mod-events = { version = "0.2.1", default-features = false }
```
MSRV: Rust 1.81.
## Basic Usage
### 1. Define Your Events
Events are just structs that implement the `Event` trait:
```rust
use mod_events::Event;
#[derive(Debug, Clone)]
struct UserRegistered {
user_id: u64,
email: String,
timestamp: u64,
}
impl Event for UserRegistered {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
```
### 2. Create a Dispatcher
```rust
use mod_events::EventDispatcher;
let dispatcher = EventDispatcher::new();
```
### 3. Subscribe to Events
```rust
// Simple subscription
});
// With error handling. The closure returns `Result<(), ListenerError>`;
// `&str`, `String`, and `Box<dyn Error + Send + Sync>` all convert into
// `ListenerError` via `Into`, so `Err("…".into())` works as-is.
return Err("Email cannot be empty".into());
}
println!("Processing user: {}", event.email);
Ok(())
});
```
### 4. Dispatch Events
```rust
// Fire and forget
dispatcher.emit(UserRegistered {
user_id: 123,
email: "alice@example.com".to_string(),
timestamp: 1234567890,
});
// Check results
let result = dispatcher.dispatch(UserRegistered {
user_id: 456,
email: "bob@example.com".to_string(),
timestamp: 1234567891,
});
if result.all_succeeded() {
println!("All handlers succeeded!");
}
```
## Advanced Features
### Priority System
```rust
use mod_events::Priority;
// High priority runs first
Ok(())
}, Priority::High);
// Normal priority runs second
});
```
### Middleware
```rust
// Add logging middleware
dispatcher.add_middleware(|event: &dyn Event| {
println!("Event: {}", event.event_name());
true // Allow event to continue
});
// Add filtering middleware
dispatcher.add_middleware(|event: &dyn Event| {
// Block events during maintenance
!is_maintenance_mode()
});
```
### Async Support
```rust
// Async event handler
dispatcher.subscribe_async(|event: &UserRegistered| async {
send_welcome_email(&event.email).await?;
Ok(())
});
// Dispatch async
let result = dispatcher.dispatch_async(user_event).await;
```
<br>
## Next Steps
- Read the [API Reference](api-reference.md)
- Check out more [Examples](examples.md)
- Learn [Best Practices](best-practices.md)
- Review [Performance Guide](performance.md)
- See the [Migration Guide](migration.md)