use crate::{formats::pyc::PycWriteConfig, program::PythonProgram};
use gaia_binary::{BinaryWriter, Fixed, LittleEndian};
use gaia_types::GaiaError;
use std::io::Write;
#[derive(Debug)]
pub struct PycWriter<W: Write> {
writer: BinaryWriter<W, Fixed<LittleEndian>>,
config: PycWriteConfig,
}
impl<W: Write> PycWriter<W> {
pub fn new(writer: W, config: PycWriteConfig) -> Self {
Self { writer: BinaryWriter::new(writer), config }
}
pub fn write(&mut self, program: &PythonProgram) -> Result<usize, GaiaError> {
let mut bytes_written = 0;
self.writer.write_all(&program.header.magic)?;
self.writer.write_u32(program.header.flags)?;
self.writer.write_u32(program.header.timestamp)?;
self.writer.write_u32(program.header.size)?;
bytes_written += 16;
let mut marshal_data = Vec::new();
let mut marshal_writer = super::marshal::MarshalWriter::new(&mut marshal_data, program.version);
marshal_writer.write_code_object(&program.code_object).map_err(|e| GaiaError::from(e))?;
self.writer.write_all(&marshal_data)?;
bytes_written += marshal_data.len();
Ok(bytes_written)
}
}