Skip to content

Commit 211415d

Browse files
committed
Remove remaining unwraps in builder module
1 parent c6f5afd commit 211415d

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/builder.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ impl Builder {
9696
});
9797
}
9898
let mut executables = Vec::new();
99-
for line in String::from_utf8(output.stdout).unwrap().lines() {
100-
let mut artifact = json::parse(line).unwrap();
99+
for line in String::from_utf8(output.stdout)
100+
.map_err(BuildKernelError::XbuildJsonOutputInvalidUtf8)?
101+
.lines()
102+
{
103+
let mut artifact =
104+
json::parse(line).map_err(BuildKernelError::XbuildJsonOutputInvalidJson)?;
101105
if let Some(executable) = artifact["executable"].take_string() {
102106
executables.push(PathBuf::from(executable));
103107
}
@@ -233,8 +237,12 @@ impl Builder {
233237
});
234238
}
235239
let mut bootloader_elf_path = None;
236-
for line in String::from_utf8(output.stdout).unwrap().lines() {
237-
let mut artifact = json::parse(line).unwrap();
240+
for line in String::from_utf8(output.stdout)
241+
.map_err(CreateBootimageError::XbuildJsonOutputInvalidUtf8)?
242+
.lines()
243+
{
244+
let mut artifact =
245+
json::parse(line).map_err(CreateBootimageError::XbuildJsonOutputInvalidJson)?;
238246
if let Some(executable) = artifact["executable"].take_string() {
239247
if bootloader_elf_path
240248
.replace(PathBuf::from(executable))
@@ -316,10 +324,8 @@ pub enum BuildKernelError {
316324
XbuildFailed {
317325
stderr: Vec<u8>,
318326
},
319-
CargoConfigInvalid {
320-
path: PathBuf,
321-
error: String,
322-
},
327+
XbuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
328+
XbuildJsonOutputInvalidJson(json::Error),
323329
}
324330

325331
impl fmt::Display for BuildKernelError {
@@ -338,9 +344,12 @@ impl fmt::Display for BuildKernelError {
338344
BuildKernelError::XbuildFailed{stderr} => {
339345
writeln!(f, "Kernel build failed:\n{}", String::from_utf8_lossy(stderr))
340346
}
341-
BuildKernelError::CargoConfigInvalid{path,error} => {
342-
writeln!(f, "Failed to read cargo config at {}:\n{}", path.display(), error)
343-
},
347+
BuildKernelError::XbuildJsonOutputInvalidUtf8(err) => {
348+
writeln!(f, "Output of kernel build with --message-format=json is not valid UTF-8:\n{}", err)
349+
}
350+
BuildKernelError::XbuildJsonOutputInvalidJson(err) => {
351+
writeln!(f, "Output of kernel build with --message-format=json is not valid JSON:\n{}", err)
352+
}
344353
}
345354
}
346355
}
@@ -374,6 +383,8 @@ pub enum CreateBootimageError {
374383
ObjcopyFailed {
375384
stderr: Vec<u8>,
376385
},
386+
XbuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
387+
XbuildJsonOutputInvalidJson(json::Error),
377388
}
378389

379390
impl fmt::Display for CreateBootimageError {
@@ -422,6 +433,16 @@ impl fmt::Display for CreateBootimageError {
422433
"Failed to run `llvm-objcopy`: {}",
423434
String::from_utf8_lossy(stderr)
424435
),
436+
CreateBootimageError::XbuildJsonOutputInvalidUtf8(err) => writeln!(
437+
f,
438+
"Output of bootloader build with --message-format=json is not valid UTF-8:\n{}",
439+
err
440+
),
441+
CreateBootimageError::XbuildJsonOutputInvalidJson(err) => writeln!(
442+
f,
443+
"Output of bootloader build with --message-format=json is not valid JSON:\n{}",
444+
err
445+
),
425446
}
426447
}
427448
}

0 commit comments

Comments
 (0)