mockforge_plugin_core/manifest/
loader.rs1use crate::{PluginError, Result};
7use std::path::Path;
8
9use super::models::PluginManifest;
10
11pub struct ManifestLoader;
13
14impl ManifestLoader {
15 pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<PluginManifest> {
17 PluginManifest::from_file(path)
18 }
19
20 pub fn load_from_string(content: &str) -> Result<PluginManifest> {
22 PluginManifest::parse_from_str(content)
23 }
24
25 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 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 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 eprintln!("Failed to load manifest from {}: {}", path.display(), e);
53 }
54 }
55 }
56 }
57
58 Ok(manifests)
59 }
60
61 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 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 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 }
103}