mockforge-plugin-core 0.3.116

Core plugin interfaces and types for MockForge extensible architecture
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Template plugin interface
//!
//! This module defines the TemplatePlugin trait and related types for implementing
//! custom template functions and data generators in MockForge. Template plugins
//! extend the templating system with custom functions, filters, and data sources.

use crate::{PluginCapabilities, PluginContext, PluginResult, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Template plugin trait
///
/// Implement this trait to create custom template functions and data generators.
/// Template plugins are called during template expansion to provide custom
/// functions, filters, and data sources.
#[async_trait::async_trait]
pub trait TemplatePlugin: Send + Sync {
    /// Get plugin capabilities (permissions and limits)
    fn capabilities(&self) -> PluginCapabilities;

    /// Initialize the plugin with configuration
    async fn initialize(&self, config: &TemplatePluginConfig) -> Result<()>;

    /// Register template functions
    ///
    /// This method is called during plugin initialization to register custom
    /// template functions. The plugin should return a map of function names
    /// to function metadata.
    ///
    /// # Arguments
    /// * `context` - Plugin execution context
    /// * `config` - Plugin configuration
    ///
    /// # Returns
    /// Map of function names to function metadata
    async fn register_functions(
        &self,
        context: &PluginContext,
        config: &TemplatePluginConfig,
    ) -> Result<PluginResult<HashMap<String, TemplateFunction>>>;

    /// Execute a template function
    ///
    /// This method is called when a registered template function is invoked
    /// during template expansion.
    ///
    /// # Arguments
    /// * `context` - Plugin execution context
    /// * `function_name` - Name of the function being called
    /// * `args` - Function arguments
    /// * `config` - Plugin configuration
    ///
    /// # Returns
    /// Function execution result
    async fn execute_function(
        &self,
        context: &PluginContext,
        function_name: &str,
        args: &[Value],
        config: &TemplatePluginConfig,
    ) -> Result<PluginResult<Value>>;

    /// Provide data sources
    ///
    /// This method can be called to retrieve data that can be used in templates.
    /// The plugin can provide dynamic data sources that are refreshed periodically.
    ///
    /// # Arguments
    /// * `context` - Plugin execution context
    /// * `data_source` - Name of the requested data source
    /// * `config` - Plugin configuration
    ///
    /// # Returns
    /// Data source content
    async fn get_data_source(
        &self,
        context: &PluginContext,
        data_source: &str,
        config: &TemplatePluginConfig,
    ) -> Result<PluginResult<Value>>;

    /// Validate plugin configuration
    fn validate_config(&self, config: &TemplatePluginConfig) -> Result<()>;

    /// Get list of available data sources
    fn available_data_sources(&self) -> Vec<String>;

    /// Cleanup plugin resources
    async fn cleanup(&self) -> Result<()>;
}

/// Template plugin configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplatePluginConfig {
    /// Plugin-specific configuration
    pub config: HashMap<String, Value>,
    /// Enable/disable the plugin
    pub enabled: bool,
    /// Function prefix (to avoid conflicts)
    pub function_prefix: Option<String>,
    /// Data source refresh interval in seconds
    pub data_refresh_interval_secs: Option<u64>,
    /// Custom settings
    pub settings: HashMap<String, Value>,
}

impl Default for TemplatePluginConfig {
    fn default() -> Self {
        Self {
            config: HashMap::new(),
            enabled: true,
            function_prefix: None,
            data_refresh_interval_secs: Some(300), // 5 minutes
            settings: HashMap::new(),
        }
    }
}

/// Template function metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateFunction {
    /// Function name
    pub name: String,
    /// Function description
    pub description: String,
    /// Function parameters
    pub parameters: Vec<FunctionParameter>,
    /// Return type description
    pub return_type: String,
    /// Examples of usage
    pub examples: Vec<String>,
    /// Function category/tag
    pub category: Option<String>,
    /// Whether function is pure (same inputs = same outputs)
    pub pure: bool,
}

impl TemplateFunction {
    /// Create a new template function
    pub fn new<S: Into<String>>(name: S, description: S, return_type: S) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters: Vec::new(),
            return_type: return_type.into(),
            examples: Vec::new(),
            category: None,
            pure: true,
        }
    }

    /// Add a parameter
    pub fn with_parameter(mut self, param: FunctionParameter) -> Self {
        self.parameters.push(param);
        self
    }

    /// Add multiple parameters
    pub fn with_parameters(mut self, params: Vec<FunctionParameter>) -> Self {
        self.parameters.extend(params);
        self
    }

    /// Add an example
    pub fn with_example<S: Into<String>>(mut self, example: S) -> Self {
        self.examples.push(example.into());
        self
    }

    /// Set category
    pub fn with_category<S: Into<String>>(mut self, category: S) -> Self {
        self.category = Some(category.into());
        self
    }

    /// Mark as impure
    pub fn impure(mut self) -> Self {
        self.pure = false;
        self
    }

    /// Get parameter count
    pub fn param_count(&self) -> usize {
        self.parameters.len()
    }

    /// Check if function has variable arguments
    pub fn has_var_args(&self) -> bool {
        self.parameters.iter().any(|p| p.var_args)
    }
}

/// Function parameter definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionParameter {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub param_type: String,
    /// Parameter description
    pub description: String,
    /// Whether parameter is required
    pub required: bool,
    /// Default value (if optional)
    pub default_value: Option<Value>,
    /// Whether this parameter accepts variable arguments
    pub var_args: bool,
}

impl FunctionParameter {
    /// Create a required parameter
    pub fn required<S: Into<String>>(name: S, param_type: S, description: S) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            description: description.into(),
            required: true,
            default_value: None,
            var_args: false,
        }
    }

    /// Create an optional parameter
    pub fn optional<S: Into<String>>(name: S, param_type: S, description: S) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            description: description.into(),
            required: false,
            default_value: None,
            var_args: false,
        }
    }

    /// Create an optional parameter with default value
    pub fn with_default<S: Into<String>>(
        name: S,
        param_type: S,
        description: S,
        default: Value,
    ) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            description: description.into(),
            required: false,
            default_value: Some(default),
            var_args: false,
        }
    }

    /// Create a variable arguments parameter
    pub fn var_args<S: Into<String>>(name: S, param_type: S, description: S) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            description: description.into(),
            required: false,
            default_value: None,
            var_args: true,
        }
    }
}

/// Template execution context
#[derive(Debug, Clone)]
pub struct TemplateExecutionContext {
    /// Template being processed
    pub template: String,
    /// Current position in template
    pub position: usize,
    /// Available variables
    pub variables: HashMap<String, Value>,
    /// Request context (if available)
    pub request_context: Option<HashMap<String, Value>>,
    /// Custom context data
    pub custom: HashMap<String, Value>,
}

impl TemplateExecutionContext {
    /// Create new execution context
    pub fn new<S: Into<String>>(template: S) -> Self {
        Self {
            template: template.into(),
            position: 0,
            variables: HashMap::new(),
            request_context: None,
            custom: HashMap::new(),
        }
    }

    /// Set position in template
    pub fn with_position(mut self, position: usize) -> Self {
        self.position = position;
        self
    }

    /// Add variable
    pub fn with_variable<S: Into<String>>(mut self, key: S, value: Value) -> Self {
        self.variables.insert(key.into(), value);
        self
    }

    /// Add request context
    pub fn with_request_context(mut self, context: HashMap<String, Value>) -> Self {
        self.request_context = Some(context);
        self
    }

    /// Add custom data
    pub fn with_custom<S: Into<String>>(mut self, key: S, value: Value) -> Self {
        self.custom.insert(key.into(), value);
        self
    }

    /// Get variable value
    pub fn get_variable(&self, key: &str) -> Option<&Value> {
        self.variables.get(key)
    }

    /// Get request context value
    pub fn get_request_value(&self, key: &str) -> Option<&Value> {
        self.request_context.as_ref()?.get(key)
    }

    /// Get custom value
    pub fn get_custom_value(&self, key: &str) -> Option<&Value> {
        self.custom.get(key)
    }
}

/// Template function registry entry
pub struct TemplateFunctionEntry {
    /// Plugin ID that provides this function
    pub plugin_id: crate::PluginId,
    /// Function metadata
    pub function: TemplateFunction,
    /// Plugin instance
    pub plugin: std::sync::Arc<dyn TemplatePlugin>,
    /// Function configuration
    pub config: TemplatePluginConfig,
}

impl TemplateFunctionEntry {
    /// Create new function entry
    pub fn new(
        plugin_id: crate::PluginId,
        function: TemplateFunction,
        plugin: std::sync::Arc<dyn TemplatePlugin>,
        config: TemplatePluginConfig,
    ) -> Self {
        Self {
            plugin_id,
            function,
            plugin,
            config,
        }
    }

    /// Get full function name (with prefix if configured)
    pub fn full_name(&self) -> String {
        if let Some(prefix) = &self.config.function_prefix {
            format!("{}_{}", prefix, self.function.name)
        } else {
            self.function.name.clone()
        }
    }

    /// Check if function is enabled
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }
}

/// Helper trait for creating template plugins
pub trait TemplatePluginFactory: Send + Sync {
    /// Create a new template plugin instance
    fn create_plugin(&self) -> Result<Box<dyn TemplatePlugin>>;
}

/// Built-in template functions that plugins can use as helpers
pub mod builtin {
    use super::*;

    /// Generate a random UUID
    pub fn uuid_v4() -> String {
        uuid::Uuid::new_v4().to_string()
    }

    /// Get current timestamp in RFC3339 format
    pub fn now_rfc3339() -> String {
        chrono::Utc::now().to_rfc3339()
    }

    /// Generate random integer in range
    pub fn random_int(min: i64, max: i64) -> i64 {
        use rand::Rng;
        rand::rng().random_range(min..=max)
    }

    /// Generate random float between 0 and 1
    pub fn random_float() -> f64 {
        rand::random::<f64>()
    }

    /// URL encode a string
    pub fn url_encode(input: &str) -> String {
        urlencoding::encode(input).to_string()
    }

    /// URL decode a string
    pub fn url_decode(input: &str) -> Result<String> {
        urlencoding::decode(input)
            .map(|s| s.to_string())
            .map_err(|e| crate::PluginError::execution(format!("URL decode error: {}", e)))
    }

    /// JSON stringify a value
    pub fn json_stringify(value: &Value) -> Result<String> {
        serde_json::to_string(value)
            .map_err(|e| crate::PluginError::execution(format!("JSON stringify error: {}", e)))
    }

    /// JSON parse a string
    pub fn json_parse(input: &str) -> Result<Value> {
        serde_json::from_str(input)
            .map_err(|e| crate::PluginError::execution(format!("JSON parse error: {}", e)))
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_module_compiles() {
        // Basic compilation test
    }
}