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

@@ -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(),
}