summaryrefslogtreecommitdiff
path: root/src/utilities.rs
blob: 1c2b26f78ca1a6741882a7ff8389c070d3170dfc (plain)
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
use std::collections::HashMap;
use std::path::PathBuf;

pub type ConfigHashMap = HashMap<String, HashMap<String, Option<String>>>;

#[macro_export]
macro_rules! die {
    ($message:expr) => {
        println!("{}", $message);
        std::process::exit(1);
    }
}

#[macro_export]
macro_rules! create_path_or_die {
    (dir: $path:expr, $message:expr) => {
        if let Err(_) =  std::fs::create_dir($path) {
        die!($message);
        }
    };

    (file: $path:expr, $content:expr, $message:expr) => {
        if let Ok(mut file) = std::fs::File::create($path) {
            if let Err(_) = file.write_all($content.as_ref()) {
                die!($message);
            }
        } else {
            die!($message);
        }
    };
}

pub fn path_should_exist(path: &PathBuf, message: &str) {
    if !path.exists() {
        die!(message);
    }
}

pub fn path_should_not_exist(path: &PathBuf, message: &str) {
    if path.exists() {
        die!(message);
    }
}