-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_object.rs
More file actions
45 lines (33 loc) · 992 Bytes
/
json_object.rs
File metadata and controls
45 lines (33 loc) · 992 Bytes
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
use std::fs::{read_to_string, File};
use std::io;
use std::io::Write;
use std::process::exit;
use json::JsonValue;
use crate::config::get_config_path;
pub fn get_json_object_or_create(force_create: bool) -> JsonValue {
let path_exists = &get_config_path().exists();
if force_create && !path_exists {
let mut file = File::create(get_config_path()).unwrap();
write!(file, "{}", "{}").unwrap();
}
get_json_object()
}
pub fn get_json_object() -> JsonValue {
let path = get_config_path();
if !path.exists() {
eprintln!("config file does not exist at '{:?}'", &path);
exit(1);
}
let json = json::parse(&*read_to_string(&path).unwrap()).unwrap();
if !json.is_object() {
eprintln!("config file is not a JSON file ('{:?}')", &path);
exit(1);
}
return json;
}
pub fn set_json_object(json: JsonValue) -> io::Result<()> {
let mut file = File::create(get_config_path())?;
let json_string = json::stringify_pretty(json, 2);
write!(file, "{}", json_string)?;
Ok(())
}