From c55aab30cdbe60732b163424244259152330a411 Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 13 Nov 2024 20:11:52 -0500 Subject: [PATCH] Use strum for StratSigblockVersion methods Signed-off-by: mulhern --- .../strat_engine/metadata/static_header.rs | 5 ++-- src/engine/types/mod.rs | 28 ++----------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/engine/strat_engine/metadata/static_header.rs b/src/engine/strat_engine/metadata/static_header.rs index aeb8f45cd8..195562d1b7 100644 --- a/src/engine/strat_engine/metadata/static_header.rs +++ b/src/engine/strat_engine/metadata/static_header.rs @@ -497,7 +497,7 @@ impl StaticHeader { let mut buf = [0u8; bytes!(static_header_size::SIGBLOCK_SECTORS)]; buf[4..20].clone_from_slice(STRAT_MAGIC); LittleEndian::write_u64(&mut buf[20..28], *self.blkdev_size.sectors()); - buf[28] = u8::from(self.sigblock_version); + buf[28] = self.sigblock_version as u8; buf[32..64].clone_from_slice(uuid_to_string!(self.identifiers.pool_uuid).as_bytes()); buf[64..96].clone_from_slice(uuid_to_string!(self.identifiers.device_uuid).as_bytes()); LittleEndian::write_u64(&mut buf[96..104], *self.mda_size.sectors()); @@ -532,7 +532,8 @@ impl StaticHeader { let blkdev_size = BlockdevSize::new(Sectors(LittleEndian::read_u64(&buf[20..28]))); let version_buf = buf[28]; - let version = StratSigblockVersion::try_from(version_buf)?; + let version = StratSigblockVersion::from_repr(version_buf) + .ok_or_else(|| StratisError::Msg(format!("Unknown sigblock version: {version_buf}")))?; let pool_uuid = PoolUuid::parse_str(from_utf8(&buf[32..64])?)?; let dev_uuid = DevUuid::parse_str(from_utf8(&buf[64..96])?)?; diff --git a/src/engine/types/mod.rs b/src/engine/types/mod.rs index ed1b16b27d..1909d6487d 100644 --- a/src/engine/types/mod.rs +++ b/src/engine/types/mod.rs @@ -16,7 +16,7 @@ use std::{ use libudev::EventType; use serde::{Deserialize, Serialize}; use serde_json::Value; -use strum_macros::{self, EnumString}; +use strum_macros::{self, EnumString, FromRepr}; use uuid::Uuid; pub use crate::engine::{ @@ -475,31 +475,9 @@ impl UuidOrConflict { } } -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, FromRepr)] +#[repr(u8)] pub enum StratSigblockVersion { V1 = 1, V2 = 2, } - -impl TryFrom for StratSigblockVersion { - type Error = StratisError; - - fn try_from(value: u8) -> Result { - match value { - 1u8 => Ok(StratSigblockVersion::V1), - 2u8 => Ok(StratSigblockVersion::V2), - _ => Err(StratisError::Msg(format!( - "Unknown sigblock version: {value}" - ))), - } - } -} - -impl From for u8 { - fn from(version: StratSigblockVersion) -> Self { - match version { - StratSigblockVersion::V1 => 1u8, - StratSigblockVersion::V2 => 2u8, - } - } -}