kaiju_compiler_cli_core/
lib.rs

1extern crate kaiju_compiler_core as compiler_core;
2extern crate kaiju_core as core;
3extern crate libloading;
4#[macro_use]
5extern crate lazy_static;
6extern crate relative_path;
7
8pub mod external_deep_validator;
9pub mod fs_module_reader;
10
11use crate::core::assembly::*;
12use crate::core::error::*;
13use crate::core::program::*;
14use crate::core::validator::*;
15use crate::fs_module_reader::*;
16use std::fs::{read_to_string, write};
17
18pub fn load_opdescs(paths: &[String]) -> SimpleResult<OpsDescriptor> {
19    if paths.is_empty() {
20        Ok(OpsDescriptor::default())
21    } else {
22        let mut descs = vec![];
23        for path in paths {
24            match read_to_string(&path) {
25                Ok(desc) => match compile_ops_descriptor(&desc) {
26                    Ok(desc) => descs.push(desc),
27                    Err(err) => {
28                        return Err(SimpleError::new(format!("{:?}: {}", path, err.pretty)))
29                    }
30                },
31                Err(err) => return Err(SimpleError::new(format!("{:?}: {}", path, err))),
32            }
33        }
34        Ok(OpsDescriptor::merge(&descs))
35    }
36}
37
38pub fn compile_program<V>(input: &str, opsdesc: &OpsDescriptor) -> SimpleResult<Program>
39where
40    V: DeepValidator,
41{
42    compiler_core::compile_program::<V, _>(input, FsModuleReader::default(), opsdesc)
43}
44
45pub fn compile_program_and_write_pst<V>(
46    input: &str,
47    output: &str,
48    opsdesc: &OpsDescriptor,
49    pretty: bool,
50) -> SimpleResult<()>
51where
52    V: DeepValidator,
53{
54    let program = compile_program::<V>(input, opsdesc)?;
55    match program.to_json(pretty) {
56        Ok(json) => {
57            if let Err(err) = write(output, &json) {
58                Err(SimpleError::new(format!("{:?}: {}", output, err)))
59            } else {
60                Ok(())
61            }
62        }
63        Err(err) => Err(SimpleError::new(format!("{:?}: {}", output, err))),
64    }
65}
66
67pub fn compile_program_and_write_bin<V>(
68    input: &str,
69    output: &str,
70    opsdesc: &OpsDescriptor,
71) -> SimpleResult<()>
72where
73    V: DeepValidator,
74{
75    let program = compile_program::<V>(input, opsdesc)?;
76    match encode_assembly(&program, opsdesc) {
77        Ok(bytes) => {
78            if let Err(err) = write(output, &bytes) {
79                Err(SimpleError::new(format!("{:?}: {}", output, err)))
80            } else {
81                Ok(())
82            }
83        }
84        Err(err) => Err(SimpleError::new(format!("{:?}: {}", output, err.message))),
85    }
86}