Skip to content

Commit aeba542

Browse files
committed
derive default traits & add in a Display implementation to print the formatted string when called
1 parent 17fd3c1 commit aeba542

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
//! let hotp_str = HOTP::default_from_utf8(secret);
1818
//! // Get an otp with the given counter
1919
//! let otp_from_str = hotp_str.get_otp(counter);
20-
//! println!("The otp from hotp_str: {}", otp_from_str.as_string());
20+
//! println!("The otp from hotp_str: {}", otp_from_str);
2121
//!
2222
//! // Alternatively, get a HOTP instance with a '&[u8]' secret
2323
//! let hotp_bytes = HOTP::new(secret.as_bytes(), 6);
2424
//! // Get an otp with the given counter
2525
//! let otp_from_bytes = hotp_bytes.get_otp(counter);
26-
//! println!("The otp from hotp_bytes: {}", otp_from_bytes.as_string());
26+
//! println!("The otp from hotp_bytes: {}", otp_from_bytes);
2727
//! }
2828
//! ```
2929
//!
@@ -44,7 +44,7 @@
4444
//! let totp_sha1_str = TOTP::default_from_utf8(secret);
4545
//! // Get an otp with the given counter and elapsed seconds
4646
//! let otp_sha1 = totp_sha1_str.get_otp(elapsed_seconds);
47-
//! println!("The otp from totp_sha1_str: {}", otp_sha1.as_string());
47+
//! println!("The otp from totp_sha1_str: {}", otp_sha1);
4848
//!
4949
//! // Alternatively get a TOTP instance with an '&[u8]' secret
5050
//! // and different digest (Sha256 or Sha512)
@@ -59,7 +59,7 @@
5959
//! elapsed_seconds,
6060
//! 0, // Start time at unix epoch
6161
//! );
62-
//! println!("The otp from totp_sha256_bytes: {}", otp_sha256.as_string());
62+
//! println!("The otp from totp_sha256_bytes: {}", otp_sha256);
6363
//! }
6464
//! ```
6565
//!

src/otp_result.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use std::time::{SystemTime, UNIX_EPOCH};
1+
use std::fmt;
2+
use std::fmt::Formatter;
23

4+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
35
pub struct OTPResult {
46
digits: u32,
57
code: u32,
@@ -23,4 +25,10 @@ impl OTPResult {
2325
pub fn as_u32(&self) -> u32 {
2426
self.code
2527
}
28+
}
29+
30+
impl fmt::Display for OTPResult {
31+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
32+
write!(f, "{}", self.as_string())
33+
}
2634
}

0 commit comments

Comments
 (0)