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

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

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

#[macro_export]
macro_rules! create_path_or_die {
    (dir: $path:expr, $message:expr) => {
        if let Err(e) =  std::fs::create_dir($path) {
            die!(format!("{}\nError: {}", $message, e.to_string()));
        }
    };

    (file: $path:expr, $content:expr, $message:expr) => {
        if let Ok(mut file) = std::fs::File::create($path) {
            if let Err(e) = write!(file, $content) {
                die!(format!("{}\nError: {}", $message, e.to_string()));
            }
        } 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);
    }
}

pub fn deserialize_kv_with_message(data: &Vec<u8>) -> (HashMap<String, String>, String) {
    let string_rep = String::from_utf8(data.clone());
    if string_rep.is_err() {
        die!("Failed to parse commit data");
    }
    let string_rep = string_rep.unwrap();
    let mut is_message = false;
    let mut header: HashMap<String, String> = HashMap::new();
    let mut message = String::new();
    let mut last_key: Option<String> = None;
    for line in string_rep.lines() {
        if is_message {
            message.push_str(format!("\n{}", line).as_str());
            continue;
        }

        if line == "" {
            is_message = true;
            continue;
        }

        if line.starts_with(" ") && last_key.is_some() {
            let key = last_key.clone().unwrap();
            match header.get(&key) {
                Some(val) => {
                    let mut new_val = val.clone();
                    new_val.push_str(format!("\n{}", line).as_str());
                    header.insert(key, new_val);
                }
                None => {
                    header.insert(key, line.to_string());
                }
            }
        } else {
            let mut line_iter = line.splitn(2, ' ');
            let key = line_iter.next().unwrap();
            let val = line_iter.next().unwrap();
            header.insert(key.to_string(), val.to_string());
            last_key = Some(key.to_string());
        }
    }
    (header, message)
}