Skip to content

Commit

Permalink
refactor(efivar): move VariableVendor in its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz committed Oct 18, 2023
1 parent 6556c4d commit b155cc7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 88 deletions.
7 changes: 5 additions & 2 deletions efivar/src/efi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::str::FromStr;

mod variable_flags;
pub use variable_flags::*;
pub use variable_flags::VariableFlags;

mod variable;
pub use variable::*;
pub use variable::Variable;

mod variable_vendor;
pub use variable_vendor::VariableVendor;

lazy_static! {
/// Vendor GUID of the EFI variables according to the specification
Expand Down
87 changes: 1 addition & 86 deletions efivar/src/efi/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,9 @@
use std::fmt;
use std::str::FromStr;

use super::EFI_GUID;
use crate::Error;

#[derive(Copy, Clone, Eq)]
/// An EFI variable vendor identifier
pub enum VariableVendor {
/// Standard EFI variables
Efi,
/// Other EFI vendors
Custom(uuid::Uuid),
}

impl VariableVendor {
/// Return true if this vendor is the EFI vendor
pub fn is_efi(&self) -> bool {
matches!(self, VariableVendor::Efi)
}
}

impl PartialEq for VariableVendor {
fn eq(&self, other: &Self) -> bool {
match self {
VariableVendor::Efi => match other {
VariableVendor::Efi => true,
VariableVendor::Custom(uuid) => *uuid == *EFI_GUID,
},
VariableVendor::Custom(uuid) => match other {
VariableVendor::Efi => *uuid == *EFI_GUID,
VariableVendor::Custom(other_uuid) => *other_uuid == *uuid,
},
}
}
}

impl From<uuid::Uuid> for VariableVendor {
fn from(other: uuid::Uuid) -> Self {
if other == *EFI_GUID {
VariableVendor::Efi
} else {
VariableVendor::Custom(other)
}
}
}

impl AsRef<uuid::Uuid> for VariableVendor {
fn as_ref(&self) -> &uuid::Uuid {
match self {
VariableVendor::Efi => &EFI_GUID,
VariableVendor::Custom(uuid) => uuid,
}
}
}

impl fmt::Debug for VariableVendor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.as_ref(), f)
}
}

impl fmt::Display for VariableVendor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_ref(), f)
}
}
use super::variable_vendor::VariableVendor;

/// Represents an EFI variable, with a name and a vendor (namespace)
///
Expand Down Expand Up @@ -243,28 +182,4 @@ mod tests {
assert_eq!(Variable::new("Boot10000").boot_var_id(), None);
assert_eq!(Variable::new("Boot100").boot_var_id(), None);
}

#[test]
fn variable_vendor_eq() {
assert_eq!(VariableVendor::Efi, VariableVendor::Efi);

// idk what the right behaviour would be here
assert_eq!(VariableVendor::Efi, VariableVendor::Custom(*EFI_GUID));
assert_eq!(VariableVendor::Custom(*EFI_GUID), VariableVendor::Efi);

assert_eq!(
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap()),
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap())
);

assert_ne!(
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap()),
VariableVendor::Custom(Uuid::from_str("d3464728-c118-4d88-a450-7ac21a85a099").unwrap())
);

assert_ne!(
VariableVendor::Efi,
VariableVendor::Custom(Uuid::from_str("d3464728-c118-4d88-a450-7ac21a85a099").unwrap())
);
}
}
98 changes: 98 additions & 0 deletions efivar/src/efi/variable_vendor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::fmt;

use super::EFI_GUID;

#[derive(Copy, Clone, Eq)]
/// An EFI variable vendor identifier
pub enum VariableVendor {
/// Standard EFI variables
Efi,
/// Other EFI vendors
Custom(uuid::Uuid),
}

impl VariableVendor {
/// Return true if this vendor is the EFI vendor
pub fn is_efi(&self) -> bool {
matches!(self, VariableVendor::Efi)
}
}

impl PartialEq for VariableVendor {
fn eq(&self, other: &Self) -> bool {
match self {
VariableVendor::Efi => match other {
VariableVendor::Efi => true,
VariableVendor::Custom(uuid) => *uuid == *EFI_GUID,
},
VariableVendor::Custom(uuid) => match other {
VariableVendor::Efi => *uuid == *EFI_GUID,
VariableVendor::Custom(other_uuid) => *other_uuid == *uuid,
},
}
}
}

impl From<uuid::Uuid> for VariableVendor {
fn from(other: uuid::Uuid) -> Self {
if other == *EFI_GUID {
VariableVendor::Efi
} else {
VariableVendor::Custom(other)
}
}
}

impl AsRef<uuid::Uuid> for VariableVendor {
fn as_ref(&self) -> &uuid::Uuid {
match self {
VariableVendor::Efi => &EFI_GUID,
VariableVendor::Custom(uuid) => uuid,
}
}
}

impl fmt::Debug for VariableVendor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.as_ref(), f)
}
}

impl fmt::Display for VariableVendor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_ref(), f)
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use uuid::Uuid;

use crate::efi::{variable_vendor::VariableVendor, EFI_GUID};

#[test]
fn variable_vendor_eq() {
assert_eq!(VariableVendor::Efi, VariableVendor::Efi);

// idk what the right behaviour would be here
assert_eq!(VariableVendor::Efi, VariableVendor::Custom(*EFI_GUID));
assert_eq!(VariableVendor::Custom(*EFI_GUID), VariableVendor::Efi);

assert_eq!(
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap()),
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap())
);

assert_ne!(
VariableVendor::Custom(Uuid::from_str("9acae909-5f29-43c8-b925-30040693bdff").unwrap()),
VariableVendor::Custom(Uuid::from_str("d3464728-c118-4d88-a450-7ac21a85a099").unwrap())
);

assert_ne!(
VariableVendor::Efi,
VariableVendor::Custom(Uuid::from_str("d3464728-c118-4d88-a450-7ac21a85a099").unwrap())
);
}
}

0 comments on commit b155cc7

Please sign in to comment.