use crate::core::ir::{ApiSurface, EnumDef, TypeDef};
use ahash::AHashMap;
pub fn core_type_path(typ: &TypeDef, core_import: &str) -> String {
core_type_path_remapped(typ, core_import, &[])
}
pub fn core_type_path_remapped(typ: &TypeDef, core_import: &str, remaps: &[(&str, &str)]) -> String {
let path = typ.rust_path.replace('-', "_");
if path.contains("::") {
apply_crate_remaps(&path, remaps)
} else {
format!("{core_import}::{}", typ.name)
}
}
pub fn apply_crate_remaps(path: &str, remaps: &[(&str, &str)]) -> String {
if remaps.is_empty() {
return path.to_string();
}
if let Some(sep) = path.find("::") {
let leading = &path[..sep];
if let Some(&(_, override_crate)) = remaps.iter().find(|(orig, _)| *orig == leading) {
return format!("{override_crate}{}", &path[sep..]);
}
}
path.to_string()
}
pub fn core_enum_path(enum_def: &EnumDef, core_import: &str) -> String {
core_enum_path_remapped(enum_def, core_import, &[])
}
pub fn core_enum_path_remapped(enum_def: &EnumDef, core_import: &str, remaps: &[(&str, &str)]) -> String {
let path = enum_def.rust_path.replace('-', "_");
if path.starts_with(core_import) || path.contains("::") {
apply_crate_remaps(&path, remaps)
} else {
format!("{core_import}::{}", enum_def.name)
}
}
pub fn build_type_path_map(surface: &ApiSurface, core_import: &str) -> AHashMap<String, String> {
let mut map = AHashMap::new();
for typ in surface.types.iter().filter(|typ| !typ.is_trait) {
let path = typ.rust_path.replace('-', "_");
let resolved = if path.starts_with(core_import) {
path
} else {
format!("{core_import}::{}", typ.name)
};
map.insert(typ.name.clone(), resolved);
}
for en in &surface.enums {
let path = en.rust_path.replace('-', "_");
let resolved = if path.starts_with(core_import) {
path
} else {
format!("{core_import}::{}", en.name)
};
map.insert(en.name.clone(), resolved);
}
map
}
pub fn resolve_named_path(name: &str, core_import: &str, path_map: &AHashMap<String, String>) -> String {
if let Some(path) = path_map.get(name) {
path.clone()
} else {
format!("{core_import}::{name}")
}
}