relative paths in #include are relative to the file's parent directory, if possible

This commit is contained in:
Mark 2023-12-05 09:55:00 +01:00
parent 0aca8e5b25
commit 8b60da8d99
2 changed files with 13 additions and 5 deletions

View File

@ -1,6 +1,6 @@
use std::{path::PathBuf, sync::Arc};
use super::{Source, SourcePos};
use super::{Source, SourceFrom, SourcePos};
use crate::{
data::Data,
errors::{error_colors, CheckError},
@ -251,8 +251,16 @@ pub fn parse_no_chain(
src.skip_whitespace();
let string_in_src = src.get_pos();
if src.next_char() == Some('"') {
let file_path = parse_string(src, srca, string_in_src)?;
match Source::new_from_file(PathBuf::from(&file_path)) {
let file_path_str = parse_string(src, srca, string_in_src)?;
let mut file_path: PathBuf = PathBuf::from(&file_path_str);
if !file_path.is_absolute() {
if let SourceFrom::File(other_file_path) = srca.src_from() {
if let Some(files_dir) = other_file_path.parent() {
file_path = files_dir.join(file_path);
}
}
}
match Source::new_from_file(file_path) {
Ok(mut inner_src) => {
let inner_srca = Arc::new(inner_src.clone());
return Ok(Some(Box::new(
@ -277,7 +285,7 @@ pub fn parse_no_chain(
Some(error_colors::HashIncludeCantLoadFile),
),
])
.msg(format!("Can't load file '{file_path}': {e}")));
.msg(format!("Can't load file '{file_path_str}': {e}")));
}
}
} else {

View File

@ -17,7 +17,7 @@ use super::Config;
impl Config {
/// Adds functions to deal with iterables
/// `iter: fn` executes a function once for each element of the iterable
/// `for_each: fn` executes a function once for each element of the iterable
/// `map: fn` maps each value in the iterable to a new one by applying a transformation function
/// `filter: fn` filters the iterable by removing all elements where the filter function doesn't return true
/// `filter_map: fn` combines filter and map. requires that the function returns ()/(t).