use indexmap::IndexMap;
pub(super) fn register(tree: &str, prefix: &str, aliases: &mut IndexMap<String, String>) {
if let Some((root, branches)) = tree.split_once('{')
&& let Some(branches) = branches.trim().strip_suffix('}')
{
let root = root.trim().trim_end_matches("::");
let next = join(prefix, root);
let mut depth = 0usize;
let mut start = 0;
for (index, ch) in branches.char_indices() {
match ch {
'{' => depth += 1,
'}' => depth = depth.saturating_sub(1),
',' if depth == 0 => {
register(&branches[start..index], &next, aliases);
start = index + 1;
}
_ => {}
}
}
register(&branches[start..], &next, aliases);
return;
}
let parts = tree.split_whitespace().collect::<Vec<_>>();
let (path, alias) = match parts.as_slice() {
[path, "as", alias] => (*path, *alias),
_ => {
let path = parts.join("");
let target = join(prefix, &path);
if let Some(alias) = target
.rsplit("::")
.next()
.filter(|name| !name.is_empty() && *name != "*")
{
aliases.insert(alias.to_owned(), target.clone());
}
return;
}
};
aliases.insert(alias.to_owned(), join(prefix, path));
}
fn join(prefix: &str, path: &str) -> String {
match (prefix, path.trim_matches(':')) {
("", path) => path.to_owned(),
(prefix, "" | "self") => prefix.to_owned(),
(prefix, path) => format!("{prefix}::{path}"),
}
}