From bdde317a2d042e3bda3c000619407d618960f2cc Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Fri, 11 Feb 2022 10:34:42 +0100 Subject: [PATCH] Simplify optional $reselect parameter in get_txc!() --- card-functionality/src/list-cards.rs | 4 ++-- card-functionality/src/tests.rs | 5 ++--- openpgp-card-sequoia/src/main.rs | 10 +++++----- pcsc/src/lib.rs | 13 +++++-------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/card-functionality/src/list-cards.rs b/card-functionality/src/list-cards.rs index 87fe392..f63aae0 100644 --- a/card-functionality/src/list-cards.rs +++ b/card-functionality/src/list-cards.rs @@ -3,14 +3,14 @@ use anyhow::Result; -use openpgp_card_pcsc::{PcscCard, TxClient}; +use openpgp_card_pcsc::{get_txc, PcscCard, TxClient}; use openpgp_card_sequoia::card::Open; fn main() -> Result<()> { println!("The following OpenPGP cards are connected to your system:"); for mut card in PcscCard::cards(None)? { - let mut txc: TxClient = openpgp_card_pcsc::get_txc!(card)?; + let mut txc: TxClient = get_txc!(card)?; let open = Open::new(&mut txc)?; println!(" {}", open.application_identifier()?.ident()); diff --git a/card-functionality/src/tests.rs b/card-functionality/src/tests.rs index 29ea7cd..922415b 100644 --- a/card-functionality/src/tests.rs +++ b/card-functionality/src/tests.rs @@ -16,7 +16,7 @@ use openpgp_card; use openpgp_card::algorithm::AlgoSimple; use openpgp_card::card_do::{KeyGenerationTime, Sex}; use openpgp_card::{CardClient, Error, KeyType, StatusBytes}; -use openpgp_card_pcsc::TxClient; +use openpgp_card_pcsc::{get_txc, TxClient}; use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::util::{ make_cert, public_key_material_to_key, public_to_fingerprint, @@ -682,8 +682,7 @@ pub fn run_test( use anyhow::anyhow; - let mut txc = - openpgp_card_pcsc::get_txc!(card_client).map_err(|e| anyhow!(e))?; + let mut txc = get_txc!(card_client).map_err(|e| anyhow!(e))?; // let mut txc = TxClient::new(&mut tx, card_caps, reader_caps); t(&mut txc, param) diff --git a/openpgp-card-sequoia/src/main.rs b/openpgp-card-sequoia/src/main.rs index aa62264..fedfa70 100644 --- a/openpgp-card-sequoia/src/main.rs +++ b/openpgp-card-sequoia/src/main.rs @@ -11,7 +11,7 @@ use sequoia_openpgp::Cert; use openpgp_card::card_do::Sex; use openpgp_card::KeyType; -use openpgp_card_pcsc::{PcscCard, TxClient}; +use openpgp_card_pcsc::{get_txc, PcscCard, TxClient}; use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::sq_util; @@ -36,7 +36,7 @@ fn main() -> Result<(), Box> { if let Ok(test_card_ident) = test_card_ident { let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; - let mut txc = openpgp_card_pcsc::get_txc!(card)?; + let mut txc = get_txc!(card)?; let mut open = Open::new(&mut txc)?; @@ -147,7 +147,7 @@ fn main() -> Result<(), Box> { // Open fresh Card for decrypt // ----------------------------- let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; - let mut txc = openpgp_card_pcsc::get_txc!(card)?; + let mut txc = get_txc!(card)?; let mut open = Open::new(&mut txc)?; @@ -188,7 +188,7 @@ fn main() -> Result<(), Box> { // Open fresh Card for signing // ----------------------------- let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; - let mut txc = openpgp_card_pcsc::get_txc!(card)?; + let mut txc = get_txc!(card)?; let mut open = Open::new(&mut txc)?; @@ -220,7 +220,7 @@ fn main() -> Result<(), Box> { println!("The following OpenPGP cards are connected to your system:"); for mut card in PcscCard::cards(None)? { - let mut txc = openpgp_card_pcsc::get_txc!(card)?; + let mut txc = get_txc!(card)?; let open = Open::new(&mut txc)?; println!(" {}", open.application_identifier()?.ident()); diff --git a/pcsc/src/lib.rs b/pcsc/src/lib.rs index 43994c4..6734724 100644 --- a/pcsc/src/lib.rs +++ b/pcsc/src/lib.rs @@ -20,16 +20,10 @@ const FEATURE_MODIFY_PIN_DIRECT: u8 = 0x07; /// in PcscCard) #[macro_export] macro_rules! get_txc { - ($card:expr $(, $reselect:expr)? ) => {{ + ( $card:expr, $reselect:expr ) => {{ use openpgp_card::{Error, SmartcardError}; use pcsc::{Disposition, Protocols}; - #[allow(unused_assignments)] - let mut reselect = true; - $( - reselect = $reselect; - )? - let mut was_reset = false; let card_caps = $card.card_caps(); @@ -59,7 +53,7 @@ macro_rules! get_txc { // For initial card-opening, we don't do this, then // the caller always expects a card that has not // been "select"ed yet. - if reselect { + if $reselect { TxClient::select(&mut txc)?; } @@ -105,6 +99,9 @@ macro_rules! get_txc { }; } }}; + ( $card:expr ) => { + get_txc!($card, true) + }; } fn default_mode(mode: Option) -> ShareMode {