use std::path::PathBuf;
use crate::ast::walk::Visitor;
use crate::ast::Expression;
pub struct Rewriter {
base: PathBuf,
}
impl Rewriter {
pub fn new<P: Into<PathBuf>>(base: P) -> Self {
Self { base: base.into() }
}
}
impl Visitor for Rewriter {
fn visit_expression(&mut self, expr: &mut Expression) {
let main_separator = format!("{}", std::path::MAIN_SEPARATOR);
if let Expression::Include(ref mut def) = expr {
let path = PathBuf::from(def.path.fragment.as_ref());
if path.is_relative() {
def.path.fragment = self.base.join(path).to_string_lossy().to_string().into();
}
}
if let Expression::Import(ref mut def) = expr {
let path = PathBuf::from(
&def.path
.fragment
.replace("/", &main_separator)
.replace("\\", &main_separator),
);
if path.starts_with(format!("std{}", main_separator)) {
return;
}
if path.is_relative() {
def.path.fragment = self.base.join(path).to_string_lossy().to_string().into();
}
}
}
}