Skip to content

Commit

Permalink
Use strum macros to parse report type
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 13, 2024
1 parent 3fde5b2 commit 565f4eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/dbus_api/api/report_3_0/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
util::{engine_to_dbus_err_tuple, get_next_arg},
},
engine::ReportType,
stratis::StratisError,
};

pub fn get_report(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
Expand All @@ -21,7 +22,9 @@ pub fn get_report(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
let return_message = message.method_return();
let default_return = String::new();

let report_type = match ReportType::try_from(report_name) {
let report_type = match ReportType::try_from(report_name)
.map_err(|_| StratisError::Msg(format!("Report name {report_name} not understood")))
{
Ok(rt) => rt,
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
Expand Down
16 changes: 3 additions & 13 deletions src/engine/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
use libudev::EventType;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum_macros::EnumString;
use uuid::Uuid;

pub use crate::engine::{
Expand Down Expand Up @@ -206,23 +207,12 @@ impl fmt::Display for Name {
///
/// * `ErroredPoolDevices` returns the state of devices that caused an error while
/// attempting to reconstruct a pool.
#[derive(EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum ReportType {
StoppedPools,
}

impl<'a> TryFrom<&'a str> for ReportType {
type Error = StratisError;

fn try_from(name: &str) -> StratisResult<ReportType> {
match name {
"stopped_pools" => Ok(ReportType::StoppedPools),
_ => Err(StratisError::Msg(format!(
"Report name {name} not understood"
))),
}
}
}

#[derive(Debug, Eq, PartialEq)]
pub struct PoolDevice {
pub devnode: PathBuf,
Expand Down

0 comments on commit 565f4eb

Please sign in to comment.