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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::collections::HashMap;
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum SystemdItem<'a> {
Comment(&'a str),
Category(&'a str),
Directive(&'a str, &'a str),
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct UnitDirective {
key: String,
value: String,
category: String,
}
impl UnitDirective {
pub fn value(&self) -> &str { &self.value }
pub fn key(&self) -> &str { &self.key }
pub fn category(&self) -> &str { &self.category }
}
impl UnitDirective {
pub fn new(category: &str, key: &str, value: &str) -> UnitDirective {
UnitDirective {
category: String::from(category),
value: String::from(value),
key: String::from(key),
}
}
pub fn item_list_to_unit_directive_list(unit_items: &Vec<SystemdItem>)
-> Result<Vec<UnitDirective>, ()> {
use self::SystemdItem::*;
if unit_items.len() < 1 {
return Err(())
}
let mut cat = try!(UnitDirective::get_first_category(unit_items));
let mut res = vec!();
for item in unit_items {
match *item {
Category(new_cat) => cat = new_cat,
Directive(key, value) => res.push(UnitDirective::new(cat, key, value)),
_ => ()
}
}
Ok(res)
}
fn get_first_category<'b>(unit_items: &'b Vec<SystemdItem<'b>>) -> Result<&'b str, ()> {
if let Some(&SystemdItem::Category(first_cat)) = unit_items.get(0) {
Ok(first_cat)
} else {
return Err(())
}
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct SystemdUnit {
directives: HashMap<String, DirectiveEntry>
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum DirectiveEntry {
Solo(UnitDirective),
Many(Vec<UnitDirective>)
}
impl SystemdUnit {
pub fn new(unit_items: &Vec<SystemdItem>) -> Result<SystemdUnit, ()> {
let directives = try!(
UnitDirective::item_list_to_unit_directive_list(&unit_items)
);
let directives_hash = try!(
SystemdUnit::hash_from_directives(directives)
);
let res = SystemdUnit {
directives: directives_hash
};
Ok(res)
}
fn hash_from_directives(directives: Vec<UnitDirective>) -> Result<HashMap<String, DirectiveEntry>, ()> {
use self::DirectiveEntry::*;
use std::collections::hash_map::Entry;
let mut directives_hash = HashMap::new();
for directive in directives {
match directives_hash.entry(directive.key.clone()) {
Entry::Vacant(entry) => { entry.insert(Solo(directive)); },
Entry::Occupied(mut entry_container) => {
let mut vecs = vec!();
match *entry_container.get() {
Solo(ref first_dir) => { vecs.push(first_dir.clone()); },
Many(ref dirs) => { vecs = dirs.clone(); }
}
vecs.push(directive);
try!(SystemdUnit::validate_many(&vecs));
entry_container.insert(Many(vecs));
},
}
}
Ok(directives_hash)
}
fn validate_many(dirs: &Vec<UnitDirective>) -> Result<(), ()> {
assert!(dirs.len() >= 2);
let first = dirs.first().unwrap();
let last = dirs.last().unwrap();
if first.category == last.category {
Ok(())
} else {
Err(())
}
}
pub fn lookup_by_key(&self, key: &str) -> Option<&DirectiveEntry> {
self.directives.get(key)
}
pub fn lookup_by_category(&self, category: &str) -> Vec<&DirectiveEntry> {
use self::DirectiveEntry::*;
self.directives
.values()
.filter(|directive| match *directive {
&Solo(ref dir) => dir.category == category,
&Many(ref dirs) => category == dirs.get(0).expect("dirs.len() > 0").category,
})
.collect()
}
pub fn has_key(&self, key: &str) -> bool {
self.directives.contains_key(key)
}
pub fn has_category(&self, category: &str) -> bool {
use self::DirectiveEntry::*;
self.directives
.values()
.any(|directive| match *directive {
Solo(ref dir) => dir.category == category,
Many(ref dirs) => category == dirs.get(0).expect("dirs.len() > 0").category,
})
}
}