pub mod fd;
pub mod filemap;
#[cfg(target_os = "linux")]
pub mod landlock;
pub mod tempdir;
use std::{
fs::canonicalize,
io::ErrorKind,
path::{PathBuf, absolute},
};
use clean_path::clean;
fn split_guest_and_host_path(mapping: &str) -> Result<(PathBuf, PathBuf), ErrorKind> {
let mut mappingiter = mapping.split(':');
let host_str = mappingiter.next().ok_or(ErrorKind::InvalidInput)?;
let guest_str = mappingiter.next().ok_or(ErrorKind::InvalidInput)?;
let host_path = clean(canonicalize(host_str).unwrap_or_else(|_| absolute(host_str).unwrap()));
let guest_path = clean(guest_str);
Ok((guest_path, host_path))
}
#[test]
fn test_split_guest_and_host_path() {
use std::path::PathBuf;
let host_guest_strings = [
"./host_string.txt:guest_string.txt",
"/home/user/host_string.txt:guest_string.md.txt",
"host_string.txt:this_does_exist.txt:should_not_exist.txt",
"host_string.txt:test/..//guest_string.txt",
];
let mut fixture_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fixture_path.push("host_string.txt");
let results = [
(PathBuf::from("guest_string.txt"), fixture_path.clone()),
(
PathBuf::from("guest_string.md.txt"),
PathBuf::from("/home/user/host_string.txt"),
),
(PathBuf::from("this_does_exist.txt"), fixture_path.clone()),
(PathBuf::from("guest_string.txt"), fixture_path),
];
for (host_and_guest_string, expected_result) in host_guest_strings
.into_iter()
.map(split_guest_and_host_path)
.zip(results)
{
assert_eq!(
host_and_guest_string.expect("Result is an error!"),
expected_result,
);
}
}