Skip to content

Commit

Permalink
Use strum macros to parse UnlockMethod
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 565f4eb commit 50abdd9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
6 changes: 4 additions & 2 deletions src/bin/stratis-min/stratis-min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use stratisd::{
CLEVIS_TANG_TRUST_URL,
},
jsonrpc::client::{filesystem, key, pool, report},
stratis::VERSION,
stratis::{StratisError, VERSION},
};

fn parse_args() -> Command {
Expand Down Expand Up @@ -237,7 +237,9 @@ fn main() -> Result<(), String> {
};
let unlock_method =
match args.get_one::<String>("unlock_method").map(|s| s.as_str()) {
Some(um) => Some(UnlockMethod::try_from(um)?),
Some(um) => Some(UnlockMethod::try_from(um).map_err(|_| {
StratisError::Msg(format!("{um} is an invalid unlock method"))
})?),
None => None,
};
let prompt = args.get_flag("prompt");
Expand Down
4 changes: 3 additions & 1 deletion src/dbus_api/api/manager_3_0/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ pub fn unlock_pool(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
};
let unlock_method = {
let unlock_method_str: &str = get_next_arg(&mut iter, 1)?;
match UnlockMethod::try_from(unlock_method_str) {
match UnlockMethod::try_from(unlock_method_str).map_err(|_| {
StratisError::Msg(format!("{unlock_method_str} is an invalid unlock method"))
}) {
Ok(um) => um,
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
Expand Down
16 changes: 10 additions & 6 deletions src/dbus_api/api/manager_3_2/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ pub fn start_pool(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
let unlock_method = {
let unlock_method_tup: (bool, &str) = get_next_arg(&mut iter, 1)?;
match tuple_to_option(unlock_method_tup) {
Some(unlock_method_str) => match UnlockMethod::try_from(unlock_method_str) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
Some(unlock_method_str) => {
match UnlockMethod::try_from(unlock_method_str).map_err(|_| {
StratisError::Msg(format!("{unlock_method_str} is an invalid unlock method"))
}) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
}
}
},
}
None => None,
}
};
Expand Down
16 changes: 10 additions & 6 deletions src/dbus_api/api/manager_3_4/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ pub fn start_pool(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
let unlock_method = {
let unlock_method_tup: (bool, &str) = get_next_arg(&mut iter, 2)?;
match tuple_to_option(unlock_method_tup) {
Some(unlock_method_str) => match UnlockMethod::try_from(unlock_method_str) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
Some(unlock_method_str) => {
match UnlockMethod::try_from(unlock_method_str).map_err(|_| {
StratisError::Msg(format!("{unlock_method_str} is an invalid unlock method"))
}) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
}
}
},
}
None => None,
}
};
Expand Down
16 changes: 10 additions & 6 deletions src/dbus_api/api/manager_3_8/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ pub fn start_pool(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
let unlock_method = {
let unlock_method_tup: (bool, &str) = get_next_arg(&mut iter, 2)?;
match tuple_to_option(unlock_method_tup) {
Some(unlock_method_str) => match UnlockMethod::try_from(unlock_method_str) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
Some(unlock_method_str) => {
match UnlockMethod::try_from(unlock_method_str).map_err(|_| {
StratisError::Msg(format!("{unlock_method_str} is an invalid unlock method"))
}) {
Ok(um) => Some(um),
Err(e) => {
let (rc, rs) = engine_to_dbus_err_tuple(&e);
return Ok(vec![return_message.append3(default_return, rc, rs)]);
}
}
},
}
None => None,
}
};
Expand Down
18 changes: 2 additions & 16 deletions src/engine/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,14 @@ impl Display for StratisUuid {
}

/// Use Clevis or keyring to unlock LUKS volume.
#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, Debug, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum UnlockMethod {
Clevis,
Keyring,
Any,
}

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

fn try_from(s: &str) -> StratisResult<UnlockMethod> {
match s {
"keyring" => Ok(UnlockMethod::Keyring),
"clevis" => Ok(UnlockMethod::Clevis),
"any" => Ok(UnlockMethod::Any),
_ => Err(StratisError::Msg(format!(
"{s} is an invalid unlock method"
))),
}
}
}

/// Blockdev tier. Used to distinguish between blockdevs used for
/// data and blockdevs used for a cache.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit 50abdd9

Please sign in to comment.