Skip to content

Commit ea5b01d

Browse files
committed
uefi: Refactor ConfigurationString parsing to use string constants
1 parent befe1a3 commit ea5b01d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

uefi/src/proto/hii/config_str.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ pub struct ConfigurationStringElement {
9898
// nvconfig: HashMap<String, Vec<u8>>,
9999
}
100100

101+
/// Internal module with known keys found in uefi configuration strings.
102+
mod keys {
103+
pub const ALTCFG: &str = "ALTCFG";
104+
pub const GUID: &str = "GUID";
105+
pub const NAME: &str = "NAME";
106+
pub const OFFSET: &str = "OFFSET";
107+
pub const VALUE: &str = "VALUE";
108+
pub const WIDTH: &str = "WIDTH";
109+
}
110+
101111
/// A full UEFI Configuration String representation.
102112
///
103113
/// This structure contains routing information such as GUID and device path,
@@ -209,12 +219,12 @@ impl ConfigurationString {
209219
) -> Result<Self, ParseError> {
210220
let guid = Self::try_parse_with(ParseError::ConfigHdr(ConfigHdrSection::Guid), || {
211221
let v = splitter.next()?;
212-
let v = (v.0 == "GUID").then_some(v.1).flatten()?;
222+
let v = (v.0 == keys::GUID).then_some(v.1).flatten()?;
213223
Self::parse_guid_from_hex(v)
214224
})?;
215225
let name = Self::try_parse_with(ParseError::ConfigHdr(ConfigHdrSection::Name), || {
216226
let v = splitter.next()?;
217-
let v = (v.0 == "NAME").then_some(v.1).flatten()?;
227+
let v = (v.0 == keys::NAME).then_some(v.1).flatten()?;
218228
Self::parse_string_from_hex(v)
219229
})?;
220230
let device_path =
@@ -225,7 +235,7 @@ impl ConfigurationString {
225235
Some(v.to_boxed())
226236
})?;
227237
let alt_cfg_id = match splitter.peek() {
228-
Some(("ALTCFG", _)) => Some(Self::try_parse_with(
238+
Some((keys::ALTCFG, _)) => Some(Self::try_parse_with(
229239
ParseError::ConfigHdr(ConfigHdrSection::DescHdr),
230240
|| {
231241
let v = splitter.next()?.1?;
@@ -238,14 +248,14 @@ impl ConfigurationString {
238248
let mut elements = Vec::new();
239249
loop {
240250
let offset = match splitter.next() {
241-
Some(("OFFSET", Some(data))) => {
251+
Some((keys::OFFSET, Some(data))) => {
242252
Self::parse_number_from_hex(data).ok_or(ParseError::BlockName)?
243253
}
244254
None => break,
245255
_ => return Err(ParseError::BlockName),
246256
};
247257
let width = match splitter.next() {
248-
Some(("WIDTH", Some(data))) => {
258+
Some((keys::WIDTH, Some(data))) => {
249259
Self::parse_number_from_hex(data).ok_or(ParseError::BlockName)?
250260
}
251261
_ => return Err(ParseError::BlockName),
@@ -254,12 +264,12 @@ impl ConfigurationString {
254264
// And numbers have to be endianness-corrected. Thus, the bytes represented by a value's
255265
// hex string of arbitrary length have to be reversed to account for that weird encoding.
256266
let value = match splitter.next() {
257-
Some(("VALUE", Some(data))) => Self::parse_bytes_from_hex(data).rev().collect(),
267+
Some((keys::VALUE, Some(data))) => Self::parse_bytes_from_hex(data).rev().collect(),
258268
_ => return Err(ParseError::BlockConfig),
259269
};
260270

261271
while let Some(next) = splitter.peek() {
262-
if next.0 == "OFFSET" || next.0 == "GUID" {
272+
if next.0 == keys::OFFSET || next.0 == keys::GUID {
263273
break;
264274
}
265275
let _ = splitter.next(); // drop nvconfig entries for now
@@ -271,7 +281,7 @@ impl ConfigurationString {
271281
value,
272282
});
273283
// Found start of a new [ConfigurationString]
274-
if let Some(("GUID", _)) = splitter.peek() {
284+
if let Some((keys::GUID, _)) = splitter.peek() {
275285
break;
276286
}
277287
}

0 commit comments

Comments
 (0)