-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rs
109 lines (96 loc) · 3.44 KB
/
utils.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
use std::path::Path;
use std::fs::{File, create_dir_all, metadata};
use std::io::{Write, Read};
use crate::preprocessing::PreprocessedObject;
use std::ops::Deref;
use std::panic;
use crate::config::Config;
/// Returns the content of a file
pub fn get_file_content(path: &String) -> String {
let path_obj = Path::new(path);
let mut file = match File::open(&path_obj) {
Err(_) => {
stop_program(format!("Couldn't open the file {}. Please check again.", &path));
panic!()
},
Ok(file) => file
};
let mut content = String::new();
match file.read_to_string(&mut content) {
Err(_) => stop_program(format!("Couldn't read the file {}. Try again or try another file.", &path)),
_ => {}
}
content
}
/// Exports content into a file
pub fn export_content_into_file(path: &String, content: &String) {
let path_obj = Path::new(&path);
let mut file = match File::create(&path_obj) {
Err(_) => {
stop_program(format!("Couldn't create the file {}.", &path));
panic!()
},
Ok(file) => file
};
match file.write_all(content.as_bytes()) {
Err(_) => stop_program(format!("Couldn't write into file {}.", &path)),
_ => {}
}
}
/// Returning the blocks like it was still a text in the file
pub fn get_blocks_as_content(blocks: &Vec<Box<PreprocessedObject>>) -> String {
blocks.into_iter()
.map(|f| {
if let PreprocessedObject::Block {text, spaces} = f.deref() {
return format!("{}{}", " ".repeat(*spaces), text.clone());
}
"".to_string()
})
.collect::<Vec<String>>().join("\n")
}
/// Creating a directory if it's not exist.
/// If there's an error, it's printing to the console.
pub fn create_directory_if_not_exist(directory: &String) {
let directory_metadata = metadata(&directory);
if directory_metadata.is_err() || directory_metadata.unwrap().is_file() {
if create_dir_all(&directory).is_err() {
stop_program(format!("Couldn't create the directory {}", &directory));
}
}
}
/// Returns a parameter which is given by the system arguments
/// For example, if there's "--XML file" it'll return "file"
/// Additionally, it removes these arguments from the vector.
pub fn get_argument_parameter(arg: &String, args: &mut Vec<String>) -> Option<String> {
// Find target arg, if there's one
let index = args.iter().position(|x| x.eq_ignore_ascii_case(&arg));
let mut result: Option<String> = Option::None;
// Get the arg itself and remove these args from the vector
if index.is_some() {
result = Option::Some(args.get(index.unwrap()+1).unwrap().clone());
args.remove(index.unwrap());
args.remove(index.unwrap());
}
result
}
/// Checks if it has an argument, if it does it removes it (For 'single arguments' like --debug)
pub fn has_argument_parameter(arg: &String, args: &mut Vec<String>) -> bool {
let index = args.iter().position(|x| x.eq_ignore_ascii_case(&arg));
if index.is_some() {
args.remove(index.unwrap());
return true;
}
false
}
/// Prints a message if the config has debug=true
pub fn debug(msg: String, config: &Config) {
if config.debug {
println!("{}", msg);
}
}
/// Stops the program and prints a message
pub fn stop_program(message: String) {
println!("{}", message);
panic::set_hook(Box::new(|_| {}));
panic!();
}