mockforge_plugin_core/manifest/
loader.rs

1//! Plugin manifest loading utilities
2//!
3//! This module provides functionality for loading and parsing plugin manifests
4//! from files and strings.
5
6use crate::{PluginError, Result};
7use std::path::Path;
8
9use super::models::PluginManifest;
10
11/// Manifest loader utility
12pub struct ManifestLoader;
13
14impl ManifestLoader {
15    /// Load manifest from file path
16    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<PluginManifest> {
17        PluginManifest::from_file(path)
18    }
19
20    /// Load manifest from string content
21    pub fn load_from_string(content: &str) -> Result<PluginManifest> {
22        PluginManifest::parse_from_str(content)
23    }
24
25    /// Load and validate manifest from file
26    pub fn load_and_validate_from_file<P: AsRef<Path>>(path: P) -> Result<PluginManifest> {
27        let manifest = Self::load_from_file(path)?;
28        manifest.validate()?;
29        Ok(manifest)
30    }
31
32    /// Load and validate manifest from string
33    pub fn load_and_validate_from_string(content: &str) -> Result<PluginManifest> {
34        let manifest = Self::load_from_string(content)?;
35        manifest.validate()?;
36        Ok(manifest)
37    }
38
39    /// Load multiple manifests from directory
40    pub fn load_from_directory<P: AsRef<Path>>(dir: P) -> Result<Vec<PluginManifest>> {
41        let mut manifests = Vec::new();
42
43        for entry in std::fs::read_dir(dir)? {
44            let entry = entry?;
45            let path = entry.path();
46
47            if path.extension().is_some_and(|ext| ext == "yaml" || ext == "yml") {
48                match Self::load_from_file(&path) {
49                    Ok(manifest) => manifests.push(manifest),
50                    Err(e) => {
51                        // Log error but continue loading other manifests
52                        eprintln!("Failed to load manifest from {}: {}", path.display(), e);
53                    }
54                }
55            }
56        }
57
58        Ok(manifests)
59    }
60
61    /// Load and validate multiple manifests from directory
62    pub fn load_and_validate_from_directory<P: AsRef<Path>>(dir: P) -> Result<Vec<PluginManifest>> {
63        let manifests = Self::load_from_directory(dir)?;
64        let mut validated = Vec::new();
65
66        for manifest in manifests {
67            match manifest.validate() {
68                Ok(_) => validated.push(manifest),
69                Err(e) => {
70                    eprintln!("Failed to validate manifest for plugin {}: {}", manifest.id(), e);
71                }
72            }
73        }
74
75        Ok(validated)
76    }
77}
78
79impl PluginManifest {
80    /// Load manifest from file
81    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
82        let content = std::fs::read_to_string(path).map_err(|e| {
83            PluginError::config_error(&format!("Failed to read manifest file: {}", e))
84        })?;
85
86        Self::parse_from_str(&content)
87    }
88
89    /// Parse manifest from string
90    pub fn parse_from_str(content: &str) -> Result<Self> {
91        serde_yaml::from_str(content)
92            .map_err(|e| PluginError::config_error(&format!("Failed to parse manifest: {}", e)))
93    }
94}
95
96#[cfg(test)]
97mod tests {
98
99    #[test]
100    fn test_module_compiles() {
101        // Basic compilation test
102    }
103}