module_registry/
lib.rs

1//! # Module Registry
2//!
3//! A dynamic module/plugin registry system with compile-time discovery and runtime instantiation.
4//!
5//! ## Features
6//!
7//! - **Compile-Time Discovery**: Uses `inventory` crate for automatic module registration
8//! - **Runtime Instantiation**: Create modules dynamically by name
9//! - **Type-Safe**: Generic factory functions with trait objects
10//! - **Thread-Safe**: Built on `RwLock` for concurrent access
11//! - **Flexible**: Support for any module type with custom configuration
12//!
13//! ## Quick Start
14//!
15//! ```rust
16//! use module_registry::{ModuleRegistry, Module};
17//! use anyhow::Result;
18//!
19//! // Define your module trait
20//! pub trait TextProcessor: Module {
21//!     fn process(&self, input: &str) -> Result<String>;
22//! }
23//!
24//! // Implement a module
25//! struct UpperCaseModule;
26//!
27//! impl Module for UpperCaseModule {
28//!     fn name(&self) -> &str {
29//!         "uppercase"
30//!     }
31//!     
32//!     fn module_type(&self) -> &str {
33//!         "text_processor"
34//!     }
35//! }
36//!
37//! impl TextProcessor for UpperCaseModule {
38//!     fn process(&self, input: &str) -> Result<String> {
39//!         Ok(input.to_uppercase())
40//!     }
41//! }
42//!
43//! # fn main() -> Result<()> {
44//! // Create registry
45//! let registry = ModuleRegistry::new();
46//!
47//! // Register module (factory returns Box<dyn Any + Send + Sync>)
48//! registry.register(
49//!     "uppercase",
50//!     "text_processor",
51//!     || Ok(Box::new(Box::new(UpperCaseModule) as Box<dyn TextProcessor>))
52//! );
53//!
54//! // Create module instance  
55//! let any_module = registry.create_any("uppercase")?;
56//! let module = any_module.downcast::<Box<dyn TextProcessor>>()
57//!     .map_err(|_| anyhow::anyhow!("Type mismatch"))?;
58//! assert_eq!(module.name(), "uppercase");
59//! # Ok(())
60//! # }
61//! ```
62
63pub mod constants;
64pub mod macros;
65pub mod registry;
66pub mod security;
67pub mod types;
68
69// Re-export main types and functions
70pub use constants::*;
71pub use macros::*;
72pub use registry::*;
73pub use security::*;
74pub use types::*;
75
76// Re-export the main ModuleRegistry struct
77pub use registry::ModuleRegistry;
78
79// Re-export inventory collection
80inventory::collect!(ModuleRegistration);