use global_registry::{register, TypeRegistry};
trait Plugin: Send + Sync {
fn name(&self) -> &str;
fn execute(&self, input: &str) -> String;
fn version(&self) -> &str { "1.0" }
}
#[derive(Debug, Clone)]
struct EmailPlugin {
smtp_server: String,
}
impl EmailPlugin {
fn new(smtp_server: String) -> Self {
Self { smtp_server }
}
}
impl Plugin for EmailPlugin {
fn name(&self) -> &str {
"Email Plugin"
}
fn execute(&self, input: &str) -> String {
format!("Sending email via {}: {}", self.smtp_server, input)
}
fn version(&self) -> &str {
"2.1.0"
}
}
#[derive(Debug, Clone)]
struct SmsPlugin {
api_key: String,
}
impl SmsPlugin {
fn new(api_key: String) -> Self {
Self { api_key }
}
}
impl Plugin for SmsPlugin {
fn name(&self) -> &str {
"SMS Plugin"
}
fn execute(&self, input: &str) -> String {
format!("Sending SMS (key: {}): {}",
self.api_key.chars().take(4).collect::<String>() + "...",
input)
}
}
#[derive(Debug, Clone)]
struct LogPlugin;
impl Plugin for LogPlugin {
fn name(&self) -> &str {
"Log Plugin"
}
fn execute(&self, input: &str) -> String {
let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S");
format!("[{}] LOG: {}", timestamp, input)
}
fn version(&self) -> &str {
"3.0.1"
}
}
struct PluginManager;
impl PluginManager {
fn register_email_plugin(smtp_server: &str) {
let plugin = EmailPlugin::new(smtp_server.to_string());
register!(EmailPlugin, plugin)
.expect("Failed to register email plugin");
}
fn register_sms_plugin(api_key: &str) {
let plugin = SmsPlugin::new(api_key.to_string());
register!(SmsPlugin, plugin)
.expect("Failed to register SMS plugin");
}
fn register_log_plugin() {
let plugin = LogPlugin;
register!(LogPlugin, plugin)
.expect("Failed to register log plugin");
}
fn execute_email_plugin(input: &str) -> Option<String> {
TypeRegistry::global()
.get_cloned::<EmailPlugin>()
.map(|plugin| plugin.execute(input))
.ok()
}
fn execute_sms_plugin(input: &str) -> Option<String> {
TypeRegistry::global()
.get_cloned::<SmsPlugin>()
.map(|plugin| plugin.execute(input))
.ok()
}
fn execute_log_plugin(input: &str) -> Option<String> {
TypeRegistry::global()
.get_cloned::<LogPlugin>()
.map(|plugin| plugin.execute(input))
.ok()
}
fn list_all_plugins() {
println!("=== Registered Plugins ===");
let registry = TypeRegistry::global();
if registry.is_registered::<EmailPlugin>() {
if let Ok(plugin) = registry.get_cloned::<EmailPlugin>() {
println!("Found plugin: {} v{}", plugin.name(), plugin.version());
}
}
if registry.is_registered::<SmsPlugin>() {
if let Ok(plugin) = registry.get_cloned::<SmsPlugin>() {
println!("Found plugin: {} v{}", plugin.name(), plugin.version());
}
}
if registry.is_registered::<LogPlugin>() {
if let Ok(plugin) = registry.get_cloned::<LogPlugin>() {
println!("Found plugin: {} v{}", plugin.name(), plugin.version());
}
}
println!("Total registered types: {}", registry.len());
}
}
fn main() {
println!("=== Plugin System Example ===\n");
PluginManager::register_email_plugin("smtp.example.com");
PluginManager::register_sms_plugin("secret_api_key_123");
PluginManager::register_log_plugin();
let test_message = "Hello, World!";
if let Some(result) = PluginManager::execute_email_plugin(test_message) {
println!("Email: {}", result);
}
if let Some(result) = PluginManager::execute_sms_plugin(test_message) {
println!("SMS: {}", result);
}
if let Some(result) = PluginManager::execute_log_plugin(test_message) {
println!("Log: {}", result);
}
PluginManager::list_all_plugins();
let registry = TypeRegistry::global();
registry.clear().expect("Failed to clear registry");
println!("\nRegistry cleared. Total types: {}", registry.len());
}
mod chrono {
pub struct Utc;
impl Utc {
pub fn now() -> MockDateTime {
MockDateTime
}
}
pub struct MockDateTime;
impl MockDateTime {
pub fn format(&self, _format: &str) -> String {
"2024-01-15 14:30:25".to_string()
}
}
}