-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
49 lines (39 loc) · 1.05 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
use std::env;
#[derive(Debug, PartialEq)]
pub enum Object {
Directory,
File,
}
pub fn is_valid_name(name: &str, object: Object) -> bool {
let invalid_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
if name.chars().any(|c| invalid_chars.contains(&c)) {
return false;
}
if name.is_empty() {
return false;
}
if name.starts_with(' ') || name.ends_with(' ') {
return false;
}
if object == Object::Directory && name.contains('.') {
return false;
}
if object == Object::File && name.contains(' ') {
return false;
}
true
}
pub fn get_current_directory_path() -> String {
let current_dir_path = match env::current_dir() {
Ok(path) => path,
Err(_) => panic!("Could not get current directory path"),
};
current_dir_path.to_str().unwrap().to_string()
}
pub fn exit_with_error(error: &str, show_help: bool) {
println!("{}", error);
if show_help {
println!("use -h for a list of all commands");
}
std::process::exit(0);
}