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 client_generator;
87pub mod datasource;
88pub mod error;
89pub mod manifest;
90pub mod plugins;
91pub mod response;
92pub mod runtime;
93pub mod template;
94pub mod types;
95
96// Re-export the async trait
97pub mod async_trait;
98pub use async_trait::TokenResolver;
99
100// Re-export types
101pub use auth::*;
102pub use client_generator::{
103 ClientGenerationResult, ClientGeneratorConfig, ClientGeneratorPlugin,
104 ClientGeneratorPluginConfig, GeneratedFile, GenerationMetadata, OpenApiSpec,
105};
106pub use datasource::{
107 DataConnection, DataQuery, DataResult, DataSourcePlugin, DataSourcePluginConfig,
108};
109pub use plugins::{ReactClientGenerator, VueClientGenerator};
110pub use response::{
111 ResponseData, ResponseModifierConfig, ResponseModifierPlugin, ResponsePlugin,
112 ResponsePluginConfig, ResponseRequest,
113};
114pub use template::*;
115pub use types::*;
116
117// Re-export helper modules with qualified names to avoid ambiguity
118pub use datasource::helpers as datasource_helpers;
119pub use response::helpers as response_helpers;
120
121// Re-export common types for backwards compatibility
122pub use types::{
123 PluginAuthor, PluginHealth, PluginId, PluginInfo, PluginManifest, PluginMetadata, PluginState,
124 PluginVersion,
125};
126
127// Additional utility traits (commented out as we're using the async trait)
128// pub trait SyncTokenResolver {
129// /// Check if this resolver can handle a given token
130// fn can_resolve(&self, token: &str) -> bool;
131//
132// /// Resolve a token to its value synchronously
133// fn resolve_token(&self, token: &str, context: &ResolutionContext) -> Result<String, PluginError>;
134//
135// /// Get plugin metadata
136// fn get_metadata(&self) -> PluginMetadata;
137//
138// /// Validate plugin configuration
139// fn validate(&self) -> Result<(), PluginError> {
140// Ok(())
141// }
142// }
143
144// Re-export additional types for backwards compatibility
145pub use types::{PluginError, PluginInstance, RequestMetadata, ResolutionContext, Result};
146
147#[cfg(test)]
148mod tests {
149 use super::*;
150
151 #[test]
152 fn test_plugin_id() {
153 let id = PluginId::new("test-plugin");
154 assert_eq!(id.as_str(), "test-plugin");
155 }
156
157 #[test]
158 fn test_plugin_version() {
159 let version = PluginVersion::new(1, 2, 3);
160 assert_eq!(version.to_string(), "1.2.3");
161 }
162
163 #[test]
164 fn test_plugin_info() {
165 let id = PluginId::new("example");
166 let version = PluginVersion::new(1, 0, 0);
167 let author = PluginAuthor {
168 name: "Author".to_string(),
169 email: Some("author@example.com".to_string()),
170 };
171 let info = PluginInfo {
172 id: id.clone(),
173 version: version.clone(),
174 name: "Example Plugin".to_string(),
175 description: "Description".to_string(),
176 author: author.clone(),
177 };
178
179 assert_eq!(info.id.as_str(), "example");
180 assert_eq!(info.name, "Example Plugin");
181 assert_eq!(info.description, "Description");
182 assert_eq!(info.author.name, "Author");
183 assert_eq!(info.author.email, Some("author@example.com".to_string()));
184 }
185
186 #[test]
187 fn test_resolution_context() {
188 let context = ResolutionContext::new();
189 assert!(!context.environment.is_empty());
190 assert!(context.request_context.is_none());
191 }
192
193 #[test]
194 fn test_request_metadata() {
195 let request =
196 RequestMetadata::new("GET", "/api/users").with_header("Accept", "application/json");
197
198 assert_eq!(request.method, "GET");
199 assert_eq!(request.path, "/api/users");
200 assert_eq!(request.headers.get("Accept"), Some(&"application/json".to_string()));
201 }
202}
203
204// Include client generator tests
205#[cfg(test)]
206mod client_generator_tests;