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
|
use std::{fs, path};
use std::io::ErrorKind;
use toml;
pub struct Config {
config: toml::Value,
commands: toml::Value,
}
impl Config {
pub fn new() -> Config {
println!("Using built-in config");
Config {
config: toml::toml!{
source = "src"
build_dir = "build"
do_not_scan = []
exclude = []
file_delim = ":?"
command_delim = ":!"
commands_file = "commands.toml"
},
commands: toml::toml!{
empty = ""
}
}
}
pub fn import(config: &str) -> Config {
println!("Using found config");
let config: toml::Value = toml::from_str(config).expect("Could not load config");
let config = config.get("config").expect("Could not find \"[config]\" field");
// Attempt to read commands file
let commands: toml::Value = match config.get("commands_file") {
Some(n) => {
if n.is_str() {
let file_string = n.as_str().unwrap();
let file_string = match fs::read_to_string(file_string) {
Ok(n) => n,
Err(_) => {println!("Could not load commands from file"); String::from("[commands]")},
};
toml::from_str(file_string.as_str()).expect("Invalid toml")
} else {
panic!()
}
},
None => {println!("Could not load commands from file"); toml::toml!{[commands]}},
};
let commands = commands.get("commands").expect("Could not find \"[commands]\" field");
Config {
config: config.clone(),
commands: commands.clone(),
}
}
pub fn get_str(&self, string: &str) -> Option<&str> {
match self.config.get(string) {
Some(n) => n.as_str(),
None => None
}
}
pub fn get_cmd_str(&self, string: &str) -> Option<&str> {
match self.commands.get(string) {
Some(n) => n.as_str(),
None => None
}
}
}
pub fn create_dir(path: &str) -> Result<(), ()> {
match fs::create_dir(std::path::PathBuf::from(path)) {
Ok(_) => (),
Err(n) => match n.kind() {
ErrorKind::AlreadyExists => (),
_ => return Err(()),
}
}
return Ok(())
}
pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf {
let mut path = path.to_str().unwrap().split("/");
path.next();
let path: path::PathBuf = path.collect();
return path
}
pub fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf {
let mut head = path::PathBuf::from(head);
head.push(path);
return head
}
pub fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf {
let path = remove_path_head(path);
append_path_head(&path, head)
}
pub fn remove_path_tail(path: &mut path::PathBuf) -> &path::PathBuf {
path.pop();
path
}
|