1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
mod instructions;
mod segments;

pub use instructions::{
    DeviceAddress, RelayAction, RelayInstr, Segment, SensorAction, SensorInstr,
};
pub use segments::{Abort, Test, TestBody};

use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;

use crate::bits::BitWriter;
use crate::result::CompilerResult;

/// Takes in a Test Binary data structure representation and emits it to a file.
pub fn emit(test: Test, output_path: Option<PathBuf>) -> CompilerResult<u32> {
    let mut res = CompilerResult::new("Emitting Binary File");

    let mut file = check!(
        res,
        File::create(output_path.unwrap_or_else(|| "./a.tbf".into()))
    );
    let mut buffer = BitWriter::new();
    let crc_hash = test.append_to_buffer(&mut buffer);

    res.require(file.write_all(buffer.as_bytes()));

    res.with_value(crc_hash)
}