core: move DbOrUser to types, wrap item name in struct

This commit is contained in:
2025-11-29 18:59:11 +09:00
parent 877f45c103
commit 03ddf0ac8a
14 changed files with 140 additions and 106 deletions

View File

@@ -1,12 +1,15 @@
use crate::core::protocol::{
use crate::core::{
protocol::{
CreateDatabaseError, CreateUserError, DropDatabaseError, DropUserError,
GetDatabasesPrivilegeDataError, ListUsersError, request_validation::DbOrUser,
GetDatabasesPrivilegeDataError, ListUsersError,
},
types::DbOrUser,
};
pub fn name_validation_error_to_error_message(name: &str, db_or_user: DbOrUser) -> String {
pub fn name_validation_error_to_error_message(db_or_user: DbOrUser) -> String {
let argv0 = std::env::args().next().unwrap_or_else(|| match db_or_user {
DbOrUser::Database => "mysql-dbadm".to_string(),
DbOrUser::User => "mysql-useradm".to_string(),
DbOrUser::Database(_) => "mysql-dbadm".to_string(),
DbOrUser::User(_) => "mysql-useradm".to_string(),
});
format!(
@@ -15,16 +18,16 @@ pub fn name_validation_error_to_error_message(name: &str, db_or_user: DbOrUser)
"Only A-Z, a-z, 0-9, _ (underscore) and - (dash) permitted. Skipping.",
),
argv0,
db_or_user.capitalized(),
name,
db_or_user.capitalized_noun(),
db_or_user.name(),
)
}
pub fn owner_validation_error_message(name: &str, db_or_user: DbOrUser) -> String {
pub fn owner_validation_error_message(db_or_user: DbOrUser) -> String {
format!(
"You are not in charge of mysql-{}: '{}'. Skipping.",
db_or_user.lowercased(),
name
db_or_user.lowercased_noun(),
db_or_user.name(),
)
}
@@ -36,11 +39,14 @@ pub fn handle_create_user_error(error: CreateUserError, name: &str) {
CreateUserError::SanitizationError(_) => {
eprintln!(
"{}",
name_validation_error_to_error_message(name, DbOrUser::User)
name_validation_error_to_error_message(DbOrUser::User(name.into()))
);
}
CreateUserError::OwnershipError(_) => {
eprintln!("{}", owner_validation_error_message(name, DbOrUser::User));
eprintln!(
"{}",
owner_validation_error_message(DbOrUser::User(name.into()))
);
}
CreateUserError::MySqlError(_) | CreateUserError::UserAlreadyExists => {
eprintln!("{}: Failed to create user '{}'.", argv0, name);
@@ -56,11 +62,14 @@ pub fn handle_drop_user_error(error: DropUserError, name: &str) {
DropUserError::SanitizationError(_) => {
eprintln!(
"{}",
name_validation_error_to_error_message(name, DbOrUser::User)
name_validation_error_to_error_message(DbOrUser::User(name.into()))
);
}
DropUserError::OwnershipError(_) => {
eprintln!("{}", owner_validation_error_message(name, DbOrUser::User));
eprintln!(
"{}",
owner_validation_error_message(DbOrUser::User(name.into()))
);
}
DropUserError::MySqlError(_) | DropUserError::UserDoesNotExist => {
eprintln!("{}: Failed to delete user '{}'.", argv0, name);
@@ -76,11 +85,14 @@ pub fn handle_list_users_error(error: ListUsersError, name: &str) {
ListUsersError::SanitizationError(_) => {
eprintln!(
"{}",
name_validation_error_to_error_message(name, DbOrUser::User)
name_validation_error_to_error_message(DbOrUser::User(name.into()))
);
}
ListUsersError::OwnershipError(_) => {
eprintln!("{}", owner_validation_error_message(name, DbOrUser::User));
eprintln!(
"{}",
owner_validation_error_message(DbOrUser::User(name.into()))
);
}
ListUsersError::UserDoesNotExist => {
eprintln!(
@@ -104,13 +116,13 @@ pub fn handle_create_database_error(error: CreateDatabaseError, name: &str) {
CreateDatabaseError::SanitizationError(_) => {
eprintln!(
"{}",
name_validation_error_to_error_message(name, DbOrUser::Database)
name_validation_error_to_error_message(DbOrUser::Database(name.into()))
);
}
CreateDatabaseError::OwnershipError(_) => {
eprintln!(
"{}",
owner_validation_error_message(name, DbOrUser::Database)
owner_validation_error_message(DbOrUser::Database(name.into()))
);
}
CreateDatabaseError::MySqlError(_) => {
@@ -130,13 +142,13 @@ pub fn handle_drop_database_error(error: DropDatabaseError, name: &str) {
DropDatabaseError::SanitizationError(_) => {
eprintln!(
"{}",
name_validation_error_to_error_message(name, DbOrUser::Database)
name_validation_error_to_error_message(DbOrUser::Database(name.into()))
);
}
DropDatabaseError::OwnershipError(_) => {
eprintln!(
"{}",
owner_validation_error_message(name, DbOrUser::Database)
owner_validation_error_message(DbOrUser::Database(name.into()))
);
}
DropDatabaseError::MySqlError(_) => {
@@ -158,10 +170,10 @@ pub fn format_show_database_error_message(
match error {
GetDatabasesPrivilegeDataError::SanitizationError(_) => {
name_validation_error_to_error_message(name, DbOrUser::Database)
name_validation_error_to_error_message(DbOrUser::Database(name.into()))
}
GetDatabasesPrivilegeDataError::OwnershipError(_) => {
owner_validation_error_message(name, DbOrUser::Database)
owner_validation_error_message(DbOrUser::Database(name.into()))
}
GetDatabasesPrivilegeDataError::MySqlError(err) => {
format!(

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLDatabase,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase},
};
pub type CreateDatabasesRequest = Vec<MySQLDatabase>;
@@ -60,10 +60,10 @@ impl CreateDatabaseError {
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
match self {
CreateDatabaseError::SanitizationError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
CreateDatabaseError::OwnershipError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
CreateDatabaseError::DatabaseAlreadyExists => {
format!("Database {} already exists.", database_name)

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
};
pub type CreateUsersRequest = Vec<MySQLUser>;
@@ -60,9 +60,11 @@ impl CreateUserError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
CreateUserError::SanitizationError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
CreateUserError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
CreateUserError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
CreateUserError::UserAlreadyExists => {
format!("User '{}' already exists.", username)
}

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLDatabase,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase},
};
pub type DropDatabasesRequest = Vec<MySQLDatabase>;
@@ -63,10 +63,10 @@ impl DropDatabaseError {
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
match self {
DropDatabaseError::SanitizationError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
DropDatabaseError::OwnershipError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
DropDatabaseError::DatabaseDoesNotExist => {
format!("Database {} does not exist.", database_name)

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
};
pub type DropUsersRequest = Vec<MySQLUser>;
@@ -59,8 +59,12 @@ pub fn print_drop_users_output_status_json(output: &DropUsersResponse) {
impl DropUserError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
DropUserError::SanitizationError(err) => err.to_error_message(username, DbOrUser::User),
DropUserError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
DropUserError::SanitizationError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
DropUserError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
DropUserError::UserDoesNotExist => {
format!("User '{}' does not exist.", username)
}

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use crate::{
core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLDatabase,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase},
},
server::sql::database_operations::DatabaseRow,
};
@@ -26,10 +26,10 @@ impl ListDatabasesError {
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
match self {
ListDatabasesError::SanitizationError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
ListDatabasesError::OwnershipError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
ListDatabasesError::DatabaseDoesNotExist => {
format!("Database '{}' does not exist.", database_name)

View File

@@ -8,8 +8,8 @@ use serde::{Deserialize, Serialize};
use crate::core::{
database_privileges::DatabasePrivilegeRow,
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLDatabase,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase},
};
pub type ListPrivilegesRequest = Option<Vec<MySQLDatabase>>;
@@ -29,10 +29,10 @@ impl GetDatabasesPrivilegeDataError {
pub fn to_error_message(&self, database_name: &MySQLDatabase) -> String {
match self {
GetDatabasesPrivilegeDataError::SanitizationError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
GetDatabasesPrivilegeDataError::OwnershipError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
GetDatabasesPrivilegeDataError::DatabaseDoesNotExist => {
format!("Database '{}' does not exist.", database_name)

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use crate::{
core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
},
server::sql::user_operations::DatabaseUser,
};
@@ -26,9 +26,11 @@ impl ListUsersError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
ListUsersError::SanitizationError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
ListUsersError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
ListUsersError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
ListUsersError::UserDoesNotExist => {
format!("User '{}' does not exist.", username)
}

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
};
pub type LockUsersRequest = Vec<MySQLUser>;
@@ -60,8 +60,12 @@ pub fn print_lock_users_output_status_json(output: &LockUsersResponse) {
impl LockUserError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
LockUserError::SanitizationError(err) => err.to_error_message(username, DbOrUser::User),
LockUserError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
LockUserError::SanitizationError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
LockUserError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
LockUserError::UserDoesNotExist => {
format!("User '{}' does not exist.", username)
}

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use crate::core::{
database_privileges::{DatabasePrivilegeRow, DatabasePrivilegeRowDiff, DatabasePrivilegesDiff},
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::{MySQLDatabase, MySQLUser},
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLDatabase, MySQLUser},
};
pub type ModifyPrivilegesRequest = BTreeSet<DatabasePrivilegesDiff>;
@@ -54,16 +54,16 @@ impl ModifyDatabasePrivilegesError {
pub fn to_error_message(&self, database_name: &MySQLDatabase, username: &MySQLUser) -> String {
match self {
ModifyDatabasePrivilegesError::DatabaseSanitizationError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
ModifyDatabasePrivilegesError::DatabaseOwnershipError(err) => {
err.to_error_message(database_name, DbOrUser::Database)
err.to_error_message(DbOrUser::Database(database_name.clone()))
}
ModifyDatabasePrivilegesError::UserSanitizationError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
ModifyDatabasePrivilegesError::UserOwnershipError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
ModifyDatabasePrivilegesError::DatabaseDoesNotExist => {
format!("Database '{}' does not exist.", database_name)

View File

@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
};
pub type SetUserPasswordRequest = (MySQLUser, String);
@@ -33,9 +33,11 @@ impl SetPasswordError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
SetPasswordError::SanitizationError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
SetPasswordError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
SetPasswordError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
SetPasswordError::UserDoesNotExist => {
format!("User '{}' does not exist.", username)
}

View File

@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::core::{
protocol::request_validation::{DbOrUser, NameValidationError, OwnerValidationError},
types::MySQLUser,
protocol::request_validation::{NameValidationError, OwnerValidationError},
types::{DbOrUser, MySQLUser},
};
pub type UnlockUsersRequest = Vec<MySQLUser>;
@@ -61,9 +61,11 @@ impl UnlockUserError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
UnlockUserError::SanitizationError(err) => {
err.to_error_message(username, DbOrUser::User)
err.to_error_message(DbOrUser::User(username.clone()))
}
UnlockUserError::OwnershipError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
UnlockUserError::OwnershipError(err) => err.to_error_message(username, DbOrUser::User),
UnlockUserError::UserDoesNotExist => {
format!("User '{}' does not exist.", username)
}

View File

@@ -2,31 +2,7 @@ use indoc::indoc;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::core::common::UnixUser;
/// This enum is used to differentiate between database and user operations.
/// Their output are very similar, but there are slight differences in the words used.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DbOrUser {
Database,
User,
}
impl DbOrUser {
pub fn lowercased(&self) -> &'static str {
match self {
DbOrUser::Database => "database",
DbOrUser::User => "user",
}
}
pub fn capitalized(&self) -> &'static str {
match self {
DbOrUser::Database => "Database",
DbOrUser::User => "User",
}
}
}
use crate::core::{common::UnixUser, types::DbOrUser};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum NameValidationError {
@@ -36,14 +12,14 @@ pub enum NameValidationError {
}
impl NameValidationError {
pub fn to_error_message(self, name: &str, db_or_user: DbOrUser) -> String {
pub fn to_error_message(self, db_or_user: DbOrUser) -> String {
match self {
NameValidationError::EmptyString => {
format!("{} name cannot be empty.", db_or_user.capitalized()).to_owned()
format!("{} name cannot be empty.", db_or_user.capitalized_noun()).to_owned()
}
NameValidationError::TooLong => format!(
"{} is too long. Maximum length is 64 characters.",
db_or_user.capitalized()
db_or_user.capitalized_noun()
)
.to_owned(),
NameValidationError::InvalidCharacters => format!(
@@ -52,8 +28,8 @@ impl NameValidationError {
Only A-Z, a-z, 0-9, _ (underscore) and - (dash) are permitted.
"#},
db_or_user.lowercased(),
name
db_or_user.lowercased_noun(),
db_or_user.name(),
)
.to_owned(),
}
@@ -61,7 +37,7 @@ impl NameValidationError {
}
impl OwnerValidationError {
pub fn to_error_message(self, name: &str, db_or_user: DbOrUser) -> String {
pub fn to_error_message(self, db_or_user: DbOrUser) -> String {
let user = UnixUser::from_enviroment();
let UnixUser {
@@ -85,10 +61,10 @@ impl OwnerValidationError {
- {}
{}
"#},
db_or_user.lowercased(),
name,
db_or_user.lowercased(),
db_or_user.lowercased(),
db_or_user.lowercased_noun(),
db_or_user.name(),
db_or_user.lowercased_noun(),
db_or_user.lowercased_noun(),
username,
groups
.into_iter()
@@ -99,8 +75,8 @@ impl OwnerValidationError {
.to_owned(),
OwnerValidationError::StringEmpty => format!(
"'{}' is not a valid {} name.",
name,
db_or_user.lowercased()
db_or_user.name(),
db_or_user.lowercased_noun()
)
.to_string(),
}

View File

@@ -91,3 +91,33 @@ impl From<String> for MySQLDatabase {
MySQLDatabase(s)
}
}
/// This enum is used to differentiate between database and user operations.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DbOrUser {
Database(MySQLDatabase),
User(MySQLUser),
}
impl DbOrUser {
pub fn lowercased_noun(&self) -> &'static str {
match self {
DbOrUser::Database(_) => "database",
DbOrUser::User(_) => "user",
}
}
pub fn capitalized_noun(&self) -> &'static str {
match self {
DbOrUser::Database(_) => "Database",
DbOrUser::User(_) => "User",
}
}
pub fn name(&self) -> &str {
match self {
DbOrUser::Database(db) => db.as_str(),
DbOrUser::User(user) => user.as_str(),
}
}
}