use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PyPath {
absolute: bool,
parts: Vec<String>,
}
impl PyPath {
pub fn parse(path: &str) -> Self {
Self {
absolute: path.starts_with('/'),
parts: path
.split('/')
.filter(|part| !part.is_empty() && *part != ".")
.map(str::to_string)
.collect(),
}
}
pub fn is_absolute(&self) -> bool {
self.absolute
}
pub fn join(&self, other: &Self) -> Self {
if other.absolute {
return other.clone();
}
let mut parts = self.parts.clone();
parts.extend(other.parts.iter().cloned());
Self {
absolute: self.absolute,
parts,
}
}
}
impl fmt::Display for PyPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.parts.is_empty() {
return f.write_str(if self.absolute { "/" } else { "." });
}
if self.absolute {
f.write_str("/")?;
}
f.write_str(&self.parts.join("/"))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rendered(path: &str) -> String {
PyPath::parse(path).to_string()
}
fn joined(base: &str, other: &str) -> String {
PyPath::parse(base).join(&PyPath::parse(other)).to_string()
}
#[test]
fn parsing_drops_dot_segments_and_duplicate_separators() {
assert_eq!(rendered("./a"), "a");
assert_eq!(rendered("a//b"), "a/b");
assert_eq!(rendered("a/./b"), "a/b");
assert_eq!(rendered("/tmp//fx/"), "/tmp/fx");
}
#[test]
fn dotdot_is_never_collapsed() {
assert_eq!(rendered("sub/../a"), "sub/../a");
assert_eq!(joined("/tmp/fx", "sub/../a"), "/tmp/fx/sub/../a");
assert_eq!(joined("/tmp/fx", ".."), "/tmp/fx/..");
}
#[test]
fn there_is_no_empty_path() {
assert_eq!(rendered(""), ".");
assert_eq!(rendered("/"), "/");
assert_eq!(joined("/tmp/fx", ""), "/tmp/fx");
assert_eq!(joined("", ""), ".");
assert_eq!(joined(".", "a.txt"), "a.txt");
}
#[test]
fn an_absolute_other_wins() {
assert_eq!(joined("/tmp/fx", "/etc/passwd"), "/etc/passwd");
assert_eq!(joined("relative", "/etc"), "/etc");
}
#[test]
fn a_relative_base_stays_relative() {
assert_eq!(joined("work", "a.txt"), "work/a.txt");
assert!(!PyPath::parse("work").is_absolute());
assert!(PyPath::parse("/work").is_absolute());
}
}