gtk4_macros/
blueprint.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{
4    io::Write,
5    process::{Command, Stdio},
6};
7
8pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result<String, String> {
9    let mut compiler = Command::new("blueprint-compiler")
10        .args(["compile", "-"])
11        .stdin(Stdio::piped())
12        .stdout(Stdio::piped())
13        .spawn()
14        .map_err(|e| format!("blueprint-compiler couldn't be spawned: {e}"))?;
15    let mut stdin = compiler.stdin.take().unwrap();
16    if let Err(e) = stdin.write_all(blueprint) {
17        let _ = compiler.wait();
18        return Err(format!(
19            "Couldn't send blueprint to blueprint-compiler: {e}"
20        ));
21    }
22    drop(stdin);
23
24    let output = compiler
25        .wait_with_output()
26        .map_err(|e| format!("blueprint-compiler process failed: {e}"))?;
27
28    let buf = String::from_utf8(output.stdout).unwrap();
29    if !buf.starts_with('<') {
30        return Err(format!("blueprint-compiler failed: {buf}"));
31    }
32
33    Ok(buf)
34}