use camino::Utf8Path;
pub fn relative_prefix(output_relpath: &Utf8Path) -> String {
let depth = output_relpath.components().count().saturating_sub(1); if depth == 0 {
".".to_string()
} else {
std::iter::repeat_n("..", depth)
.collect::<Vec<_>>()
.join("/")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn root_index() {
assert_eq!(relative_prefix(Utf8Path::new("index.html")), ".");
}
#[test]
fn one_level_deep() {
assert_eq!(
relative_prefix(Utf8Path::new("directory/Breakfast.html")),
".."
);
}
#[test]
fn two_levels_deep() {
assert_eq!(
relative_prefix(Utf8Path::new("recipe/Breakfast/Pancakes.html")),
"../.."
);
}
#[test]
fn three_levels_deep() {
assert_eq!(
relative_prefix(Utf8Path::new("recipe/A/B/C.html")),
"../../.."
);
}
}