Skip to content

Commit 8b4c62c

Browse files
committed
Update EfiGuidList::load() return type
Update EfiGuidList::load() return type to Result<(), Error> as this simplies the implementation of load() somewhat.
1 parent b39270b commit 8b4c62c

File tree

2 files changed

+41
-46
lines changed

2 files changed

+41
-46
lines changed

src/bin/efivar.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,24 @@ fn main() -> ExitCode {
111111
let matches = create_parser().get_matches();
112112
if matches.get_flag("list-guids") {
113113
let mut guid_list: efi_guids::EfiGuidList = Default::default();
114-
guid_list.load(matches.get_one("guids-list-path").unwrap());
115-
for g in guid_list.guids(efi_guids::GuidListSortField::Guid) {
116-
println!("{}", g);
117-
}
118-
println!("");
119-
for g in guid_list.guids(efi_guids::GuidListSortField::Id) {
120-
println!("{}", g);
121-
}
122-
println!("");
123-
for g in guid_list.guids(efi_guids::GuidListSortField::None) {
124-
println!("{}", g);
114+
match guid_list.load(matches.get_one("guids-list-path").unwrap()) {
115+
Ok(()) => {
116+
for g in guid_list.guids(efi_guids::GuidListSortField::Guid) {
117+
println!("{}", g);
118+
}
119+
println!("");
120+
for g in guid_list.guids(efi_guids::GuidListSortField::Id) {
121+
println!("{}", g);
122+
}
123+
println!("");
124+
for g in guid_list.guids(efi_guids::GuidListSortField::None) {
125+
println!("{}", g);
126+
}
127+
}
128+
Err(e) => {
129+
eprintln!("Failed to read GUIDs list file: {}", e);
130+
return std::process::ExitCode::from(1);
131+
}
125132
}
126133
}
127134
return std::process::ExitCode::from(0);

src/lib/efivar/efi_guids.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::types::EfiGuidListEntry;
33
use serde::Deserialize;
44
use std::collections::HashMap;
55
use std::fs::File;
6-
use std::io::BufReader;
6+
use std::io::{BufReader, Error};
77
use std::str::FromStr;
88

99
pub const DEFAULT_GUIDS_LIST_PATH: &'static str = env!("GUIDS_LIST_PATH");
@@ -32,44 +32,31 @@ impl Default for EfiGuidList {
3232
}
3333

3434
impl EfiGuidList {
35-
pub fn load(&mut self, path: &String) -> () {
35+
pub fn load(&mut self, path: &String) -> Result<(), Error> {
3636
let mut map: HashMap<String, EfiGuidListEntry> = HashMap::new();
37-
match File::open(path) {
38-
Ok(h) => {
39-
let reader = BufReader::new(h);
40-
let result: serde_json::Result<Vec<JsonEfiGuidListEntry>> =
41-
serde_json::from_reader(reader);
42-
match result {
43-
Ok(v) => {
44-
for entry in v {
45-
match EfiGuid::from_str(&entry.guid) {
46-
Ok(g) => {
47-
map.insert(
48-
entry.name.clone(),
49-
EfiGuidListEntry {
50-
guid: g,
51-
name: entry.name.clone(),
52-
description: entry.description,
53-
},
54-
);
55-
}
56-
Err(_) => {
57-
eprintln!(
58-
"Entry with UUID: {} invalid. Skipping...",
59-
entry.guid
60-
);
61-
}
62-
};
37+
let reader = BufReader::new(File::open(path)?);
38+
let result: serde_json::Result<Vec<JsonEfiGuidListEntry>> = serde_json::from_reader(reader);
39+
match result {
40+
Ok(v) => {
41+
for entry in v {
42+
match EfiGuid::from_str(&entry.guid) {
43+
Ok(g) => {
44+
map.insert(
45+
entry.name.clone(),
46+
EfiGuidListEntry {
47+
guid: g,
48+
name: entry.name.clone(),
49+
description: entry.description,
50+
},
51+
);
6352
}
64-
}
65-
Err(e) => {
66-
eprintln!("Failed to read GUIDs list file: {}", e);
67-
}
53+
Err(_) => {
54+
eprintln!("Entry with UUID: {} invalid. Skipping...", entry.guid);
55+
}
56+
};
6857
}
6958
}
70-
Err(e) => {
71-
eprintln!("Failed to open GUIDs list file: {}", e);
72-
}
59+
Err(e) => return Err(e.into()),
7360
}
7461
map.insert(
7562
"zero".to_string(),
@@ -81,6 +68,7 @@ impl EfiGuidList {
8168
);
8269

8370
self.guids_map = Some(map);
71+
Ok(())
8472
}
8573

8674
pub fn guids(&self, sorted_by: GuidListSortField) -> Vec<&EfiGuidListEntry> {

0 commit comments

Comments
 (0)