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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{
    io::{Error, ErrorKind, Read, Result, Write},
    process::{Command, Stdio},
};

pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result<String> {
    let mut compiler = Command::new("blueprint-compiler")
        .args(["compile", "-"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap_or_else(|_| panic!("blueprint-compiler not found"));

    let mut stdin = compiler.stdin.take().unwrap();
    stdin.write_all(b"using Gtk 4.0;\n")?;
    stdin.write_all(blueprint)?;
    drop(stdin);

    let mut buf = String::new();
    compiler.stdout.unwrap().read_to_string(&mut buf)?;

    if !buf.starts_with('<') {
        return Err(Error::new(ErrorKind::Other, buf));
    }

    Ok(buf)
}