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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,31 +2,7 @@ use indoc::indoc;
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::core::common::UnixUser; use crate::core::{common::UnixUser, types::DbOrUser};
/// 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",
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum NameValidationError { pub enum NameValidationError {
@@ -36,14 +12,14 @@ pub enum NameValidationError {
} }
impl 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 { match self {
NameValidationError::EmptyString => { 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!( NameValidationError::TooLong => format!(
"{} is too long. Maximum length is 64 characters.", "{} is too long. Maximum length is 64 characters.",
db_or_user.capitalized() db_or_user.capitalized_noun()
) )
.to_owned(), .to_owned(),
NameValidationError::InvalidCharacters => format!( NameValidationError::InvalidCharacters => format!(
@@ -52,8 +28,8 @@ impl NameValidationError {
Only A-Z, a-z, 0-9, _ (underscore) and - (dash) are permitted. Only A-Z, a-z, 0-9, _ (underscore) and - (dash) are permitted.
"#}, "#},
db_or_user.lowercased(), db_or_user.lowercased_noun(),
name db_or_user.name(),
) )
.to_owned(), .to_owned(),
} }
@@ -61,7 +37,7 @@ impl NameValidationError {
} }
impl OwnerValidationError { 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 user = UnixUser::from_enviroment();
let UnixUser { let UnixUser {
@@ -85,10 +61,10 @@ impl OwnerValidationError {
- {} - {}
{} {}
"#}, "#},
db_or_user.lowercased(), db_or_user.lowercased_noun(),
name, db_or_user.name(),
db_or_user.lowercased(), db_or_user.lowercased_noun(),
db_or_user.lowercased(), db_or_user.lowercased_noun(),
username, username,
groups groups
.into_iter() .into_iter()
@@ -99,8 +75,8 @@ impl OwnerValidationError {
.to_owned(), .to_owned(),
OwnerValidationError::StringEmpty => format!( OwnerValidationError::StringEmpty => format!(
"'{}' is not a valid {} name.", "'{}' is not a valid {} name.",
name, db_or_user.name(),
db_or_user.lowercased() db_or_user.lowercased_noun()
) )
.to_string(), .to_string(),
} }

View File

@@ -91,3 +91,33 @@ impl From<String> for MySQLDatabase {
MySQLDatabase(s) 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(),
}
}
}