ceph_async/
mon_command.rs

1use std::collections::HashMap;
2
3use serde_json;
4
5#[cfg(test)]
6mod tests {
7    use super::*;
8
9    #[test]
10    fn it_builds_a_mon_command() {
11        let command = MonCommand::new()
12            .with_prefix("osd set")
13            .with("key", "osdout");
14
15        let actual: HashMap<String, String> = serde_json::from_str(&command.as_json()).unwrap();
16        let expected: HashMap<String, String> =
17            serde_json::from_str(r#"{"prefix":"osd set","format":"json","key":"osdout"}"#).unwrap();
18
19        assert_eq!(expected, actual);
20    }
21}
22
23pub struct MonCommand<'a> {
24    map: HashMap<&'a str, &'a str>,
25}
26
27impl<'a> Default for MonCommand<'a> {
28    fn default() -> Self {
29        MonCommand {
30            map: {
31                let mut map = HashMap::new();
32                map.insert("format", "json");
33                map
34            },
35        }
36    }
37}
38
39impl<'a> MonCommand<'a> {
40    pub fn new() -> MonCommand<'a> {
41        MonCommand::default()
42    }
43
44    pub fn with_format(self, format: &'a str) -> MonCommand<'a> {
45        self.with("format", format)
46    }
47
48    pub fn with_name(self, name: &'a str) -> MonCommand<'a> {
49        self.with("name", name)
50    }
51
52    pub fn with_prefix(self, prefix: &'a str) -> MonCommand<'a> {
53        self.with("prefix", prefix)
54    }
55
56    pub fn with(mut self, name: &'a str, value: &'a str) -> MonCommand<'a> {
57        self.map.insert(name, value);
58        self
59    }
60
61    pub fn as_json(&self) -> String {
62        serde_json::to_string(&self.map).unwrap()
63    }
64}