From 2c31912bd2f8731c146da6640e28896c9de6286a Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 22 May 2018 08:55:45 +0000 Subject: Improve OtpSlotData constructor / builder Firstly, use Into instead of String::from(&str). Secondly, add methods to set the two arguments not set in the constructor. --- src/otp.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/otp.rs b/src/otp.rs index c0a470f..c951d5f 100644 --- a/src/otp.rs +++ b/src/otp.rs @@ -295,7 +295,7 @@ pub struct OtpSlotData { /// If true, press the enter key after sending an OTP code using double-pressed /// numlock, capslock or scrolllock. pub use_enter: bool, - /// Set the token ID [OATH Token Identifier Specification][tokspec], section + /// Set the token ID, see [OATH Token Identifier Specification][tokspec], section /// “Class A”. /// /// [tokspec]: https://openauthentication.org/token-specs/ @@ -315,16 +315,36 @@ pub struct RawOtpSlotData { impl OtpSlotData { /// Constructs a new instance of this struct. - pub fn new(number: u8, name: &str, secret: &str, mode: OtpMode) -> OtpSlotData { + pub fn new, T: Into>( + number: u8, + name: S, + secret: T, + mode: OtpMode, + ) -> OtpSlotData { OtpSlotData { number, - name: String::from(name), - secret: String::from(secret), + name: name.into(), + secret: secret.into(), mode, use_enter: false, token_id: None, } } + + /// Enables pressing the enter key after sending an OTP code using double-pressed numlock, + /// capslock or scrollock. + pub fn use_enter(mut self) -> OtpSlotData { + self.use_enter = true; + self + } + + /// Sets the token ID, see [OATH Token Identifier Specification][tokspec], section “Class A”. + /// + /// [tokspec]: https://openauthentication.org/token-specs/ + pub fn token_id>(mut self, id: S) -> OtpSlotData { + self.token_id = Some(id.into()); + self + } } impl RawOtpSlotData { -- cgit v1.2.1