From e6e8a024bf892851cf65ebea4c214d5bc1c90594 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 14 Jul 2019 18:10:32 -0700 Subject: Introduce TryInto trait This change introduces a new trait, TryInto, to the crate. In the future this trait will allow us to keep a flexible set of error result types from the various try_with_* functions, which use a certain nitrokey error variant to check for the entry of a wrong secret. Note that while a TryInto trait exists in Rust's standard library, that was not found to be helpful because we have no way to define it for nitrkey crate's error type. Because of that, we will always have a mismatch between our internal error and std::convert::Infallible. --- nitrocli/src/error.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'nitrocli/src/error.rs') diff --git a/nitrocli/src/error.rs b/nitrocli/src/error.rs index 1346526..d1eb2eb 100644 --- a/nitrocli/src/error.rs +++ b/nitrocli/src/error.rs @@ -22,6 +22,22 @@ use std::io; use std::str; use std::string; +/// A trait used to simplify error handling in conjunction with the +/// try_with_* functions we use for repeatedly asking the user for a +/// secret. +pub trait TryInto { + fn try_into(self) -> Result; +} + +impl TryInto for T +where + T: Into, +{ + fn try_into(self) -> Result { + Ok(self.into()) + } +} + #[derive(Debug)] pub enum Error { ArgparseError(i32), -- cgit v1.2.1