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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
extern crate serde;
extern crate toml;
use crate::hmerror::HMError;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use std::{
fmt, fs, io,
io::{prelude::*, BufReader},
path::{Path, PathBuf},
string::String,
};
use toml::value;
#[derive(Debug, Clone, Hash)]
pub struct Worker {
pub name: String,
pub status: Option<i32>,
pub completed: bool,
}
impl<'a> Worker {
#[allow(dead_code)]
pub fn new() -> Worker {
Worker {
name: String::from(""),
status: Some(1),
completed: false,
}
}
}
impl PartialEq for Worker {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Worker {}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ManagedObject {
pub name: String,
pub source: String,
pub file: String,
pub destination: String,
pub method: String,
pub task: String,
pub solution: String,
pub dependencies: Vec<String>,
pub satisfied: bool,
}
impl ManagedObject {
pub fn is_task(&self) -> bool {
return !self.solution.is_empty();
}
pub fn set_satisfied(&mut self) -> () {
self.satisfied = true;
}
}
impl fmt::Display for ManagedObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} {} {} {} {} {} {} {}",
self.name,
self.file,
self.source,
self.method,
self.destination,
self.task,
self.solution,
self.satisfied
)
}
}
impl Default for ManagedObject {
fn default() -> Self {
ManagedObject {
name: String::from(""),
source: String::from(""),
file: String::from(""),
destination: String::from(""),
method: String::from(""),
task: String::from(""),
solution: String::from(""),
dependencies: Vec::new(),
satisfied: false,
}
}
}
#[derive(Deserialize, Clone)]
pub struct Config {
#[serde(rename = "obj", deserialize_with = "deserialize_files")]
pub files: Vec<(String, value::Value)>,
}
impl Default for Config {
fn default() -> Self {
Config { files: Vec::new() }
}
}
impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut mos: Vec<ManagedObject> = Vec::new();
for _f in self.files.iter() {
let mut mo = ManagedObject::default();
mo.name = _f.0.to_owned();
match _f.1.get("file") {
None => (),
Some(_x) => {
mo.file = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("source") {
None => (),
Some(_x) => {
mo.source = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("method") {
None => (),
Some(_x) => {
mo.method = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("destination") {
None => (),
Some(_x) => {
mo.destination = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("solution") {
None => (),
Some(_x) => {
mo.solution = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("task") {
None => (),
Some(_x) => {
mo.task = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("dependencies") {
None => (),
Some(_x) => {
let _f = _x.as_str().unwrap();
mo.dependencies = _f.split(", ").map(|s| s.to_string()).collect();
}
}
mos.push(mo);
}
write!(f, "{:#?}", mos)
}
}
#[allow(dead_code)]
pub fn get_mo(_n: String) -> Result<ManagedObject, HMError> {
unimplemented!("not done")
}
pub fn deserialize_files<'de, D>(deserializer: D) -> Result<Vec<(String, value::Value)>, D::Error>
where
D: Deserializer<'de>,
{
let mut files: Vec<(String, value::Value)> = Vec::new();
let raw_files: Vec<value::Table> = Deserialize::deserialize(deserializer)?;
for mut entry in raw_files {
if let Some(name) = entry.remove("file") {
if let Some(name) = name.as_str() {
files.push((name.to_owned(), value::Value::Table(entry)));
}
} else if let Some(name) = entry.remove("task") {
if let Some(name) = name.as_str() {
files.push((name.to_owned(), value::Value::Table(entry)));
}
}
}
Ok(files)
}
pub fn as_managed_objects(config: Config) -> HashMap<String, ManagedObject> {
let mut mos: HashMap<String, ManagedObject> = HashMap::new();
for _f in config.files.iter() {
let mut mo = ManagedObject::default();
mo.name = _f.0.to_owned();
match _f.1.get("solution") {
None => (),
Some(_x) => {
mo.solution = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("task") {
None => (),
Some(_x) => {
mo.task = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("source") {
None => (),
Some(_x) => {
mo.source = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("method") {
None => (),
Some(_x) => {
mo.method = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("destination") {
None => (),
Some(_x) => {
mo.destination = String::from(_x.as_str().unwrap());
}
}
match _f.1.get("dependencies") {
None => (),
Some(_x) => {
let _f = _x.as_str().unwrap();
mo.dependencies = _f.split(", ").map(|s| s.to_string()).collect();
}
}
mos.insert(mo.name.clone(), mo);
}
return mos;
}
fn open_config(file: &str) -> io::Result<fs::File> {
fs::File::open(file)
}
pub fn deserialize_file(file: &str) -> Result<Config, String> {
let mut contents = String::new();
let g = match open_config(file) {
Ok(_a) => _a,
Err(e) => return Err(e.to_string()),
};
let mut file_contents = BufReader::new(g);
match file_contents.read_to_string(&mut contents) {
Ok(v) => v,
Err(_e) => 0,
};
if cfg!(debug_assertions) {
println!("file: {}", &file);
}
toml::from_str(&contents).or_else(|e| Err(e.to_string()))
}
pub fn ensure_config_dir() -> Result<PathBuf, &'static str> {
match dirs::config_dir() {
Some(p) => {
let whole_path = p.join(Path::new("homemaker"));
match fs::create_dir_all(&whole_path) {
Ok(()) => return Ok(PathBuf::from(&whole_path.join("config.toml"))),
Err(_e) => return Err("Couldn't create config dir!"),
}
}
None => return Err("Couldn't get config directory from $XDG"),
};
}