-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
123 lines (115 loc) · 4.17 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
pub static PROJECT_FILE_EXTENSION: &str = "pman";
use std::ops::Add;
use std::path::{Path, PathBuf};
use crate::structure::{Project, Task, TaskContainer};
use std::io::Error;
pub fn create_working_folder_if_not_exist() {
let working_folder = get_working_folder();
if !working_folder.exists() {
match std::fs::create_dir(working_folder.as_path()) {
Ok(_) => {}
Err(e) => {
panic!("Error occurred while creating the working dir: {}", e);
}
};
}
}
pub fn wrap(string_to_wrap: String, wrap_at_length: u32) -> String {
let words: Vec<&str> = string_to_wrap.split(" ").collect();
let mut line_length: u32 = 0;
let mut final_string = String::new();
let shortened_wrap = wrap_at_length - 4; // arrow length
for word in words.iter() {
line_length += word.chars().count() as u32;
line_length += 1; // accounting for the space
if line_length >= shortened_wrap {
final_string = final_string.add("\n");
line_length = word.chars().count() as u32;
} else {
final_string = final_string.add(" ");
}
final_string = final_string.add(word);
}
final_string
}
pub fn get_working_folder() -> PathBuf {
let work_path = match std::env::args().nth(1) {
Some(val) => std::path::PathBuf::from(val),
None => dirs::home_dir().unwrap(),
};
let folder_path = String::from('.').add(PROJECT_FILE_EXTENSION);
work_path.join(Path::new(folder_path.as_str()))
}
pub fn delete_project_of_name(project_name: String, working_path: PathBuf) -> Result<(), Error> {
let mut path = working_path.join(project_name);
path.set_extension(PROJECT_FILE_EXTENSION);
match std::fs::remove_file(path.as_path()) {
Ok(()) => Ok(()),
Err(e) => Result::Err(e),
}
}
pub fn load_project_from_path(path: PathBuf) -> Result<Project, std::io::Error> {
match std::fs::read_to_string(path) {
Ok(project_string) => match serde_json::from_str(project_string.as_str()) {
Ok(deserialized_project) => Result::Ok(deserialized_project),
Err(e) => Result::Err(Error::from(e)),
},
Err(e) => Result::Err(e),
}
}
pub fn get_projects_in_path(path: PathBuf) -> Vec<Project> {
let mut serialized_projects: Vec<Project> = vec![];
let folder_result = std::fs::read_dir(path.as_path()).unwrap();
for file in folder_result {
let f = file.unwrap();
if f.file_type().unwrap().is_file() {
match f.path().extension() {
Some(ext) => {
if ext == PROJECT_FILE_EXTENSION {
match match serde_json::from_str(
std::fs::read_to_string(f.path()).unwrap().as_str(),
) {
Ok(result) => Some(result),
Err(_) => None,
} {
Some(project) => serialized_projects.push(project),
_ => {}
}
}
}
_ => {}
};
}
}
serialized_projects
}
#[test]
fn create_dummy_project() {
create_dummy_project_with_name(String::from("project1"));
create_dummy_project_with_name(String::from("project2"));
create_dummy_project_with_name(String::from("project3"));
}
#[allow(dead_code)]
fn create_dummy_project_with_name(name: String) {
create_working_folder_if_not_exist();
let mut p = Project::new(name.clone());
p.description = p.name.clone().add(" description");
p.add_task(
name.clone().add("test 1"),
String::from("Sample description"),
);
p.add_task(
name.clone().add("test2 2"),
String::from("Sample description"),
);
p.completed_tasks.push(Task::new(
String::from("a completed task"),
String::from("Sample description"),
));
let mut project_file_path = get_working_folder().join(p.name.clone());
project_file_path.set_extension(PROJECT_FILE_EXTENSION);
match p.write_project_full_path(project_file_path) {
Ok(_) => {}
Err(e) => panic!("{}", e),
}
}