sciimg/
path.rs

1use std::path::Path;
2
3// Checks if file exists.
4pub fn file_exists(chk_path: &str) -> bool {
5    Path::new(&chk_path).exists()
6}
7
8pub fn basename(chk_path: &str) -> String {
9    String::from(Path::new(&chk_path).file_name().unwrap().to_str().unwrap())
10}
11
12pub fn file_writable(chk_path: &str) -> bool {
13    let path = Path::new(&chk_path);
14    !path.metadata().unwrap().permissions().readonly()
15}
16
17pub fn get_parent(chk_path: &str) -> String {
18    let path = Path::new(&chk_path);
19    let parent = path.parent().unwrap();
20    String::from(parent.to_str().unwrap())
21}
22
23pub fn parent_exists(chk_path: &str) -> bool {
24    let parent = get_parent(chk_path);
25    if !parent.is_empty() {
26        file_exists(parent.as_str())
27    } else {
28        true // Cannot assume true on $CWD (or that it is $CWD)...
29    }
30}
31
32pub fn parent_writable(chk_path: &str) -> bool {
33    let parent = get_parent(chk_path);
34    if !parent.is_empty() {
35        file_writable(parent.as_str())
36    } else {
37        true // Cannot assume true on $CWD (or that is is $CWD)...
38    }
39}
40
41pub fn parent_exists_and_writable(chk_path: &str) -> bool {
42    parent_exists(chk_path) && parent_writable(chk_path)
43}