add ability to include file as data

This commit is contained in:
2021-11-21 13:34:15 +01:00
parent 8027a8849b
commit 640c7fff52
7 changed files with 60 additions and 6 deletions

31
src/includes.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use crate::ast;
use anyhow::{anyhow, Result};
pub fn resolve_includes(script: &mut ast::Script, path: &Path) -> Result<()> {
let script_dir = path.parent().expect("Script path has no parent");
for data in &mut script.data {
for values in &mut data.data {
match values {
ast::DataValues::File {
ref path,
ref mut data,
} => {
let mut full_path = script_dir.to_path_buf();
full_path.push(path);
File::open(&full_path)
.map_err(|e| {
anyhow!("Failed to load data from {}: {}", full_path.display(), e)
})?
.read_to_end(data)?;
}
_ => (),
}
}
}
Ok(())
}