mysqladm/core/protocol/commands/
drop_users.rs1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::json;
5
6use crate::core::{
7 protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
8 types::MySQLUser,
9};
10
11pub type DropUsersRequest = Vec<MySQLUser>;
12
13pub type DropUsersResponse = BTreeMap<MySQLUser, Result<(), DropUserError>>;
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
16pub enum DropUserError {
17 SanitizationError(NameValidationError),
18 OwnershipError(OwnerValidationError),
19 UserDoesNotExist,
20 MySqlError(String),
21}
22
23pub fn print_drop_users_output_status(output: &DropUsersResponse) {
24 for (username, result) in output {
25 match result {
26 Ok(()) => {
27 println!("User '{}' dropped successfully.", username);
28 }
29 Err(err) => {
30 println!("{}", err.to_error_message(username));
31 println!("Skipping...");
32 }
33 }
34 println!();
35 }
36}
37
38pub fn print_drop_users_output_status_json(output: &DropUsersResponse) {
39 let value = output
40 .iter()
41 .map(|(name, result)| match result {
42 Ok(()) => (name.to_string(), json!({ "status": "success" })),
43 Err(err) => (
44 name.to_string(),
45 json!({
46 "status": "error",
47 "error": err.to_error_message(name),
48 }),
49 ),
50 })
51 .collect::<serde_json::Map<_, _>>();
52 println!(
53 "{}",
54 serde_json::to_string_pretty(&value)
55 .unwrap_or("Failed to serialize result to JSON".to_string())
56 );
57}
58
59impl DropUserError {
60 pub fn to_error_message(&self, username: &MySQLUser) -> String {
61 match self {
62 DropUserError::SanitizationError(err) => err.to_error_message(username, DbOrUser::User),
63 DropUserError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
64 DropUserError::UserDoesNotExist => {
65 format!("User '{}' does not exist.", username)
66 }
67 DropUserError::MySqlError(err) => {
68 format!("MySQL error: {}", err)
69 }
70 }
71 }
72}