lp/
lib.rs

1use std::fs;
2use toml;
3use serde::Deserialize;
4
5// Plugin Manifest
6#[derive(Debug, Deserialize)]
7pub struct Toml {
8    pub plugin: Plugin,
9}
10
11#[derive(Debug, Deserialize, Clone)]
12pub struct Plugin {
13    pub authors: Option<Vec<String>>,
14    pub name: String,
15    pub version: String,
16    pub description: Option<String>,
17    pub license: Option<String>,
18    pub path: String,
19}
20
21// Plugin Management
22impl Plugin {
23    /// Registers the plugin, adding it to the appropriate directory
24    /// Because application architecture may vary, you may want to implement your own logic for registering plugins.
25    pub fn register(&self, f: impl Fn(Plugin)){
26        f(self.clone());
27    }
28
29    /// Unregisters the plugin, removing it from the directory
30    /// Because application architecture may vary, you may want to implement your own logic for unregistering plugins.
31    pub fn unregister(&self, f: impl Fn(Plugin)) {
32        f(self.clone());
33    }
34
35    /// Executes the plugin
36    /// At the end of the day, it is up to you to implement the logic for what the plugin does when it runs.
37    pub fn run(&self, f: impl Fn(Plugin)) {
38        f(self.clone());
39    }
40}
41
42impl Toml {
43    pub fn parse(path: &str) -> Result<Self, toml::de::Error> {
44        let toml =  fs::read_to_string(path).unwrap();
45        match toml::from_str(&toml) {
46            Ok(toml) => Ok(toml),
47            Err(e) => Err(e),
48        }
49    }
50}
51
52mod tests {
53    #[test]
54    fn test_plugin() {
55        use super::*;
56
57        let toml = Toml::parse("test_asset/plugin.toml").unwrap();
58        
59        toml.plugin.register(|plugin| {
60            assert_eq!(plugin.name, "test_asset".to_string());
61            assert_eq!(plugin.version, "0.1.0".to_string());
62            assert_eq!(plugin.path, "/path/to/test_asset".to_string());
63        });
64    }
65}