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
use std::collections::HashMap;
use std::{path::Path, io::Read, fs::File};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostManifest {
    pub actors: Vec<String>,
    pub capabilities: Vec<String>,
    pub config: Vec<ConfigEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigEntry {
    pub actor: String,
    pub capability: String,
    pub values: HashMap<String, String>,
}

impl HostManifest {
    pub fn from_yaml(path: impl AsRef<Path>) -> ::std::result::Result<HostManifest, Box<dyn std::error::Error>> {
        let mut buf = Vec::new();
        let mut file = File::open(path)?;
        file.read_to_end(&mut buf)?;
        match serde_yaml::from_slice::<HostManifest>(&buf) {
            Ok(m) => Ok(m),
            Err(e) => Err(format!("Failed to deserialize yaml: {} ", e).into())
        }
    }

    pub fn from_json(path: impl AsRef<Path>) -> ::std::result::Result<HostManifest, Box<dyn std::error::Error>> {
        let mut buf = Vec::new();
        let mut file = File::open(path)?;
        file.read_to_end(&mut buf)?;
        match serde_json::from_slice::<HostManifest>(&buf) {
            Ok(m) => Ok(m),
            Err(_) => Err("Failed to deserialize json".into())
        }
    }
}

#[cfg(test)]
mod test {
    use crate::manifest::ConfigEntry;
    use std::collections::HashMap;
    #[test]    
    fn round_trip() {
        let manifest = super::HostManifest{
            actors: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            capabilities: vec!["wascc:one".to_string(), "wascc:two".to_string()],
            config: vec![
                ConfigEntry{
                    actor: "a".to_string(),
                    capability: "wascc:one".to_string(),
                    values: gen_values(),
                }
            ],
        };
        let yaml = serde_yaml::to_string(&manifest).unwrap();
        assert_eq!(yaml, "---\nactors:\n  - a\n  - b\n  - c\ncapabilities:\n  - \"wascc:one\"\n  - \"wascc:two\"\nconfig:\n  - actor: a\n    capability: \"wascc:one\"\n    values:\n      ROOT: /tmp");
    }

    fn gen_values() -> HashMap<String, String> {
        let mut hm = HashMap::new();
        hm.insert("ROOT".to_string(), "/tmp".to_string());
    
        hm
    }
}

/*
---
actors:
- bob
- al
- steve
capabilities:
- file://foo
config:
- actor: bob
  capability: wascc:messaging
  values:
    a: b
    */