mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 19:56:42 +01:00
add ability to include file as data
This commit is contained in:
31
src/includes.rs
Normal file
31
src/includes.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user