use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub enum AnyPath {
Image(String),
Host(PathBuf),
}
impl AnyPath {
pub fn is_host(&self) -> bool {
matches!(self, AnyPath::Host(_))
}
}
pub fn resolve(raw: &str, image_cwd: &str, host_cwd: &Path) -> AnyPath {
if let Some(rest) = raw.strip_prefix('@') {
AnyPath::Host(resolve_host(rest, host_cwd))
} else {
AnyPath::Image(resolve_image(raw, image_cwd))
}
}
pub fn resolve_image(raw: &str, image_cwd: &str) -> String {
let combined = if raw.starts_with('/') {
raw.to_string()
} else if image_cwd.ends_with('/') {
format!("{}{}", image_cwd, raw)
} else {
format!("{}/{}", image_cwd, raw)
};
normalise_image(&combined)
}
fn normalise_image(path: &str) -> String {
let mut stack: Vec<&str> = Vec::new();
for part in path.split('/') {
match part {
"" | "." => {}
".." => {
stack.pop();
}
other => stack.push(other),
}
}
if stack.is_empty() {
"/".to_string()
} else {
let mut out = String::new();
for part in stack {
out.push('/');
out.push_str(part);
}
out
}
}
fn resolve_host(raw: &str, host_cwd: &Path) -> PathBuf {
let expanded = expand_tilde(raw);
let candidate = PathBuf::from(expanded);
if candidate.is_absolute() {
candidate
} else {
host_cwd.join(candidate)
}
}
fn expand_tilde(raw: &str) -> String {
if let Some(rest) = raw.strip_prefix("~/") {
if let Some(home) = std::env::var_os("HOME") {
let mut buf = PathBuf::from(home);
buf.push(rest);
return buf.to_string_lossy().into_owned();
}
} else if raw == "~"
&& let Some(home) = std::env::var_os("HOME")
{
return home.to_string_lossy().into_owned();
}
raw.to_string()
}
pub fn image_basename(path: &str) -> &str {
path.rsplit_once('/').map(|(_, name)| name).unwrap_or(path)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn image_relative_paths() {
assert_eq!(resolve_image("foo", "/etc"), "/etc/foo");
assert_eq!(resolve_image("./foo", "/etc"), "/etc/foo");
assert_eq!(resolve_image("../bin/ls", "/etc"), "/bin/ls");
assert_eq!(resolve_image("/abs", "/etc"), "/abs");
assert_eq!(resolve_image("..", "/etc/x"), "/etc");
assert_eq!(resolve_image("../..", "/etc/x"), "/");
}
#[test]
fn host_prefix_marker() {
let cwd = Path::new("/work");
match resolve("@/tmp/x", "/", cwd) {
AnyPath::Host(p) => assert_eq!(p, Path::new("/tmp/x")),
_ => panic!("expected host"),
}
match resolve("@foo", "/", cwd) {
AnyPath::Host(p) => assert_eq!(p, Path::new("/work/foo")),
_ => panic!("expected host"),
}
match resolve("foo", "/etc", cwd) {
AnyPath::Image(p) => assert_eq!(p, "/etc/foo"),
_ => panic!("expected image"),
}
}
#[test]
fn parent_and_basename() {
assert_eq!(image_basename("/etc/profile"), "profile");
assert_eq!(image_basename("/"), "");
}
}