Skip to content

Commit b4adc26

Browse files
committed
Refactor print_mode code
1 parent f0e87dd commit b4adc26

File tree

8 files changed

+90
-3
lines changed

8 files changed

+90
-3
lines changed

meson.build

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@ lib_refivar = static_library(
5757
lib_refivar_efi_guids_list_path_rs
5858
],
5959
{
60+
'print_mode': [
61+
'src/lib/efivar/print_mode/decimal.rs',
62+
'src/lib/efivar/print_mode/mod.rs',
63+
'src/lib/efivar/print_mode/verbose.rs',
64+
],
6065
'types': [
6166
'src/lib/efivar/types/efi_guid_error.rs',
6267
'src/lib/efivar/types/efi_guid_list_entry.rs',
6368
'src/lib/efivar/types/efi_guid.rs',
6469
'src/lib/efivar/types/efi_variable_attribute.rs',
6570
'src/lib/efivar/types/efi_variable.rs',
66-
'src/lib/efivar/types/mod.rs'
71+
'src/lib/efivar/types/mod.rs',
72+
'src/lib/efivar/types/print_mode.rs',
6773
],
6874
}
6975
),

src/lib/efivar/efi_guids.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::efi_guids_list_path;
12
use crate::types::EfiGuid;
23
use crate::types::EfiGuidListEntry;
34
use serde::Deserialize;
45
use std::collections::HashMap;
56
use std::fs::File;
67
use std::io::{BufReader, Error};
78
use std::str::FromStr;
8-
use crate::efi_guids_list_path;
99

1010
pub const DEFAULT_GUIDS_LIST_PATH: &'static str = efi_guids_list_path::VALUE;
1111

@@ -28,7 +28,7 @@ struct JsonEfiGuidListEntry {
2828

2929
impl Default for EfiGuidList {
3030
fn default() -> Self {
31-
EfiGuidList { guids_map: None }
31+
return EfiGuidList { guids_map: None };
3232
}
3333
}
3434

src/lib/efivar/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod efi_guids;
22
pub mod efivar_display;
3+
pub mod print_mode;
34
pub mod types;
45

56
mod efi_guids_list_path;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::types::EfiVariable;
2+
use std::fmt;
3+
4+
pub struct Decimal<'a>(pub &'a EfiVariable<'a>);
5+
6+
impl fmt::Display for Decimal<'_> {
7+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8+
for (i, b) in self.0.data.iter().enumerate() {
9+
if (i > 0) && (i % 16 == 0) {
10+
f.write_str(" ")?;
11+
}
12+
if i % 16 < 8 {
13+
f.write_str(&format!("{} ", b))?;
14+
} else {
15+
f.write_str(&format!(" {}", b))?;
16+
}
17+
}
18+
return Ok(());
19+
}
20+
}

src/lib/efivar/print_mode/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod decimal;
2+
mod verbose;
3+
4+
pub use self::decimal::Decimal;
5+
pub use self::verbose::Verbose;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::types::{EfiVariable, EfiVariableAttribute};
2+
use std::fmt;
3+
4+
pub struct Verbose<'a>(pub &'a EfiVariable<'a>);
5+
6+
impl fmt::Display for Verbose<'_> {
7+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8+
f.write_str(&format!("GUID: {}\n", self.0.guid))?;
9+
f.write_str(&format!("Name: \"{}\"\n", self.0.name))?;
10+
f.write_str("Attributes:\n")?;
11+
let mut sorted_attrs: Vec<&EfiVariableAttribute> =
12+
self.0.attributes.clone().into_iter().collect();
13+
sorted_attrs.sort_unstable_by(|a1, a2| <u32>::from(*a1).cmp(&<u32>::from(*a2)));
14+
for a in sorted_attrs.iter() {
15+
f.write_str(&format!("\t{}\n", String::from(*a)))?;
16+
}
17+
18+
f.write_str("Value:\n")?;
19+
for i in (0..self.0.data.len()).step_by(16) {
20+
f.write_str(&format!("{:08x} ", i))?;
21+
22+
let mut decode: [u8; 16] = [0; 16];
23+
let mut line_iter = self.0.data.iter().skip(i);
24+
for j in i..i + 16 {
25+
match line_iter.next() {
26+
Some(c) => {
27+
if (0x20 < *c) && (*c < 0x7e) {
28+
decode[j - i] = u32::from(*c) as u8;
29+
} else {
30+
decode[j - i] = u32::from('.') as u8;
31+
}
32+
if j < i + 8 {
33+
f.write_str(&format!("{:02x} ", u8::from(*c)))?;
34+
} else {
35+
f.write_str(&format!(" {:02x}", u8::from(*c)))?;
36+
}
37+
}
38+
None => {
39+
decode[j - i] = u32::from(' ') as u8;
40+
f.write_str(" ")?;
41+
}
42+
};
43+
}
44+
f.write_str(&format!(" |{}|\n", std::str::from_utf8(&decode).unwrap()))?;
45+
}
46+
f.write_str(&format!("{:08x}\n", self.0.data.len()))?;
47+
return Ok(());
48+
}
49+
}

src/lib/efivar/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ mod efi_guid_error;
33
mod efi_guid_list_entry;
44
mod efi_variable;
55
mod efi_variable_attribute;
6+
mod print_mode;
67

78
pub use self::efi_guid::EfiGuid;
89
pub use self::efi_guid_error::EfiGuidError;
910
pub use self::efi_guid_list_entry::EfiGuidListEntry;
1011
pub use self::efi_variable::EfiVariable;
1112
pub use self::efi_variable_attribute::EfiVariableAttribute;
13+
pub use self::print_mode::PrintMode;

src/lib/efivar/types/print_mode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub enum PrintMode {
2+
VERBOSE,
3+
DECIMAL,
4+
}

0 commit comments

Comments
 (0)