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
//! Define our `Config` and `Worker`.
//! Implement how to take the `config.toml` and turn it into a `Config { files: Vec<ManagedObject> }`.
//! Describes the `Worker` object, which is how we communicate back to our `main()` thread
//! about how our `task` is going.
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;

///
/// Allow us to communicate meaningfully back to `main()` thread.
///
#[derive(Debug, Clone, Hash)]
pub struct Worker {
  pub name: String,
  pub status: Option<i32>,
  pub completed: bool,
}

impl<'a> Worker {
  // i'll get to you
  #[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 {}

/// We're a super-set of all the kinds of `ManagedObject`s we can be.
/// Just don't use the fields you don't wanna use.
/// A simple `ManagedObject` is a name, source, destination, and method (currently only symlink).
/// The simple `ManagedObject` would just be symlinked to its destination.
/// Complex `ManagedObject`s include solutions, which are executed scripts.
#[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 {
  /// quite simply, if we're a task, we'll have a `solution`.
  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,
    }
  }
}

/// Represents just the file `config.toml` and contains a vector of things
/// that shall become `ManagedObject`s.
#[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() }
  }
}

/*
  this is all such terrible rust please don't look at it
*/
/// pls do not view src for this, am not proud
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();
          // thanks https://stackoverflow.com/a/37547426
          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")
}

/// This takes our file/task array and turns them into `ManagedObjects`,
/// to be stuffed into the `Config`.
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)
}

/// Convenience function that allows getting a HashMap from a `Config` of
/// the `ManagedObject`s within.
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();
        // thanks https://stackoverflow.com/a/37547426
        mo.dependencies = _f.split(", ").map(|s| s.to_string()).collect();
      }
    }
    mos.insert(mo.name.clone(), mo);
  }
  return mos;
}

/// Take a big, fat guess.
/// Open the specified file. We've already made sure our Path and stuff
/// look good.
fn open_config(file: &str) -> io::Result<fs::File> {
  fs::File::open(file)
}

/// Open our config file and read the entire contents into hopefully
/// valid toml. Either we gucci and return back a `Config` made of toml,
/// or we explain what went wrong with the `toml` Err.
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()))
}

/// Make sure $XDG_CONFIG_DIR exists.
/// On Linux and similar this is /home/\<username\>/.config;
/// macOS /Users/\<username\>/.config.
///
/// Assuming it does exist or we can create it, stuff config.toml on the end of it
/// and return `Ok(my_path_buf/config.toml)`.
///
/// This is safe because `ensure_config_dir()` is called only after we already know
/// the user didn't specify a `config.toml` path themselves. We must check our
/// default expected location for it.
pub fn ensure_config_dir() -> Result<PathBuf, &'static str> {
  // get /home/<username>/.config, if exists...
  match dirs::config_dir() {
    Some(p) => {
      // if something
      // creates a PathBuf from $XDG_CONFIG_DIR
      let whole_path = p.join(Path::new("homemaker"));
      match fs::create_dir_all(&whole_path) {
        /*
        then when we return it, do the entire config dir path (/home/hlmtre/.config)
        and add our config file to the end of the PathBuf
        ```
        Ok(("/home/hlmtre/.config").join("config.toml"))
        ```
        sort of as a pseudocodey example
         */
        Ok(()) => return Ok(PathBuf::from(&whole_path.join("config.toml"))),
        Err(_e) => return Err("Couldn't create config dir!"),
      }
    }
    // if dirs::config_path() call doesn't return anything
    None => return Err("Couldn't get config directory from $XDG"),
  };
}