From 10941bfb5baddcb7aa6dcc2f4992cb044c13c0bb Mon Sep 17 00:00:00 2001 From: str4d Date: Sun, 1 Jan 2023 18:16:10 +0000 Subject: [PATCH] Add partial `Debug` impls for `Context` and `YubiKey` (#457) This enables `yubikey::Result` to be debug-formatted, for example when wrapping the output of an API method in `dbg!()`. --- CHANGELOG.md | 4 ++++ src/reader.rs | 7 +++++++ src/yubikey.rs | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5bec87..683add6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Added +- `impl Debug for {Context, YubiKey}` + ## 0.7.0 (2022-11-14) ### Added - Display inner PC/SC errors ([#420]) diff --git a/src/reader.rs b/src/reader.rs index b3bbee7..caa5cf7 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -4,6 +4,7 @@ use crate::{Result, YubiKey}; use std::{ borrow::Cow, ffi::CStr, + fmt, sync::{Arc, Mutex}, }; @@ -19,6 +20,12 @@ pub struct Context { reader_names: Vec, } +impl fmt::Debug for Context { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Context").finish_non_exhaustive() + } +} + impl Context { /// Open a PC/SC context, which can be used to enumerate available PC/SC /// readers (which can be used to connect to YubiKeys). diff --git a/src/yubikey.rs b/src/yubikey.rs index 8ae98a6..9e93298 100644 --- a/src/yubikey.rs +++ b/src/yubikey.rs @@ -156,6 +156,16 @@ pub struct YubiKey { pub(crate) serial: Serial, } +impl fmt::Debug for YubiKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("YubiKey") + .field("name", &self.name) + .field("version", &self.version) + .field("serial", &self.serial) + .finish_non_exhaustive() + } +} + impl YubiKey { /// Open a connection to a YubiKey. ///