mockforge_plugin_core/
lib.rs

1//! # MockForge Plugin Core
2//!
3//! Core traits, types, and runtime interfaces for the MockForge plugin system.
4//!
5//! This crate provides the foundational abstractions for building MockForge plugins,
6//! including custom authentication handlers, data sources, response generators, and
7//! template token resolvers.
8//!
9//! ## Overview
10//!
11//! MockForge uses a WebAssembly-based plugin system that allows developers to extend
12//! its functionality without modifying the core application. Plugins are sandboxed for
13//! security and can be loaded/unloaded at runtime.
14//!
15//! ## Plugin Types
16//!
17//! The plugin system supports several categories of plugins:
18//!
19//! - **Authentication Plugins**: Custom authentication and authorization logic
20//! - **Data Source Plugins**: Connect to external data sources for realistic test data
21//! - **Response Plugins**: Generate custom responses based on request data
22//! - **Template Plugins**: Custom token resolvers for the template system
23//!
24//! ## Quick Start
25//!
26//! To create a plugin, implement one or more of the plugin traits:
27//!
28//! ```rust,ignore
29//! use mockforge_plugin_core::{TokenResolver, ResolutionContext, PluginError};
30//!
31//! pub struct MyPlugin;
32//!
33//! #[async_trait::async_trait]
34//! impl TokenResolver for MyPlugin {
35//!     async fn can_resolve(&self, token: &str) -> bool {
36//!         token.starts_with("my_")
37//!     }
38//!
39//!     async fn resolve_token(
40//!         &self,
41//!         token: &str,
42//!         context: &ResolutionContext,
43//!     ) -> Result<String, PluginError> {
44//!         // Custom resolution logic
45//!         Ok(format!("resolved: {}", token))
46//!     }
47//!
48//!     async fn get_metadata(&self) -> PluginMetadata {
49//!         PluginMetadata::new("My custom plugin")
50//!             .with_capability("token_resolver")
51//!             .with_prefix("my_")
52//!     }
53//! }
54//! ```
55//!
56//! ## Key Types
57//!
58//! - [`PluginId`]: Unique identifier for plugins
59//! - [`PluginVersion`]: Semantic version information
60//! - [`PluginManifest`]: Plugin metadata and dependencies
61//! - [`PluginError`]: Common error types
62//! - [`ResolutionContext`]: Context for token resolution
63//!
64//! ## Features
65//!
66//! - Type-safe plugin interfaces
67//! - Comprehensive error handling
68//! - Built-in validation and health checks
69//! - Async/await support
70//! - Security sandboxing via WebAssembly
71//!
72//! ## For Plugin Developers
73//!
74//! For a more convenient development experience, consider using the
75//! [`mockforge-plugin-sdk`](https://docs.rs/mockforge-plugin-sdk) crate, which provides
76//! helper macros, testing utilities, and additional conveniences.
77//!
78//! ## Documentation
79//!
80//! - [Plugin Development Guide](https://docs.mockforge.dev/plugins)
81//! - [API Reference](https://docs.rs/mockforge-plugin-core)
82//! - [Example Plugins](https://github.com/SaaSy-Solutions/mockforge/tree/main/examples/plugins)
83
84// Public modules
85pub mod auth;
86pub mod datasource;
87pub mod error;
88pub mod manifest;
89pub mod response;
90pub mod runtime;
91pub mod template;
92pub mod types;
93
94// Re-export the async trait
95pub mod async_trait;
96pub use async_trait::TokenResolver;
97
98// Re-export types
99pub use auth::*;
100pub use datasource::{
101    DataConnection, DataQuery, DataResult, DataSourcePlugin, DataSourcePluginConfig,
102};
103pub use response::{
104    ResponseData, ResponseModifierConfig, ResponseModifierPlugin, ResponsePlugin,
105    ResponsePluginConfig, ResponseRequest,
106};
107pub use template::*;
108pub use types::*;
109
110// Re-export helper modules with qualified names to avoid ambiguity
111pub use datasource::helpers as datasource_helpers;
112pub use response::helpers as response_helpers;
113
114// Re-export common types for backwards compatibility
115pub use types::{
116    PluginAuthor, PluginHealth, PluginId, PluginInfo, PluginManifest, PluginMetadata, PluginState,
117    PluginVersion,
118};
119
120// Additional utility traits (commented out as we're using the async trait)
121// pub trait SyncTokenResolver {
122//     /// Check if this resolver can handle a given token
123//     fn can_resolve(&self, token: &str) -> bool;
124//
125//     /// Resolve a token to its value synchronously
126//     fn resolve_token(&self, token: &str, context: &ResolutionContext) -> Result<String, PluginError>;
127//
128//     /// Get plugin metadata
129//     fn get_metadata(&self) -> PluginMetadata;
130//
131//     /// Validate plugin configuration
132//     fn validate(&self) -> Result<(), PluginError> {
133//         Ok(())
134//     }
135// }
136
137// Re-export additional types for backwards compatibility
138pub use types::{PluginError, PluginInstance, RequestMetadata, ResolutionContext, Result};
139
140#[cfg(test)]
141mod tests {
142    use super::*;
143
144    #[test]
145    fn test_plugin_id() {
146        let id = PluginId::new("test-plugin");
147        assert_eq!(id.as_str(), "test-plugin");
148    }
149
150    #[test]
151    fn test_plugin_version() {
152        let version = PluginVersion::new(1, 2, 3);
153        assert_eq!(version.to_string(), "1.2.3");
154    }
155
156    #[test]
157    fn test_plugin_info() {
158        let id = PluginId::new("example");
159        let version = PluginVersion::new(1, 0, 0);
160        let author = PluginAuthor {
161            name: "Author".to_string(),
162            email: Some("author@example.com".to_string()),
163        };
164        let info = PluginInfo {
165            id: id.clone(),
166            version: version.clone(),
167            name: "Example Plugin".to_string(),
168            description: "Description".to_string(),
169            author: author.clone(),
170        };
171
172        assert_eq!(info.id.as_str(), "example");
173        assert_eq!(info.name, "Example Plugin");
174        assert_eq!(info.description, "Description");
175        assert_eq!(info.author.name, "Author");
176        assert_eq!(info.author.email, Some("author@example.com".to_string()));
177    }
178
179    #[test]
180    fn test_resolution_context() {
181        let context = ResolutionContext::new();
182        assert!(!context.environment.is_empty());
183        assert!(context.request_context.is_none());
184    }
185
186    #[test]
187    fn test_request_metadata() {
188        let request =
189            RequestMetadata::new("GET", "/api/users").with_header("Accept", "application/json");
190
191        assert_eq!(request.method, "GET");
192        assert_eq!(request.path, "/api/users");
193        assert_eq!(request.headers.get("Accept"), Some(&"application/json".to_string()));
194    }
195}