1
use serde::{Deserialize, Serialize};
2
use thiserror::Error;
3

            
4
use crate::core::{
5
    protocol::request_validation::ValidationError,
6
    types::{DbOrUser, MySQLUser},
7
};
8

            
9
pub type SetUserPasswordRequest = (MySQLUser, String);
10

            
11
pub type SetUserPasswordResponse = Result<(), SetPasswordError>;
12

            
13
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
14
pub enum SetPasswordError {
15
    #[error("Validation error: {0}")]
16
    ValidationError(#[from] ValidationError),
17

            
18
    #[error("User does not exist")]
19
    UserDoesNotExist,
20

            
21
    #[error("MySQL error: {0}")]
22
    MySqlError(String),
23
}
24

            
25
pub fn print_set_password_output_status(output: &SetUserPasswordResponse, username: &MySQLUser) {
26
    match output {
27
        Ok(()) => {
28
            println!("Password for user '{username}' set successfully.");
29
        }
30
        Err(err) => {
31
            eprintln!("{}", err.to_error_message(username));
32
            eprintln!("Skipping...");
33
        }
34
    }
35
}
36

            
37
impl SetPasswordError {
38
    #[must_use]
39
    pub fn to_error_message(&self, username: &MySQLUser) -> String {
40
        match self {
41
            SetPasswordError::ValidationError(err) => {
42
                err.to_error_message(&DbOrUser::User(username.clone()))
43
            }
44
            SetPasswordError::UserDoesNotExist => {
45
                format!("User '{username}' does not exist.")
46
            }
47
            SetPasswordError::MySqlError(err) => {
48
                format!("MySQL error: {err}")
49
            }
50
        }
51
    }
52

            
53
    #[allow(dead_code)]
54
    #[must_use]
55
    pub fn error_type(&self) -> String {
56
        match self {
57
            SetPasswordError::ValidationError(err) => err.error_type(),
58
            SetPasswordError::UserDoesNotExist => "user-does-not-exist".to_string(),
59
            SetPasswordError::MySqlError(_) => "mysql-error".to_string(),
60
        }
61
    }
62
}