From b155cc73da0388d529930d843667a1a257e31658 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Wed, 18 Oct 2023 21:09:10 +0200 Subject: [PATCH] refactor(efivar): move VariableVendor in its own file --- efivar/src/efi.rs | 7 ++- efivar/src/efi/variable.rs | 87 +-------------------------- efivar/src/efi/variable_vendor.rs | 98 +++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 88 deletions(-) create mode 100644 efivar/src/efi/variable_vendor.rs diff --git a/efivar/src/efi.rs b/efivar/src/efi.rs index dd58c6c2..ef42915a 100644 --- a/efivar/src/efi.rs +++ b/efivar/src/efi.rs @@ -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 diff --git a/efivar/src/efi/variable.rs b/efivar/src/efi/variable.rs index 4a90ae2c..802edc14 100644 --- a/efivar/src/efi/variable.rs +++ b/efivar/src/efi/variable.rs @@ -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 for VariableVendor { - fn from(other: uuid::Uuid) -> Self { - if other == *EFI_GUID { - VariableVendor::Efi - } else { - VariableVendor::Custom(other) - } - } -} - -impl AsRef 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) /// @@ -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()) - ); - } } diff --git a/efivar/src/efi/variable_vendor.rs b/efivar/src/efi/variable_vendor.rs new file mode 100644 index 00000000..be89c190 --- /dev/null +++ b/efivar/src/efi/variable_vendor.rs @@ -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 for VariableVendor { + fn from(other: uuid::Uuid) -> Self { + if other == *EFI_GUID { + VariableVendor::Efi + } else { + VariableVendor::Custom(other) + } + } +} + +impl AsRef 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()) + ); + } +}