use crate::{builder::metadata::BODY_META, *};
use anyhow::{anyhow, Context as _};
use std::fs::copy;
use std::io::Write;
pub struct FileReader;
impl FileReader {
pub fn new() -> Self {
Self
}
}
impl Compiler for FileReader {
fn compile(&self, ctx: Context) -> CompilerReturn {
compile!({
let mut ctx = ctx;
let src = ctx.get_source_data()?;
ctx.insert_compiling_raw_metadata(BODY_META, src)?;
Ok(ctx)
})
}
}
pub struct FileWriter;
impl FileWriter {
pub fn new() -> Self {
Self
}
}
impl Compiler for FileWriter {
fn compile(&self, ctx: Context) -> CompilerReturn {
compile!({
let target_display = ctx.target()?;
let target_display = target_display.display();
let mut target = ctx
.open_target()
.with_context(|| format!("Failed to open file {}", target_display))?;
let body = ctx.body()?;
if let Some(s) = body.as_str() {
target
.write(s.as_bytes())
.with_context(|| format!("Failed to write file {}", target_display))?;
} else if body.is_array() {
let bytes = body.as_bytes()?;
target
.write(&bytes)
.with_context(|| format!("Failed to write file {}", target_display))?;
} else {
return Err(anyhow!("Invalid body format"));
}
Ok(ctx)
})
}
}
pub struct CopyCompiler;
impl CopyCompiler {
pub fn new() -> Self {
Self
}
}
impl Compiler for CopyCompiler {
fn compile(&self, ctx: Context) -> CompilerReturn {
compile!({
ctx.create_target_parent_dir()?;
let src = ctx.source()?;
let tgt = ctx.target()?;
copy(src, tgt).context("Copy error")?;
Ok(ctx)
})
}
}