1mod driver;
9
10pub use brink_ir::FileId;
11
12use brink_format::StoryData;
13use brink_ir::Diagnostic;
14use std::io;
15use std::path::Path;
16
17#[derive(Debug)]
19pub struct CompileOutput {
20 pub data: StoryData,
21 pub warnings: Vec<Diagnostic>,
22}
23
24pub struct LirOutput {
26 pub program: brink_ir::lir::Program,
27 pub warnings: Vec<Diagnostic>,
28}
29
30pub fn compile_path(path: &Path) -> Result<CompileOutput, CompileError> {
35 compile(path.to_string_lossy().as_ref(), |p| {
36 std::fs::read_to_string(p).map_err(|e| io::Error::new(e.kind(), format!("{p}: {e}")))
37 })
38}
39
40pub fn compile<F>(entry: &str, read_file: F) -> Result<CompileOutput, CompileError>
46where
47 F: FnMut(&str) -> Result<String, io::Error>,
48{
49 driver::compile(entry, read_file)
50}
51
52pub fn compile_to_json<F>(entry: &str, read_file: F) -> Result<brink_json::InkJson, CompileError>
56where
57 F: FnMut(&str) -> Result<String, io::Error>,
58{
59 let lir_output = driver::compile_to_lir(entry, read_file)?;
60 Ok(brink_codegen_json::emit(&lir_output.program))
61}
62
63pub fn compile_string_to_json(source: &str) -> Result<brink_json::InkJson, CompileError> {
65 compile_to_json("<string>", |_| Ok(source.to_string()))
66}
67
68#[derive(Debug, thiserror::Error)]
70pub enum CompileError {
71 #[error("I/O error: {0}")]
73 Io(#[from] io::Error),
74 #[error("{} diagnostic(s) prevented compilation", .0.len())]
76 Diagnostics(Vec<Diagnostic>),
77 #[error("circular INCLUDE dependency: {0}")]
79 CircularInclude(String),
80}
81
82impl From<brink_driver::DiscoverError> for CompileError {
83 fn from(err: brink_driver::DiscoverError) -> Self {
84 match err {
85 brink_driver::DiscoverError::Io(e) => Self::Io(e),
86 brink_driver::DiscoverError::CircularInclude(msg) => Self::CircularInclude(msg),
87 }
88 }
89}