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::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, MySQLDatabase, Request, Response, protocol::{
print_create_databases_output_status, print_create_databases_output_status_json, 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::{ use crate::{
client::commands::{erroneous_server_response, read_password_from_stdin_with_double_check}, client::commands::{erroneous_server_response, read_password_from_stdin_with_double_check},
core::protocol::{ core::{
ClientToServerMessageStream, MySQLUser, Request, Response, protocol::{
print_create_users_output_status, print_create_users_output_status_json, ClientToServerMessageStream, Request, Response, print_create_users_output_status,
print_set_password_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::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, MySQLDatabase, Request, Response, protocol::{
print_drop_databases_output_status, print_drop_databases_output_status_json, 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::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, MySQLUser, Request, Response, print_drop_users_output_status, protocol::{
print_drop_users_output_status_json, 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, parse_privilege_data_from_editor_content, reduce_privilege_diffs,
}, },
protocol::{ protocol::{
ClientToServerMessageStream, MySQLDatabase, Request, Response, ClientToServerMessageStream, Request, Response,
print_modify_database_privileges_output_status, print_modify_database_privileges_output_status,
}, },
types::MySQLDatabase,
}, },
}; };

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, MySQLUser, Request, Response, print_lock_users_output_status, protocol::{
print_lock_users_output_status_json, 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::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, ListUsersError, MySQLUser, Request, Response, protocol::{
print_set_password_output_status, ClientToServerMessageStream, ListUsersError, Request, Response,
print_set_password_output_status,
},
types::MySQLUser,
}, },
}; };

View File

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

View File

@@ -8,7 +8,8 @@ use crate::{
core::{ core::{
common::yn, common::yn,
database_privileges::{DATABASE_PRIVILEGE_FIELDS, db_priv_field_human_readable_name}, 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::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ClientToServerMessageStream, MySQLUser, Request, Response}, core::{
protocol::{ClientToServerMessageStream, Request, Response},
types::MySQLUser,
},
}; };
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]

View File

@@ -4,9 +4,12 @@ use tokio_stream::StreamExt;
use crate::{ use crate::{
client::commands::erroneous_server_response, client::commands::erroneous_server_response,
core::protocol::{ core::{
ClientToServerMessageStream, MySQLUser, Request, Response, protocol::{
print_unlock_users_output_status, print_unlock_users_output_status_json, 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] #[inline]
pub fn trim_db_name_to_32_chars(db_name: &MySQLDatabase) -> MySQLDatabase { 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, bootstrap::bootstrap_server_connection_and_drop_privileges,
database_privileges::DatabasePrivilegeRow, database_privileges::DatabasePrivilegeRow,
protocol::{ protocol::{
ClientToServerMessageStream, GetDatabasesPrivilegeDataError, MySQLDatabase, Request, ClientToServerMessageStream, GetDatabasesPrivilegeDataError, Request, Response,
Response, create_client_to_server_message_stream, create_client_to_server_message_stream,
}, },
types::MySQLDatabase,
}, },
}; };

View File

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

View File

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

View File

@@ -3,7 +3,7 @@
use std::fmt; use std::fmt;
use crate::core::protocol::{MySQLDatabase, MySQLUser}; use crate::core::types::{MySQLDatabase, MySQLUser};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// This is the list of fields that are used to fetch the db + user + privileges /// 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. //! database privileges related CLI commands.
use super::diff::{DatabasePrivilegeChange, DatabasePrivilegeRowDiff}; 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, /// This enum represents a part of a CLI argument for editing database privileges,
/// indicating whether privileges are to be added, set, or removed. /// 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. //! generating, validating and reducing diffs between two sets of database privileges.
use super::base::{DatabasePrivilegeRow, db_priv_field_human_readable_name}; 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 prettytable::Table;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{

View File

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

View File

@@ -1,16 +1,15 @@
use std::{ use std::collections::BTreeSet;
collections::BTreeSet,
fmt::{Display, Formatter},
ops::{Deref, DerefMut},
str::FromStr,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::net::UnixStream; use tokio::net::UnixStream;
use tokio_serde::{Framed as SerdeFramed, formats::Bincode}; use tokio_serde::{Framed as SerdeFramed, formats::Bincode};
use tokio_util::codec::{Framed, LengthDelimitedCodec}; 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< pub type ServerToClientMessageStream = SerdeFramed<
Framed<UnixStream, LengthDelimitedCodec>, 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()) 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] #[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Request { pub enum Request {

View File

@@ -9,12 +9,11 @@ use crate::{
core::{ core::{
common::UnixUser, common::UnixUser,
database_privileges::{DatabasePrivilegeRow, DatabasePrivilegeRowDiff}, database_privileges::{DatabasePrivilegeRow, DatabasePrivilegeRowDiff},
types::{MySQLDatabase, MySQLUser},
}, },
server::sql::{database_operations::DatabaseRow, user_operations::DatabaseUser}, server::sql::{database_operations::DatabaseRow, user_operations::DatabaseUser},
}; };
use super::{MySQLDatabase, MySQLUser};
/// This enum is used to differentiate between database and user operations. /// 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. /// Their output are very similar, but there are slight differences in the words used.
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[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 serde::{Deserialize, Serialize};
use crate::core::protocol::MySQLDatabase; use crate::core::types::MySQLDatabase;
use crate::{ use crate::{
core::{ core::{
common::UnixUser, common::UnixUser,

View File

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

View File

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