Files
ValueScript/valuescript_compiler/src/resolve_path.rs
2023-03-09 12:19:53 +11:00

28 lines
712 B
Rust

use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ResolvedPath {
pub path: String,
}
impl std::fmt::Display for ResolvedPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path)
}
}
pub fn resolve_path(importer_path: &ResolvedPath, path: &String) -> ResolvedPath {
let importer_path_buf = PathBuf::from(&importer_path.path);
let parent = importer_path_buf.parent().unwrap_or_else(|| Path::new("/"));
ResolvedPath {
path: parent
.join(path)
.canonicalize()
.expect("Failed to canonicalize path")
.to_str()
.expect("Failed to convert path to string")
.to_string(),
}
}