Skip to content

Commit ff5dbb0

Browse files
sdroegebilelmoussaoui
authored andcommitted
gtk4-macros: Wait for blueprint-compiler process in all branches
Otherwise clippy complains and zombie processes might stay around. Also make sure to never panic on normal errors for the same reason, and provide more useful error messages.
1 parent c2f1051 commit ff5dbb0

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

gtk4-macros/src/blueprint.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use std::{
4-
io::{Error, ErrorKind, Read, Result, Write},
4+
io::Write,
55
process::{Command, Stdio},
66
};
77

8-
pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result<String> {
8+
pub(crate) fn compile_blueprint(blueprint: &[u8]) -> Result<String, String> {
99
let mut compiler = Command::new("blueprint-compiler")
1010
.args(["compile", "-"])
1111
.stdin(Stdio::piped())
1212
.stdout(Stdio::piped())
1313
.spawn()
14-
.unwrap_or_else(|_| panic!("blueprint-compiler not found"));
15-
14+
.map_err(|e| format!("blueprint-compiler couldn't be spawned: {e}"))?;
1615
let mut stdin = compiler.stdin.take().unwrap();
17-
stdin.write_all(b"using Gtk 4.0;\n")?;
18-
stdin.write_all(blueprint)?;
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+
}
1925
drop(stdin);
2026

21-
let mut buf = String::new();
22-
compiler.stdout.unwrap().read_to_string(&mut buf)?;
27+
let output = compiler
28+
.wait_with_output()
29+
.map_err(|e| format!("blueprint-compiler process failed: {e}"))?;
2330

31+
let buf = String::from_utf8(output.stdout).unwrap();
2432
if !buf.starts_with('<') {
25-
return Err(Error::new(ErrorKind::Other, buf));
33+
return Err(format!("blueprint-compiler failed: {buf}"));
2634
}
2735

2836
Ok(buf)

0 commit comments

Comments
 (0)