mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
absolutize path common functions
This commit is contained in:
43
src/utils/path.rs
Normal file
43
src/utils/path.rs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
//! # Path
|
||||||
|
//!
|
||||||
|
//! Path related utilities
|
||||||
|
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
/// ### absolutize
|
||||||
|
///
|
||||||
|
/// Absolutize target path if relative.
|
||||||
|
/// For example:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// assert_eq!(absolutize(&Path::new("/home/omar"), &Path::new("readme.txt")).as_path(), Path::new("/home/omar/readme.txt"));
|
||||||
|
/// assert_eq!(absolutize(&Path::new("/home/omar"), &Path::new("/tmp/readme.txt")).as_path(), Path::new("/tmp/readme.txt"));
|
||||||
|
/// ```
|
||||||
|
pub fn absolutize(wrkdir: &Path, target: &Path) -> PathBuf {
|
||||||
|
match target.is_absolute() {
|
||||||
|
true => target.to_path_buf(),
|
||||||
|
false => {
|
||||||
|
let mut p: PathBuf = wrkdir.to_path_buf();
|
||||||
|
p.push(target);
|
||||||
|
p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn absolutize_path() {
|
||||||
|
assert_eq!(
|
||||||
|
absolutize(&Path::new("/home/omar"), &Path::new("readme.txt")).as_path(),
|
||||||
|
Path::new("/home/omar/readme.txt")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
absolutize(&Path::new("/home/omar"), &Path::new("/tmp/readme.txt")).as_path(),
|
||||||
|
Path::new("/tmp/readme.txt")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user