t_rust_less_lib/otp/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum OTPError {
5  InvalidUrl(String),
6  InvalidScheme,
7  InvalidType,
8  InvalidAlgorithm,
9  InvalidSecret,
10  MissingParameter(String),
11}
12
13impl fmt::Display for OTPError {
14  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15    match self {
16      OTPError::InvalidUrl(error) => write!(f, "Invalid url: {}", error)?,
17      OTPError::InvalidScheme => write!(f, "Invalid url scheme. Expected otpauth")?,
18      OTPError::InvalidType => write!(f, "Invalid OTP type. Only totp and hotp are supported")?,
19      OTPError::InvalidAlgorithm => write!(f, "Invalid OTP algorithm. Only SHA1, SHA256, SHA512 are supported")?,
20      OTPError::InvalidSecret => write!(f, "Invalid secret")?,
21      OTPError::MissingParameter(name) => write!(f, "Missing required parameter: {}", name)?,
22    }
23
24    Ok(())
25  }
26}
27
28pub type OTPResult<T> = Result<T, OTPError>;
29
30error_convert_from!(url::ParseError, OTPError, InvalidUrl(display));