aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-20 06:42:39 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-20 06:42:39 -0800
commit01fb9e579de1c019ed718e04f9b592f2687f7040 (patch)
tree0e8f87cb21e69aed505345b3ff92bc8822ac2155
parentac2bfc709f57094bac54b299f0a307b765e9b674 (diff)
downloadnitrocli-01fb9e579de1c019ed718e04f9b592f2687f7040.tar.gz
nitrocli-01fb9e579de1c019ed718e04f9b592f2687f7040.tar.bz2
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.
-rw-r--r--nitrocli/src/pinentry.rs20
1 files 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 <http://www.gnu.org/licenses/>. *
// *************************************************************************
+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<E>(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("+"));