introduce the use of anyhow to no longer use panic

This commit is contained in:
2025-10-11 14:35:29 -07:00
parent 449eb85ab8
commit fc239ea54d
14 changed files with 121 additions and 84 deletions

View File

@@ -1,3 +1,4 @@
use anyhow::{Context, Result};
use uefi::CString16;
use uefi::fs::{FileSystem, Path};
use uefi::proto::device_path::text::{AllowShortcuts, DevicePathFromText, DisplayOnly};
@@ -6,28 +7,30 @@ use uefi::proto::media::fs::SimpleFileSystem;
pub mod framebuffer;
pub fn text_to_device_path(path: &str) -> PoolDevicePath {
let path = CString16::try_from(path).expect("unable to convert path to CString16");
pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
let device_path_from_text = uefi::boot::open_protocol_exclusive::<DevicePathFromText>(
uefi::boot::get_handle_for_protocol::<DevicePathFromText>()
.expect("no device path from text protocol"),
.context("no device path from text protocol")?,
)
.expect("unable to open device path from text protocol");
.context("unable to open device path from text protocol")?;
device_path_from_text
.convert_text_to_device_path(&path)
.expect("unable to convert text to device path")
.context("unable to convert text to device path")
}
pub fn device_path_root(path: &DevicePath) -> String {
pub fn device_path_root(path: &DevicePath) -> Result<String> {
let mut path = path
.node_iter()
.filter_map(|item| {
let item = item
.to_string(DisplayOnly(false), AllowShortcuts(false))
.expect("unable to convert device path to string");
if item.to_string().contains("(") {
Some(item)
let item = item.to_string(DisplayOnly(false), AllowShortcuts(false));
if item
.as_ref()
.map(|item| item.to_string().contains("("))
.unwrap_or(false)
{
Some(item.unwrap_or_default())
} else {
None
}
@@ -36,16 +39,17 @@ pub fn device_path_root(path: &DevicePath) -> String {
.collect::<Vec<_>>()
.join("/");
path.push('/');
path
Ok(path)
}
pub fn read_file_contents(path: &str) -> Vec<u8> {
pub fn read_file_contents(path: &str) -> Result<Vec<u8>> {
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(
uefi::boot::get_handle_for_protocol::<SimpleFileSystem>().expect("no filesystem protocol"),
uefi::boot::get_handle_for_protocol::<SimpleFileSystem>()
.context("no filesystem protocol")?,
)
.expect("unable to open filesystem protocol");
.context("unable to open filesystem protocol")?;
let mut fs = FileSystem::new(fs);
let path = CString16::try_from(path).expect("unable to convert path to CString16");
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
let content = fs.read(Path::new(&path));
content.expect("unable to read file contents")
content.context("unable to read file contents")
}