-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathotp_result.rs
71 lines (63 loc) · 2.33 KB
/
otp_result.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::fmt;
use std::fmt::Formatter;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
/// A convenience struct to hold the result of a [`HOTP`] or [`TOTP`]
/// generation.
///
/// Contains the amount of digits the OTP should be, and the actual OTP,
/// which will be equal to or less than the digit count. Currently houses
/// a convenience [`OTPResult::as_string`] which returns a zero-padded string
/// that has a length of [`OTPResult::digits`]. Additionally, the numerical
/// representation of the code can be got with [`OTPResult::as_u32`].
///
/// Returned as a result of either [`HOTP::get_otp`], [`TOTP::get_otp`]
/// or [`TOTP::get_otp_with_custom_time_start`].
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub struct OTPResult {
digits: u32,
code: u32,
}
/// Constructors for the [`OTPResult`] struct.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl OTPResult {
/// Creates a new instance with the provided digit count and OTP code.
pub fn new(digits: u32, code: u32 ) -> Self {
OTPResult { digits, code }
}
}
/// Getters for the [`OTPResult`] struct.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl OTPResult {
/// Gets the digit count given to the struct on creation.
///
/// Also the count used to determine how long the formatted string will be.
pub fn get_digits(&self) -> u32 { self.digits }
}
/// Convenience code getters for the [`OTPResult`] struct
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl OTPResult {
/// Returns the OTP as a formatted string of length [`OTPResult.digits`].
///
/// If [`OTPResult::code`] is less than [`OTPResult::digits`] long, leading zeroes
/// will be added to the string.
pub fn as_string(&self) -> String {
format!("{:01$}", self.code as usize, self.digits as usize)
}
/// Returns the OTP as it's original numerical representation
///
/// This number may not be [`OTPResult::digits`] long.
pub fn as_u32(&self) -> u32 {
self.code
}
}
/// A Display implementation for the [`OTPResult`] struct
///
/// Returns the String-formatted code, which is zero-padded
/// to be [`OTPResult::digits`] long.
impl fmt::Display for OTPResult {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_string())
}
}