awc_firebase_auth/
error.rs

1use std::fmt;
2use serde::{Serialize, Deserialize};
3
4pub enum LoginError {
5    EmailNotFound,
6    InvalidPassword,
7    UserDisabled,
8    EmailExists,
9    OperationNotAllowed,
10    TooManyAttempts,
11    Unknown
12}
13
14pub enum RegisterError {
15    EmailExists,
16    OperationNotAllowed,
17    TooManyAttempts,
18    Unknown
19}
20
21
22impl fmt::Display for LoginError {
23
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        match *self {
26            LoginError::EmailNotFound => write!(f, "Email not found"),
27            LoginError::InvalidPassword => write!(f, "Invalid password"),
28            LoginError::UserDisabled => write!(f, "User disabled"),
29            LoginError::EmailExists => write!(f, "Email exists"),
30            LoginError::OperationNotAllowed => write!(f, "Operation not allowed"),        
31            LoginError::TooManyAttempts => write!(f, "Too many attempts"),
32            LoginError::Unknown => write!(f, "Unknown"),
33        }
34    }
35}
36
37impl fmt::Display for RegisterError {
38
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        match *self {
41            RegisterError::EmailExists => write!(f, "Email exists"),
42            RegisterError::OperationNotAllowed => write!(f, "Operation not allowed"),
43            RegisterError::TooManyAttempts => write!(f, "Too many attempts"),
44            RegisterError::Unknown => write!(f, "Unknown"),
45        }
46    }
47
48}
49
50#[derive(Serialize, Deserialize)]
51struct ErrorBody {
52    domain: String,
53    reason: String,
54    message: String,
55}
56
57#[derive(Serialize, Deserialize)]
58pub struct Error {
59    errors: Vec<ErrorBody>,
60    code: i32,
61    message: String,
62}
63
64#[derive(Serialize, Deserialize)]
65pub struct ErrorContainer {
66    pub error: Error,
67}
68
69impl Error {
70    pub fn register_error(&self) -> RegisterError {
71        match self.message.as_str() {
72            "EMAIL_EXISTS" => RegisterError::EmailExists,
73            "OPERATION_NOT_ALLOWED" => RegisterError::OperationNotAllowed,
74            "TOO_MANY_ATTEMPTS_TRY_LATER" => RegisterError::TooManyAttempts,
75            _ => RegisterError::Unknown,
76        }
77    }
78
79    pub fn login_error(&self) -> LoginError {
80        match self.message.as_str() {
81            "EMAIL_NOT_FOUND" => LoginError::EmailNotFound,
82            "INVALID_PASSWORD" => LoginError::InvalidPassword,
83            "USER_DISABLED" => LoginError::UserDisabled,
84            "EMAIL_EXISTS" => LoginError::EmailExists,
85            "OPERATION_NOT_ALLOWED" => LoginError::OperationNotAllowed,
86            "TOO_MANY_ATTEMPTS_TRY_LATER" => LoginError::TooManyAttempts,
87            _ => LoginError::Unknown,
88        }
89    }
90}