use crate::item::BreadcrumbItem;
use std::path::{Component, Path};
#[must_use]
pub fn from_path<P: AsRef<Path>>(path: P) -> Vec<BreadcrumbItem<'static>> {
let path = path.as_ref();
let mut items = Vec::new();
for component in path.components() {
match component {
Component::RootDir => {
items.push(BreadcrumbItem::new("/".to_string()));
}
Component::Prefix(prefix) => {
let s = prefix.as_os_str().to_string_lossy().to_string();
items.push(BreadcrumbItem::new(s));
}
Component::CurDir => {
items.push(BreadcrumbItem::new(".".to_string()));
}
Component::ParentDir => {
items.push(BreadcrumbItem::new("..".to_string()));
}
Component::Normal(os_str) => {
let s = os_str.to_string_lossy().to_string();
items.push(BreadcrumbItem::new(s));
}
}
}
items
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_path() {
let path = Path::new("/projects/ratatui/src/main.rs");
let items = from_path(path);
let labels: Vec<String> = items
.iter()
.map(|item| {
item.label
.spans
.iter()
.map(|s| s.content.as_ref())
.collect()
})
.collect();
assert_eq!(labels, vec!["/", "projects", "ratatui", "src", "main.rs"]);
}
}