Skip to content

Commit

Permalink
Use strum for StratSigblockVersion methods
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 14, 2024
1 parent 83eef66 commit c55aab3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/engine/strat_engine/metadata/static_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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])?)?;
Expand Down
28 changes: 3 additions & 25 deletions src/engine/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<u8> for StratSigblockVersion {
type Error = StratisError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1u8 => Ok(StratSigblockVersion::V1),
2u8 => Ok(StratSigblockVersion::V2),
_ => Err(StratisError::Msg(format!(
"Unknown sigblock version: {value}"
))),
}
}
}

impl From<StratSigblockVersion> for u8 {
fn from(version: StratSigblockVersion) -> Self {
match version {
StratSigblockVersion::V1 => 1u8,
StratSigblockVersion::V2 => 2u8,
}
}
}

0 comments on commit c55aab3

Please sign in to comment.