use std::path::{Component, Path, PathBuf};
pub(crate) fn get_stdlib_source(module: &str) -> Option<&'static str> {
harn_stdlib::get_stdlib_source(module)
}
pub(crate) fn builtin_reexports(module: &str) -> &'static [&'static str] {
harn_stdlib::builtin_reexports(module)
}
pub(crate) fn stdlib_virtual_path(module: &str) -> PathBuf {
PathBuf::from(format!("<std>/{module}"))
}
pub(crate) fn is_stdlib_virtual_path(path: &Path) -> bool {
path.to_str().is_some_and(|path| path.starts_with("<std>/"))
}
pub(crate) fn relative_stdlib_module(current_file: &Path, import_path: &str) -> Option<String> {
let current = current_file.to_str()?.strip_prefix("<std>/")?;
let mut parts = current.split('/').collect::<Vec<_>>();
parts.pop()?;
for component in Path::new(import_path).components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
parts.pop()?;
}
Component::Normal(part) => parts.push(part.to_str()?),
Component::RootDir | Component::Prefix(_) => return None,
}
}
let last = parts.last_mut()?;
*last = last.strip_suffix(".harn").unwrap_or(last);
let module = parts.join("/");
get_stdlib_source(&module).map(|_| module)
}