-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed some bugs + refectoring + tests added #73
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod files; | ||
pub mod path_result; | ||
pub mod filesystem; | ||
pub mod str_path; | ||
pub mod path; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
use regex::Regex; | ||
|
||
pub fn is_absolute(path: &str) -> bool | ||
{ | ||
path.starts_with('/') || | ||
Regex::new(r"(^(\\\\\?\\)?[a-zA-Z]:)|(^\\\\\?\\[a-zA-Z]{1,})") | ||
.expect("Invalid regex.") | ||
.is_match(path) | ||
} | ||
|
||
pub fn is_root(path: &str) -> bool | ||
{ | ||
path == "/" || | ||
Regex::new(r"(^(\\\\\?\\)?[a-zA-Z]:\\?$)|(^\\\\\?\\[a-zA-Z]{1,}\\?$)") | ||
.expect("Invalid regex.") | ||
.is_match(path) | ||
} | ||
|
||
pub fn parent(path: &str) -> Option<&str> | ||
{ | ||
if is_root(path) | ||
{ | ||
return None; | ||
} | ||
let path = path.trim_end_matches(|c| c == '/' || c == '\\'); | ||
let last_delimiter_index = path.rfind(|c| c == '/' || c == '\\'); | ||
Some(path.split_at(last_delimiter_index? + 1).0) | ||
} | ||
|
||
pub fn join(start: &str, end: &str, separator: char) -> String | ||
{ | ||
let start = start.trim_end_matches(|c| c == separator); | ||
let end = end.trim_start_matches(|c| c == separator); | ||
if end == ".." | ||
{ | ||
parent(start).unwrap_or(start).to_string() | ||
} | ||
else if end == "." | ||
{ | ||
start.to_string() | ||
} | ||
else | ||
{ | ||
format!("{}{}{}", start, separator, end) | ||
} | ||
} | ||
|
||
pub fn filename(path: &str) -> Option<&str> | ||
{ | ||
if is_root(path) | ||
{ | ||
None | ||
} | ||
else | ||
{ | ||
let path = path.trim_end_matches(|c| c == '/' || c == '\\'); | ||
let path = path.rsplit(|c| c == '/' || c == '\\').collect::<Vec<_>>(); | ||
Some(path[0]) | ||
} | ||
} | ||
|
||
pub fn diff<'a>(full_path: &'a str, prefix_path: &str) -> &'a str | ||
{ | ||
if full_path == prefix_path | ||
{ | ||
return "."; | ||
} | ||
else if let Some(unprefixed_path) = full_path.strip_prefix(prefix_path) | ||
{ | ||
unprefixed_path.trim_start_matches(|c| c == '/' || c == '\\') | ||
} | ||
else if let Some(unprefixed_path_reverse_logic) = prefix_path.strip_prefix(full_path) | ||
{ | ||
if unprefixed_path_reverse_logic.split(|c| c == '/' || c == '\\').filter(|s|!s.is_empty()).count() == 1 | ||
{ | ||
return ".."; | ||
} | ||
else | ||
{ | ||
full_path | ||
} | ||
} | ||
else | ||
{ | ||
full_path | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test | ||
{ | ||
use super::*; | ||
|
||
#[test] | ||
fn test_is_absolute() | ||
{ | ||
assert!(is_absolute("/")); | ||
assert!(!is_absolute("test")); | ||
assert!(is_absolute("/home/user")); | ||
assert!(!is_absolute("home/user")); | ||
|
||
assert!(!is_absolute("\\\\?\\")); | ||
assert!(is_absolute("\\\\?\\C:\\Users\\")); | ||
assert!(is_absolute("\\\\?\\d:\\Users")); | ||
assert!(is_absolute("C:\\Users")); | ||
assert!(is_absolute("d:\\Users")); | ||
assert!(!is_absolute("ASDF\\Users")); | ||
assert!(is_absolute("\\\\?\\ASDF\\")); | ||
assert!(!is_absolute(".\\Users")); | ||
} | ||
|
||
#[test] | ||
fn test_is_root() | ||
{ | ||
assert!(is_root("/")); | ||
assert!(!is_root("test")); | ||
assert!(!is_root("/home/user")); | ||
assert!(!is_root("home/user")); | ||
|
||
assert!(!is_root("\\\\?\\")); | ||
assert!(is_root("\\\\?\\C:\\")); | ||
assert!(is_root("\\\\?\\d:")); | ||
assert!(is_root("C:\\")); | ||
assert!(is_root("d:")); | ||
assert!(!is_root("ASDF\\")); | ||
assert!(is_root("\\\\?\\ASDF\\")); | ||
assert!(!is_root(".\\Users")); | ||
} | ||
|
||
#[test] | ||
fn test_parent() | ||
{ | ||
assert_eq!(parent("/home/user"), Some("/home/")); | ||
assert_eq!(parent("/home"), Some("/")); | ||
assert_eq!(parent("/"), None); | ||
assert_eq!(parent("C:\\Users\\user"), Some("C:\\Users\\")); | ||
assert_eq!(parent("C:\\Users"), Some("C:\\")); | ||
assert_eq!(parent("C:\\"), None); | ||
assert_eq!(parent("\\\\?\\C:\\Users\\user"), Some("\\\\?\\C:\\Users\\")); | ||
assert_eq!(parent("\\\\?\\C:\\Users"), Some("\\\\?\\C:\\")); | ||
assert_eq!(parent("\\\\?\\C:\\"), None); | ||
assert_eq!(parent("\\\\?\\ASDF\\Users\\user"), Some("\\\\?\\ASDF\\Users\\")); | ||
assert_eq!(parent("\\\\?\\ASDF\\Users"), Some("\\\\?\\ASDF\\")); | ||
assert_eq!(parent("\\\\?\\ASDF"), None); | ||
} | ||
|
||
#[test] | ||
fn test_join() | ||
{ | ||
assert_eq!(join("/home", "user", '/'), "/home/user"); | ||
assert_eq!(join("/home", "..", '/'), "/"); | ||
assert_eq!(join("/home", ".", '/'), "/home"); | ||
|
||
assert_eq!(join("C:\\Users", "user", '\\'), "C:\\Users\\user"); | ||
assert_eq!(join("C:\\Users", "..", '\\'), "C:\\"); | ||
assert_eq!(join("C:\\Users", ".", '\\'), "C:\\Users"); | ||
} | ||
|
||
#[test] | ||
fn test_filename() | ||
{ | ||
assert_eq!(filename("/home/user"), Some("user")); | ||
assert_eq!(filename("/home"), Some("home")); | ||
assert_eq!(filename("/"), None); | ||
assert_eq!(filename("C:\\Users\\user"), Some("user")); | ||
assert_eq!(filename("C:\\Users"), Some("Users")); | ||
assert_eq!(filename("C:\\"), None); | ||
assert_eq!(filename("\\\\?\\C:\\Users\\user"), Some("user")); | ||
assert_eq!(filename("\\\\?\\C:\\Users"), Some("Users")); | ||
assert_eq!(filename("\\\\?\\C:\\"), None); | ||
assert_eq!(filename("\\\\?\\ASDF\\Users\\user"), Some("user")); | ||
assert_eq!(filename("\\\\?\\ASDF\\Users"), Some("Users")); | ||
assert_eq!(filename("\\\\?\\ASDF"), None); | ||
} | ||
|
||
#[test] | ||
fn test_diff() | ||
{ | ||
assert_eq!(diff("/home/user", "/home"), "user"); | ||
assert_eq!(diff("/home", "/home/user"), ".."); | ||
assert_eq!(diff("/home", "/home"), "."); | ||
assert_eq!(diff("/home", "/"), "home"); | ||
assert_eq!(diff("/", "/"), "."); | ||
assert_eq!(diff("C:\\Users\\user", "C:\\Users"), "user"); | ||
assert_eq!(diff("C:\\Users", "C:\\Users\\user"), ".."); | ||
assert_eq!(diff("C:\\Users", "C:\\Users"), "."); | ||
assert_eq!(diff("C:\\Users", "C:\\"), "Users"); | ||
assert_eq!(diff("C:\\", "C:\\"), "."); | ||
assert_eq!(diff("\\\\?\\C:\\Users\\user", "\\\\?\\C:\\Users"), "user"); | ||
assert_eq!(diff("\\\\?\\C:\\Users", "\\\\?\\C:\\Users"), "."); | ||
assert_eq!(diff("\\\\?\\C:\\Users", "\\\\?\\C:\\"), "Users"); | ||
assert_eq!(diff("\\\\?\\C:\\", "\\\\?\\C:\\"), "."); | ||
assert_eq!(diff("\\\\?\\ASDF\\Users\\user", "\\\\?\\ASDF\\Users"), "user"); | ||
assert_eq!(diff("\\\\?\\ASDF\\Users", "\\\\?\\ASDF\\Users"), "."); | ||
assert_eq!(diff("\\\\?\\ASDF\\Users", "\\\\?\\ASDF"), "Users"); | ||
assert_eq!(diff("\\\\?\\ASDF", "\\\\?\\ASDF"), "."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / clippy
unneeded return statement Warning