use super::config::InventoryConfig;
use super::hook::{DefaultInventoryHook, InventoryHook};
use super::service::InventoryService;
use super::state::InventoryState;
use super::system::InventorySystem;
use crate::Plugin;
use std::sync::Arc;
#[derive(Plugin)]
#[plugin(name = "issun:inventory")]
pub struct InventoryPlugin {
#[plugin(skip)]
hook: Arc<dyn InventoryHook>,
#[resource]
config: InventoryConfig,
#[state]
state: InventoryState,
#[service]
service: InventoryService,
#[system]
system: InventorySystem,
}
impl InventoryPlugin {
pub fn new() -> Self {
let hook = Arc::new(DefaultInventoryHook);
Self {
hook: hook.clone(),
config: InventoryConfig::default(),
state: InventoryState::new(),
service: InventoryService::new(),
system: InventorySystem::new(hook),
}
}
pub fn with_hook<H: InventoryHook + 'static>(mut self, hook: H) -> Self {
let hook = Arc::new(hook);
self.hook = hook.clone();
self.system = InventorySystem::new(hook);
self
}
pub fn with_config(mut self, config: InventoryConfig) -> Self {
self.config = config;
self
}
}
impl Default for InventoryPlugin {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plugin::Plugin;
#[test]
fn test_plugin_creation() {
let plugin = InventoryPlugin::new();
assert_eq!(plugin.name(), "issun:inventory");
}
#[test]
fn test_plugin_with_custom_hook() {
struct CustomHook;
#[async_trait::async_trait]
impl InventoryHook for CustomHook {}
let plugin = InventoryPlugin::new().with_hook(CustomHook);
assert_eq!(plugin.name(), "issun:inventory");
}
#[test]
fn test_plugin_with_custom_config() {
let config = InventoryConfig {
enabled: true,
default_capacity: 20,
allow_stacking: false,
max_stack_size: 1,
};
let plugin = InventoryPlugin::new().with_config(config);
assert_eq!(plugin.name(), "issun:inventory");
}
}