From 01fb9e579de1c019ed718e04f9b592f2687f7040 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sun, 20 Jan 2019 06:42:39 -0800 Subject: Return Cow object from SecretEntry methods We do not know what kind of data future implementers of the SecretEntry trait may want to return. For all we know these could just be static strings, in which case the forced conversion into a String by virtue of the return type is wasteful. To be more flexible in the future while gaining some consistency, this change makes all those trait's methods return a Cow object instead. --- nitrocli/src/pinentry.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/nitrocli/src/pinentry.rs b/nitrocli/src/pinentry.rs index f7a400c..68cc4aa 100644 --- a/nitrocli/src/pinentry.rs +++ b/nitrocli/src/pinentry.rs @@ -17,12 +17,15 @@ // * along with this program. If not, see . * // ************************************************************************* +use std::borrow; use std::fmt; use std::process; use std::str; use crate::error::Error; +type CowStr = borrow::Cow<'static, str>; + /// PIN type requested from pinentry. /// /// The available PIN types correspond to the PIN types used by the Nitrokey devices: user and @@ -35,11 +38,11 @@ Enum! {PinType, [ /// A trait representing a secret to be entered by the user. pub trait SecretEntry: fmt::Debug { /// The cache ID to use for this secret. - fn cache_id(&self) -> String; + fn cache_id(&self) -> CowStr; /// The prompt to display when asking for the secret. - fn prompt(&self) -> &'static str; + fn prompt(&self) -> CowStr; /// The description to display when asking for the secret. - fn description(&self, mode: Mode) -> String; + fn description(&self, mode: Mode) -> CowStr; } #[derive(Debug)] @@ -69,7 +72,7 @@ impl PinEntry { } impl SecretEntry for PinEntry { - fn cache_id(&self) -> String { + fn cache_id(&self) -> CowStr { let model = self.model.to_string().to_lowercase(); let suffix = format!("{}:{}", model, self.serial); @@ -77,16 +80,18 @@ impl SecretEntry for PinEntry { PinType::Admin => format!("nitrocli:admin:{}", suffix), PinType::User => format!("nitrocli:user:{}", suffix), } + .into() } - fn prompt(&self) -> &'static str { + fn prompt(&self) -> CowStr { match self.pin_type { PinType::Admin => "Admin PIN", PinType::User => "User PIN", } + .into() } - fn description(&self, mode: Mode) -> String { + fn description(&self, mode: Mode) -> CowStr { format!( "{} for\rNitrokey {} {}", match self.pin_type { @@ -104,6 +109,7 @@ impl SecretEntry for PinEntry { self.model, self.serial, ) + .into() } } @@ -167,7 +173,7 @@ pub fn inquire(entry: &E, mode: Mode, error_msg: Option<&str>) -> crate::Resu where E: SecretEntry, { - let cache_id = entry.cache_id(); + let cache_id = entry.cache_id().into(); let error_msg = error_msg .map(|msg| msg.replace(" ", "+")) .unwrap_or_else(|| String::from("+")); -- cgit v1.2.3