summaryrefslogtreecommitdiff
path: root/rust-src/src/key.rs
diff options
context:
space:
mode:
authorFinn Behrens <me@kloenk.de>2023-11-12 17:12:36 +0100
committerKent Overstreet <kent.overstreet@linux.dev>2023-11-12 11:14:16 -0500
commita613340b26ad88801666362d2824118396f34c38 (patch)
treeec64226aa56b7c7cc88e70a6d70dca2496c4871d /rust-src/src/key.rs
parent73da05d983dc1641057aeb3236c254fd275f87c7 (diff)
rust keylocation add none variant and implement ValueEnum
This enables a possible values help in the clap help text. Signed-Off-By: Finn Behrens <me@kloenk.de> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'rust-src/src/key.rs')
-rw-r--r--rust-src/src/key.rs44
1 files changed, 29 insertions, 15 deletions
diff --git a/rust-src/src/key.rs b/rust-src/src/key.rs
index 9319351e..93daa263 100644
--- a/rust-src/src/key.rs
+++ b/rust-src/src/key.rs
@@ -1,37 +1,50 @@
use log::{info};
use bch_bindgen::bcachefs::bch_sb_handle;
+use clap::builder::PossibleValue;
use crate::c_str;
use anyhow::anyhow;
#[derive(Clone, Debug)]
pub enum KeyLocation {
+ None,
Fail,
Wait,
Ask,
}
-#[derive(Clone, Debug)]
-pub struct KeyLoc(pub Option<KeyLocation>);
-impl std::ops::Deref for KeyLoc {
- type Target = Option<KeyLocation>;
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
-
-impl std::str::FromStr for KeyLoc {
+impl std::str::FromStr for KeyLocation {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> {
match s {
- "" => Ok(KeyLoc(None)),
- "fail" => Ok(KeyLoc(Some(KeyLocation::Fail))),
- "wait" => Ok(KeyLoc(Some(KeyLocation::Wait))),
- "ask" => Ok(KeyLoc(Some(KeyLocation::Ask))),
- _ => Err(anyhow!("invalid password option")),
+ ""|"none" => Ok(KeyLocation::None),
+ "fail" => Ok(KeyLocation::Fail),
+ "wait" => Ok(KeyLocation::Wait),
+ "ask" => Ok(KeyLocation::Ask),
+ _ => Err(anyhow!("invalid password option")),
}
}
}
+impl clap::ValueEnum for KeyLocation {
+ fn value_variants<'a>() -> &'a [Self] {
+ &[
+ KeyLocation::None,
+ KeyLocation::Fail,
+ KeyLocation::Wait,
+ KeyLocation::Ask,
+ ]
+ }
+
+ fn to_possible_value(&self) -> Option<PossibleValue> {
+ Some(match self {
+ Self::None => PossibleValue::new("none").alias(""),
+ Self::Fail => PossibleValue::new("fail"),
+ Self::Wait => PossibleValue::new("wait"),
+ Self::Ask => PossibleValue::new("ask"),
+ })
+ }
+}
+
fn check_for_key(key_name: &std::ffi::CStr) -> anyhow::Result<bool> {
use bch_bindgen::keyutils::{self, keyctl_search};
let key_name = key_name.to_bytes_with_nul().as_ptr() as *const _;
@@ -126,5 +139,6 @@ pub fn prepare_key(sb: &bch_sb_handle, password: KeyLocation) -> anyhow::Result<
KeyLocation::Fail => Err(anyhow!("no key available")),
KeyLocation::Wait => Ok(wait_for_key(&sb.sb().uuid())?),
KeyLocation::Ask => ask_for_key(sb),
+ _ => Err(anyhow!("no keyoption specified for locked filesystem")),
}
}