2020-09-12 23:25:23 +12:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
2020-09-01 05:37:30 +12:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2020-09-02 05:34:39 +12:00
|
|
|
/// Class for common functions used across other class/functions
|
|
|
|
|
2020-09-01 05:37:30 +12:00
|
|
|
pub struct Common();
|
|
|
|
impl Common {
|
2020-09-12 23:25:23 +12:00
|
|
|
/// Printing time which took between start and stop point and prints also function name
|
2020-09-17 22:07:58 +12:00
|
|
|
pub fn print_time(_start_time: SystemTime, _end_time: SystemTime, _function_name: String) {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
println!("Execution of function \"{}\" took {:?}", _function_name, _end_time.duration_since(_start_time).expect("Time cannot go reverse."));
|
2020-09-01 05:37:30 +12:00
|
|
|
}
|
2020-09-12 08:32:17 +12:00
|
|
|
|
2020-09-12 23:25:23 +12:00
|
|
|
pub fn delete_multiple_entries(entries: &[String]) -> Vec<String> {
|
|
|
|
let mut path: &Path;
|
|
|
|
let mut warnings: Vec<String> = Vec::new();
|
|
|
|
for entry in entries {
|
|
|
|
path = Path::new(entry);
|
|
|
|
if path.is_dir() {
|
|
|
|
match fs::remove_dir_all(&entry) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => warnings.push("Failed to remove folder ".to_owned() + entry.as_str()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match fs::remove_file(&entry) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => warnings.push("Failed to remove file ".to_owned() + entry.as_str()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
warnings
|
|
|
|
}
|
|
|
|
pub fn delete_one_entry(entry: &str) -> String {
|
|
|
|
let path: &Path = Path::new(entry);
|
|
|
|
let mut warning: String = String::from("");
|
2020-09-16 05:17:13 +12:00
|
|
|
if path.is_dir() {
|
|
|
|
match fs::remove_dir_all(&entry) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => warning = "Failed to remove folder ".to_owned() + entry,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match fs::remove_file(&entry) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => warning = "Failed to remove file ".to_owned() + entry,
|
|
|
|
}
|
2020-09-12 23:25:23 +12:00
|
|
|
}
|
|
|
|
warning
|
|
|
|
}
|
|
|
|
|
2020-09-12 08:32:17 +12:00
|
|
|
/// Function to check if directory match expression
|
|
|
|
pub fn regex_check(expression: &str, directory: &str) -> bool {
|
|
|
|
if !expression.contains('*') {
|
2020-09-17 23:35:11 +12:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
println!("Invalid expression ERROR: Expression should have *");
|
|
|
|
}
|
2020-09-12 08:32:17 +12:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let temp_splits: Vec<&str> = expression.split('*').collect();
|
|
|
|
let mut splits: Vec<&str> = Vec::new();
|
|
|
|
for i in temp_splits {
|
|
|
|
if i != "" {
|
|
|
|
splits.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if splits.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Early checking if directory contains all parts needed by expression
|
|
|
|
for split in &splits {
|
|
|
|
if !directory.contains(split) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut position_of_splits: Vec<usize> = Vec::new();
|
|
|
|
|
|
|
|
// `git*` shouldn't be true for `/gitsfafasfs`
|
|
|
|
if !expression.starts_with('*') && directory.find(&splits[0]).unwrap() > 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// `*home` shouldn't be true for `/homeowner`
|
|
|
|
if !expression.ends_with('*') && !directory.ends_with(splits.last().unwrap()) {
|
|
|
|
// && !directory.ends_with(&(splits.last().unwrap().to_string() + "/")){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the end we check if parts between * are correctly positioned
|
|
|
|
position_of_splits.push(directory.find(&splits[0]).unwrap());
|
|
|
|
let mut current_index: usize;
|
|
|
|
let mut found_index: usize;
|
|
|
|
for i in splits[1..].iter().enumerate() {
|
|
|
|
current_index = *position_of_splits.get(i.0).unwrap() + i.1.len();
|
|
|
|
found_index = match directory[current_index..].find(i.1) {
|
|
|
|
Some(t) => t,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
position_of_splits.push(found_index + current_index);
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2020-09-18 17:32:37 +12:00
|
|
|
|
|
|
|
pub struct Messages {
|
|
|
|
pub messages: Vec<String>,
|
|
|
|
pub warnings: Vec<String>,
|
|
|
|
pub errors: Vec<String>,
|
|
|
|
}
|
2020-09-26 07:05:47 +12:00
|
|
|
|
2020-09-18 17:32:37 +12:00
|
|
|
impl Messages {
|
|
|
|
pub fn new() -> Messages {
|
|
|
|
Messages {
|
|
|
|
messages: vec![],
|
|
|
|
warnings: vec![],
|
|
|
|
errors: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn print_messages(&self) {
|
|
|
|
if !self.messages.is_empty() {
|
|
|
|
println!("-------------------------------MESSAGES--------------------------------");
|
|
|
|
for i in &self.messages {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
println!("---------------------------END OF MESSAGES-----------------------------");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.warnings.is_empty() {
|
|
|
|
println!("-------------------------------WARNINGS--------------------------------");
|
|
|
|
|
|
|
|
for i in &self.warnings {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
println!("---------------------------END OF WARNINGS-----------------------------");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.errors.is_empty() {
|
|
|
|
println!("--------------------------------ERRORS---------------------------------");
|
|
|
|
|
|
|
|
for i in &self.errors {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
println!("----------------------------END OF ERRORS------------------------------");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Default for Messages {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 08:32:17 +12:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::common::Common;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_regex() {
|
|
|
|
assert!(Common::regex_check("*home*", "/home/rafal"));
|
|
|
|
assert!(Common::regex_check("*home", "/home"));
|
|
|
|
assert!(Common::regex_check("*home/", "/home/"));
|
|
|
|
assert!(Common::regex_check("*home/*", "/home/"));
|
|
|
|
assert!(Common::regex_check("*.git*", "/home/.git"));
|
|
|
|
assert!(Common::regex_check("*/home/rafal*rafal*rafal*rafal*", "/home/rafal/rafalrafalrafal"));
|
|
|
|
assert!(!Common::regex_check("*home", "/home/"));
|
|
|
|
assert!(!Common::regex_check("*home", "/homefasfasfasfasf/"));
|
|
|
|
assert!(!Common::regex_check("*home", "/homefasfasfasfasf"));
|
|
|
|
assert!(!Common::regex_check("rafal*afal*fal", "rafal"));
|
2020-09-17 23:35:11 +12:00
|
|
|
assert!(!Common::regex_check("rafal*a", "rafal"));
|
2020-09-12 08:32:17 +12:00
|
|
|
assert!(!Common::regex_check("AAAAAAAA****", "/AAAAAAAAAAAAAAAAA"));
|
|
|
|
assert!(!Common::regex_check("*.git/*", "/home/.git"));
|
|
|
|
assert!(!Common::regex_check("*home/*koc", "/koc/home/"));
|
|
|
|
assert!(!Common::regex_check("*home/", "/home"));
|
|
|
|
assert!(!Common::regex_check("*TTT", "/GGG"));
|
|
|
|
assert!(!Common::regex_check("AAA", "AAA"));
|
|
|
|
}
|
2020-09-01 05:37:30 +12:00
|
|
|
}
|