-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.rs
173 lines (155 loc) · 4.66 KB
/
config.rs
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
use toml::Value;
const CONFIG_FILE_TEMPLATE: &str = "[hooks]
pre-commit = \"cargo test\"
[logging]
verbose = true
";
const DEFAULT_CONFIG_FILE_NAME: &str = ".rusty-hook.toml";
const CONFIG_FILE_NAMES: [&str; 2] = [DEFAULT_CONFIG_FILE_NAME, "rusty-hook.toml"];
pub const NO_CONFIG_FILE_FOUND: &str = "No config file found";
pub const MISSING_CONFIG_KEY: &str = "Missing config key";
pub const FATAL_ERROR_DURING_CONFIG_LOOKUP: &str =
"Fatal error encountered while looking for existing config";
fn find_config_file<F>(root_directory_path: &str, file_exists: F) -> Result<String, String>
where
F: Fn(&str) -> Result<bool, ()>,
{
for &config_file_name in CONFIG_FILE_NAMES.iter() {
let path = format!("{}/{}", root_directory_path, config_file_name);
match file_exists(&path) {
Err(_) => {
return Err(String::from(FATAL_ERROR_DURING_CONFIG_LOOKUP));
}
Ok(found) => {
if found {
return Ok(path);
}
}
};
}
Ok(String::from(NO_CONFIG_FILE_FOUND))
}
pub fn create_default_config_file<F, G>(
write_file: F,
file_exists: G,
root_directory_path: &str,
) -> Result<(), String>
where
F: Fn(&str, &str, bool) -> Result<(), String>,
G: Fn(&str) -> Result<bool, ()>,
{
create_config_file(
&write_file,
&file_exists,
root_directory_path,
DEFAULT_CONFIG_FILE_NAME,
)
}
pub fn create_config_file<F, G>(
write_file: F,
file_exists: G,
root_directory_path: &str,
desired_config_file_name: &str,
) -> Result<(), String>
where
F: Fn(&str, &str, bool) -> Result<(), String>,
G: Fn(&str) -> Result<bool, ()>,
{
match find_config_file(root_directory_path, &file_exists) {
Err(_) => {
return Err(String::from(FATAL_ERROR_DURING_CONFIG_LOOKUP));
}
Ok(path) => {
if path != NO_CONFIG_FILE_FOUND {
return Ok(());
}
}
};
let config_file = if CONFIG_FILE_NAMES
.iter()
.any(|n| n == &desired_config_file_name)
{
desired_config_file_name
} else {
DEFAULT_CONFIG_FILE_NAME
};
if write_file(
&format!("{}/{}", root_directory_path, config_file),
CONFIG_FILE_TEMPLATE,
false,
)
.is_err()
{
return Err(String::from("Failed to create config file"));
};
Ok(())
}
pub fn get_config_file_contents<F, G>(
read_file: F,
file_exists: G,
root_directory_path: &str,
) -> Result<String, String>
where
F: Fn(&str) -> Result<String, ()>,
G: Fn(&str) -> Result<bool, ()>,
{
let path = match find_config_file(root_directory_path, &file_exists) {
Ok(path) => {
if path == NO_CONFIG_FILE_FOUND {
return Err(String::from(NO_CONFIG_FILE_FOUND));
} else {
path
}
}
Err(_) => return Err(String::from(NO_CONFIG_FILE_FOUND)),
};
match read_file(&path) {
Ok(contents) => Ok(contents),
Err(_) => Err(String::from("Failure reading file")),
}
}
fn get_table_key_value_from_config(
config_contents: &str,
table: &str,
key: &str,
) -> Result<Value, String> {
let value = match config_contents.parse::<Value>() {
Ok(val) => val,
Err(_) => return Err(String::from("Error parsing config file")),
};
let config = value.as_table().unwrap();
if !config.contains_key(table) {
return Err(String::from("Missing config table"));
};
if !value[table].as_table().unwrap().contains_key(key) {
return Err(String::from(MISSING_CONFIG_KEY));
};
Ok(value[table][key].clone())
}
pub fn get_log_setting(config_contents: &str) -> bool {
match get_table_key_value_from_config(config_contents, "logging", "verbose") {
Err(_) => true,
Ok(value) => value.as_bool().unwrap_or(true),
}
}
pub fn get_hook_script(config_contents: &str, hook_name: &str) -> Result<String, String> {
match get_table_key_value_from_config(config_contents, "hooks", hook_name) {
Err(err) => Err(err),
Ok(value) => match value {
Value::String(script) => Ok(script),
Value::Array(val) => Ok(val
.iter()
.map(|v| v.as_str())
.collect::<Option<Vec<_>>>()
.ok_or(format!(
"Invalid hook config for {}. An element in the array is not a string",
hook_name
))?
.join(" && ")),
_ => Err(String::from("Invalid hook config")),
},
}
}
#[cfg(test)]
#[path = "config_test.rs"]
mod config_tests;