use super::config::LogisticsConfig;
use super::hook::{DefaultLogisticsHook, LogisticsHook};
use super::service::LogisticsService;
use super::state::LogisticsState;
use super::system::LogisticsSystem;
use crate::Plugin;
use std::sync::Arc;
#[derive(Plugin)]
#[plugin(name = "issun:logistics")]
pub struct LogisticsPlugin {
#[plugin(skip)]
hook: Arc<dyn LogisticsHook>,
#[plugin(resource)]
config: LogisticsConfig,
#[plugin(runtime_state)]
state: LogisticsState,
#[plugin(service)]
service: LogisticsService,
#[plugin(system)]
system: LogisticsSystem,
}
impl LogisticsPlugin {
pub fn new() -> Self {
let hook = Arc::new(DefaultLogisticsHook);
Self {
hook: hook.clone(),
config: LogisticsConfig::default(),
state: LogisticsState::new(),
service: LogisticsService,
system: LogisticsSystem::new(hook),
}
}
pub fn with_hook<H: LogisticsHook + 'static>(mut self, hook: H) -> Self {
let hook = Arc::new(hook);
self.hook = hook.clone();
self.system = LogisticsSystem::new(hook);
self
}
pub fn with_config(mut self, config: LogisticsConfig) -> Self {
self.config = config;
self
}
}
impl Default for LogisticsPlugin {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plugin::Plugin;
#[test]
fn test_plugin_creation() {
let plugin = LogisticsPlugin::new();
assert_eq!(plugin.name(), "issun:logistics");
}
#[test]
fn test_plugin_with_custom_hook() {
struct CustomHook;
#[async_trait::async_trait]
impl LogisticsHook for CustomHook {}
let plugin = LogisticsPlugin::new().with_hook(CustomHook);
assert_eq!(plugin.name(), "issun:logistics");
}
#[test]
fn test_plugin_with_custom_config() {
let config = LogisticsConfig::default()
.with_throughput_multiplier(2.0)
.with_max_routes_per_update(500);
let plugin = LogisticsPlugin::new().with_config(config);
assert_eq!(plugin.name(), "issun:logistics");
}
}