opgpcard: Restrict values of id of set-identity

This commit is contained in:
Nora Widdecke 2022-10-25 00:28:47 +02:00
parent a7731ec467
commit e81ebd21a0
No known key found for this signature in database
GPG key ID: 2D4111B31DBB99B6
2 changed files with 24 additions and 5 deletions

View file

@ -146,8 +146,8 @@ pub enum Command {
#[clap(name = "card ident", short = 'c', long = "card")] #[clap(name = "card ident", short = 'c', long = "card")]
ident: String, ident: String,
#[clap(name = "identity")] #[clap(name = "identity", value_enum)]
id: u8, id: SetIdentityId,
}, },
} }
@ -358,3 +358,23 @@ impl From<TouchPolicy> for openpgp_card_sequoia::types::TouchPolicy {
} }
} }
} }
#[derive(ValueEnum, Debug, Clone)]
pub enum SetIdentityId {
#[clap(name = "0")]
Zero,
#[clap(name = "1")]
One,
#[clap(name = "2")]
Two,
}
impl From<SetIdentityId> for u8 {
fn from(id: SetIdentityId) -> Self {
match id {
SetIdentityId::Zero => 0,
SetIdentityId::One => 1,
SetIdentityId::Two => 2,
}
}
}

View file

@ -579,13 +579,12 @@ fn list_cards(format: OutputFormat, output_version: OutputVersion) -> Result<()>
Ok(()) Ok(())
} }
fn set_identity(ident: &str, id: u8) -> Result<(), Box<dyn std::error::Error>> { fn set_identity(ident: &str, id: cli::SetIdentityId) -> Result<(), Box<dyn std::error::Error>> {
let backend = util::open_card(ident)?; let backend = util::open_card(ident)?;
let mut card = Card::new(backend); let mut card = Card::new(backend);
let mut open = card.transaction()?; let mut open = card.transaction()?;
open.set_identity(id)?; open.set_identity(u8::from(id))?;
Ok(()) Ok(())
} }