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
17        .write_all(b"using Gtk 4.0;\n")
18        .and_then(|_| stdin.write_all(blueprint))
19    {
20        let _ = compiler.wait();
21        return Err(format!(
22            "Couldn't send blueprint to blueprint-compiler: {e}"
23        ));
24    }
25    drop(stdin);
26
27    let output = compiler
28        .wait_with_output()
29        .map_err(|e| format!("blueprint-compiler process failed: {e}"))?;
30
31    let buf = String::from_utf8(output.stdout).unwrap();
32    if !buf.starts_with('<') {
33        return Err(format!("blueprint-compiler failed: {buf}"));
34    }
35
36    Ok(buf)
37}