waterui_core/
plugin.rs

1//! Provides the `Plugin` trait for extending application functionality.
2//!
3//! This module defines the core plugin system that allows for modular, extensible
4//! application architectures. Plugins can be dynamically installed into and removed
5//! from an `Environment`, enabling a flexible component-based design.
6//!
7//! The plugin system supports:
8//! - Dynamic installation and removal of components
9//! - Separation of concerns through modular design
10//! - Extension of application functionality without modifying core code
11//!
12//! # Usage
13//!
14//! Plugins are typically implemented as standalone structs that implement the `Plugin` trait.
15//! Once implemented, they can be installed into an `Environment` to extend its capabilities.
16
17use crate::Environment;
18
19/// The `Plugin` trait defines the interface for components that can be installed into
20/// and removed from an `Environment`.
21///
22/// # Examples
23///
24/// ```
25/// use waterui_core::{plugin::Plugin, Environment};
26///
27/// struct MyPlugin;
28///
29/// impl Plugin for MyPlugin {
30///     // Plugins don't require any implementation-specific methods by default,
31///     // but you can override the `install` and `uninstall` methods if your plugin
32///     // needs custom installation or removal behavior.
33///     //
34///     // For example, a plugin might:
35///     // - Register event handlers
36///     // - Initialize resources
37///     // - Set up configurations
38///     // - Connect to external services
39///     //
40///     // The default implementation simply stores/removes the plugin
41///     // instance in the environment.
42/// }
43///
44/// let mut env = Environment::new();
45/// MyPlugin.install(&mut env);
46/// ```
47pub trait Plugin: Sized + 'static {
48    /// Installs this plugin into the provided environment.
49    ///
50    /// This method adds the plugin instance to the environment's storage,
51    /// making it available for later retrieval.
52    ///
53    /// # Arguments
54    ///
55    /// * `env` - A mutable reference to the environment
56    fn install(self, env: &mut Environment) {
57        env.insert(self);
58    }
59
60    /// Removes this plugin from the provided environment.
61    ///
62    /// # Arguments
63    ///
64    /// * `env` - A mutable reference to the environment
65    fn uninstall(self, env: &mut Environment) {
66        env.remove::<Self>();
67    }
68}