python_assembler/formats/pyc/writer/
mod.rs1use crate::{formats::pyc::PycWriteConfig, program::PythonProgram};
2use byteorder::LittleEndian;
3use gaia_types::{BinaryWriter, GaiaError};
4use std::io::Write;
5
6#[derive(Debug)]
8pub struct PycWriter<W> {
9 writer: BinaryWriter<W, LittleEndian>,
10 config: PycWriteConfig,
11}
12
13impl<W: Write> PycWriter<W> {
14 pub fn new(writer: W, config: PycWriteConfig) -> Self {
16 Self { writer: BinaryWriter::new(writer), config }
17 }
18
19 pub fn write(&mut self, program: &PythonProgram) -> Result<usize, GaiaError> {
21 let mut bytes_written = 0;
22
23 self.writer.write_all(&program.header.magic)?;
25 self.writer.write_u32(program.header.flags)?;
26 self.writer.write_u32(program.header.timestamp)?;
27 self.writer.write_u32(program.header.size)?;
28 bytes_written += 16;
29
30 let mut marshal_data = Vec::new();
32 let mut marshal_writer = super::marshal::MarshalWriter::new(&mut marshal_data, program.version);
33 marshal_writer.write_code_object(&program.code_object).map_err(|e| GaiaError::from(e))?;
34
35 self.writer.write_all(&marshal_data)?;
36 bytes_written += marshal_data.len();
37
38 Ok(bytes_written)
39 }
40}