Skip to content

Commit a25198b

Browse files
committed
add documentation for the OTPResult class
1 parent da0a7af commit a25198b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/otp_result.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
11
use std::fmt;
22
use std::fmt::Formatter;
33

4+
/// A convenience struct to hold the result of a [`HOTP`] or [`TOTP`]
5+
/// generation.
6+
///
7+
/// Contains the amount of digits the OTP should be, and the actual OTP,
8+
/// which will be equal to or less than the digit count. Currently houses
9+
/// a convenience [`OTPResult::as_string`] which returns a zero-padded string
10+
/// that has a length of [`OTPResult::digits`]. Additionally, the numerical
11+
/// representation of the code can be got with [`OTPResult::as_u32`].
12+
///
13+
/// Returned as a result of either [`HOTP::get_otp`], [`TOTP::get_otp`]
14+
/// or [`TOTP::get_otp_with_custom_time_start`].
415
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
516
pub struct OTPResult {
617
digits: u32,
718
code: u32,
819
}
920

21+
/// Constructors for the [`OTPResult`] struct.
1022
impl OTPResult {
23+
/// Creates a new instance with the provided digit count and OTP code.
1124
pub fn new(digits: u32, code: u32 ) -> Self {
1225
OTPResult { digits, code }
1326
}
1427
}
1528

29+
/// Getters for the [`OTPResult`] struct.
1630
impl OTPResult {
31+
/// Gets the digit count given to the struct on creation.
32+
///
33+
/// Also the count used to determine how long the formatted string will be.
1734
pub fn get_digits(&self) -> u32 { self.digits }
1835
}
1936

37+
/// Conveneince code getters for the [`OTPResult`] struct
2038
impl OTPResult {
39+
/// Returns the OTP as a formatted string of length [`OTPResult.digits`].
40+
///
41+
/// If [`OTPResult::code`] is less than [`OTPResult::digits`] long, leading zeroes
42+
/// will be added to the string.
2143
pub fn as_string(&self) -> String {
2244
format!("{:01$}", self.code as usize, self.digits as usize)
2345
}
2446

47+
48+
/// Returns the OTP as it's original numerical representation
49+
///
50+
/// This number may not be [`OTPResult::digits`] long.
2551
pub fn as_u32(&self) -> u32 {
2652
self.code
2753
}
2854
}
2955

56+
/// A Display implementation for the [`OTPResult`] struct
57+
///
58+
/// Returns the String-formatted code, which is zero-padded
59+
/// to be [`OTPResult::digits`] long.
3060
impl fmt::Display for OTPResult {
3161
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3262
write!(f, "{}", self.as_string())

0 commit comments

Comments
 (0)