core: split mysql user/db into separate types module

This commit is contained in:
2025-11-26 02:03:18 +09:00
parent 9e39401049
commit 1571f6e2c7
25 changed files with 170 additions and 134 deletions

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, MySQLDatabase, Request, Response,
print_create_databases_output_status, print_create_databases_output_status_json,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_create_databases_output_status,
print_create_databases_output_status_json,
},
types::MySQLDatabase,
},
};

View File

@@ -5,10 +5,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::{erroneous_server_response, read_password_from_stdin_with_double_check},
core::protocol::{
ClientToServerMessageStream, MySQLUser, Request, Response,
print_create_users_output_status, print_create_users_output_status_json,
print_set_password_output_status,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_create_users_output_status,
print_create_users_output_status_json, print_set_password_output_status,
},
types::MySQLUser,
},
};

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, MySQLDatabase, Request, Response,
print_drop_databases_output_status, print_drop_databases_output_status_json,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_drop_databases_output_status,
print_drop_databases_output_status_json,
},
types::MySQLDatabase,
},
};

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, MySQLUser, Request, Response, print_drop_users_output_status,
print_drop_users_output_status_json,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_drop_users_output_status,
print_drop_users_output_status_json,
},
types::MySQLUser,
},
};

View File

@@ -17,9 +17,10 @@ use crate::{
parse_privilege_data_from_editor_content, reduce_privilege_diffs,
},
protocol::{
ClientToServerMessageStream, MySQLDatabase, Request, Response,
ClientToServerMessageStream, Request, Response,
print_modify_database_privileges_output_status,
},
types::MySQLDatabase,
},
};

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, MySQLUser, Request, Response, print_lock_users_output_status,
print_lock_users_output_status_json,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_lock_users_output_status,
print_lock_users_output_status_json,
},
types::MySQLUser,
},
};

View File

@@ -6,9 +6,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, ListUsersError, MySQLUser, Request, Response,
print_set_password_output_status,
core::{
protocol::{
ClientToServerMessageStream, ListUsersError, Request, Response,
print_set_password_output_status,
},
types::MySQLUser,
},
};

View File

@@ -5,7 +5,10 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{ClientToServerMessageStream, MySQLDatabase, Request, Response},
core::{
protocol::{ClientToServerMessageStream, Request, Response},
types::MySQLDatabase,
},
};
#[derive(Parser, Debug, Clone)]

View File

@@ -8,7 +8,8 @@ use crate::{
core::{
common::yn,
database_privileges::{DATABASE_PRIVILEGE_FIELDS, db_priv_field_human_readable_name},
protocol::{ClientToServerMessageStream, MySQLDatabase, Request, Response},
protocol::{ClientToServerMessageStream, Request, Response},
types::MySQLDatabase,
},
};

View File

@@ -5,7 +5,10 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{ClientToServerMessageStream, MySQLUser, Request, Response},
core::{
protocol::{ClientToServerMessageStream, Request, Response},
types::MySQLUser,
},
};
#[derive(Parser, Debug, Clone)]

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{
client::commands::erroneous_server_response,
core::protocol::{
ClientToServerMessageStream, MySQLUser, Request, Response,
print_unlock_users_output_status, print_unlock_users_output_status_json,
core::{
protocol::{
ClientToServerMessageStream, Request, Response, print_unlock_users_output_status,
print_unlock_users_output_status_json,
},
types::MySQLUser,
},
};

View File

@@ -1,4 +1,4 @@
use crate::core::protocol::{MySQLDatabase, MySQLUser};
use crate::core::types::{MySQLDatabase, MySQLUser};
#[inline]
pub fn trim_db_name_to_32_chars(db_name: &MySQLDatabase) -> MySQLDatabase {

View File

@@ -19,9 +19,10 @@ use crate::{
bootstrap::bootstrap_server_connection_and_drop_privileges,
database_privileges::DatabasePrivilegeRow,
protocol::{
ClientToServerMessageStream, GetDatabasesPrivilegeDataError, MySQLDatabase, Request,
Response, create_client_to_server_message_stream,
ClientToServerMessageStream, GetDatabasesPrivilegeDataError, Request, Response,
create_client_to_server_message_stream,
},
types::MySQLDatabase,
},
};

View File

@@ -18,9 +18,9 @@ use crate::{
core::{
bootstrap::bootstrap_server_connection_and_drop_privileges,
protocol::{
ClientToServerMessageStream, MySQLUser, Request, Response,
create_client_to_server_message_stream,
ClientToServerMessageStream, Request, Response, create_client_to_server_message_stream,
},
types::MySQLUser,
},
server::sql::user_operations::DatabaseUser,
};

View File

@@ -2,3 +2,4 @@ pub mod bootstrap;
pub mod common;
pub mod database_privileges;
pub mod protocol;
pub mod types;

View File

@@ -3,7 +3,7 @@
use std::fmt;
use crate::core::protocol::{MySQLDatabase, MySQLUser};
use crate::core::types::{MySQLDatabase, MySQLUser};
use serde::{Deserialize, Serialize};
/// This is the list of fields that are used to fetch the db + user + privileges

View File

@@ -2,7 +2,7 @@
//! database privileges related CLI commands.
use super::diff::{DatabasePrivilegeChange, DatabasePrivilegeRowDiff};
use crate::core::protocol::{MySQLDatabase, MySQLUser};
use crate::core::types::{MySQLDatabase, MySQLUser};
/// This enum represents a part of a CLI argument for editing database privileges,
/// indicating whether privileges are to be added, set, or removed.

View File

@@ -2,7 +2,7 @@
//! generating, validating and reducing diffs between two sets of database privileges.
use super::base::{DatabasePrivilegeRow, db_priv_field_human_readable_name};
use crate::core::protocol::{MySQLDatabase, MySQLUser};
use crate::core::types::{MySQLDatabase, MySQLUser};
use prettytable::Table;
use serde::{Deserialize, Serialize};
use std::{

View File

@@ -6,7 +6,7 @@ use super::base::{
};
use crate::core::{
common::{rev_yn, yn},
protocol::MySQLDatabase,
types::MySQLDatabase,
};
use anyhow::{Context, anyhow};
use itertools::Itertools;

View File

@@ -1,16 +1,15 @@
use std::{
collections::BTreeSet,
fmt::{Display, Formatter},
ops::{Deref, DerefMut},
str::FromStr,
};
use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
use tokio::net::UnixStream;
use tokio_serde::{Framed as SerdeFramed, formats::Bincode};
use tokio_util::codec::{Framed, LengthDelimitedCodec};
use crate::core::{database_privileges::DatabasePrivilegesDiff, protocol::*};
use crate::core::{
database_privileges::DatabasePrivilegesDiff,
protocol::*,
types::{MySQLDatabase, MySQLUser},
};
pub type ServerToClientMessageStream = SerdeFramed<
Framed<UnixStream, LengthDelimitedCodec>,
@@ -36,92 +35,6 @@ pub fn create_client_to_server_message_stream(socket: UnixStream) -> ClientToSer
tokio_serde::Framed::new(length_delimited, Bincode::default())
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct MySQLUser(String);
impl FromStr for MySQLUser {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MySQLUser(s.to_string()))
}
}
impl Deref for MySQLUser {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for MySQLUser {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Display for MySQLUser {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for MySQLUser {
fn from(s: &str) -> Self {
MySQLUser(s.to_string())
}
}
impl From<String> for MySQLUser {
fn from(s: String) -> Self {
MySQLUser(s)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct MySQLDatabase(String);
impl FromStr for MySQLDatabase {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MySQLDatabase(s.to_string()))
}
}
impl Deref for MySQLDatabase {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for MySQLDatabase {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Display for MySQLDatabase {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for MySQLDatabase {
fn from(s: &str) -> Self {
MySQLDatabase(s.to_string())
}
}
impl From<String> for MySQLDatabase {
fn from(s: String) -> Self {
MySQLDatabase(s)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Request {

View File

@@ -9,12 +9,11 @@ use crate::{
core::{
common::UnixUser,
database_privileges::{DatabasePrivilegeRow, DatabasePrivilegeRowDiff},
types::{MySQLDatabase, MySQLUser},
},
server::sql::{database_operations::DatabaseRow, user_operations::DatabaseUser},
};
use super::{MySQLDatabase, MySQLUser};
/// 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)]

93
src/core/types.rs Normal file
View File

@@ -0,0 +1,93 @@
use std::{
fmt,
ops::{Deref, DerefMut},
str::FromStr,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct MySQLUser(String);
impl FromStr for MySQLUser {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MySQLUser(s.to_string()))
}
}
impl Deref for MySQLUser {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for MySQLUser {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl fmt::Display for MySQLUser {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for MySQLUser {
fn from(s: &str) -> Self {
MySQLUser(s.to_string())
}
}
impl From<String> for MySQLUser {
fn from(s: String) -> Self {
MySQLUser(s)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct MySQLDatabase(String);
impl FromStr for MySQLDatabase {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MySQLDatabase(s.to_string()))
}
}
impl Deref for MySQLDatabase {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for MySQLDatabase {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl fmt::Display for MySQLDatabase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for MySQLDatabase {
fn from(s: &str) -> Self {
MySQLDatabase(s.to_string())
}
}
impl From<String> for MySQLDatabase {
fn from(s: String) -> Self {
MySQLDatabase(s)
}
}

View File

@@ -5,7 +5,7 @@ use sqlx::prelude::*;
use serde::{Deserialize, Serialize};
use crate::core::protocol::MySQLDatabase;
use crate::core::types::MySQLDatabase;
use crate::{
core::{
common::UnixUser,

View File

@@ -30,9 +30,9 @@ use crate::{
protocol::{
DiffDoesNotApplyError, GetAllDatabasesPrivilegeData, GetAllDatabasesPrivilegeDataError,
GetDatabasesPrivilegeData, GetDatabasesPrivilegeDataError,
ModifyDatabasePrivilegesError, ModifyDatabasePrivilegesOutput, MySQLDatabase,
MySQLUser,
ModifyDatabasePrivilegesError, ModifyDatabasePrivilegesOutput,
},
types::{MySQLDatabase, MySQLUser},
},
server::{
common::{create_user_group_matching_regex, try_get_with_binary_fallback},

View File

@@ -14,8 +14,9 @@ use crate::{
protocol::{
CreateUserError, CreateUsersOutput, DropUserError, DropUsersOutput, ListAllUsersError,
ListAllUsersOutput, ListUsersError, ListUsersOutput, LockUserError, LockUsersOutput,
MySQLUser, SetPasswordError, SetPasswordOutput, UnlockUserError, UnlockUsersOutput,
SetPasswordError, SetPasswordOutput, UnlockUserError, UnlockUsersOutput,
},
types::MySQLUser,
},
server::{
common::{create_user_group_matching_regex, try_get_with_binary_fallback},