use code_ranker_plugin_api::{attrs::AttrValue, node::Node};
pub(crate) fn node_path(node: &Node) -> String {
if let Some(AttrValue::Str(p)) = node.attrs.get("path") {
return p.clone();
}
node.id
.strip_prefix("{target}/")
.unwrap_or(&node.id)
.to_string()
}
pub(crate) struct PathParts {
pub name: String,
pub stem: String,
pub ext: String,
pub dir: String,
}
pub(crate) fn split_path(path: &str) -> PathParts {
let name = path.rsplit('/').next().unwrap_or(path).to_string();
let dir = match path.rfind('/') {
Some(i) => path[..i].to_string(),
None => String::new(),
};
let (stem, ext) = match name.rfind('.') {
Some(i) if i > 0 => (name[..i].to_string(), name[i + 1..].to_string()),
_ => (name.clone(), String::new()),
};
PathParts {
name,
stem,
ext,
dir,
}
}
pub(crate) fn path_fields(node: &Node) -> [(&'static str, String); 5] {
let path = node_path(node);
let parts = split_path(&path);
[
("path", path),
("name", parts.name),
("stem", parts.stem),
("ext", parts.ext),
("dir", parts.dir),
]
}