1use std::fs;
2use toml;
3use serde::Deserialize;
4
5#[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
21impl Plugin {
23 pub fn register(&self, f: impl Fn(Plugin)){
26 f(self.clone());
27 }
28
29 pub fn unregister(&self, f: impl Fn(Plugin)) {
32 f(self.clone());
33 }
34
35 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}