diff --git a/src/client/commands/create_db.rs b/src/client/commands/create_db.rs index 53407af..b88044a 100644 --- a/src/client/commands/create_db.rs +++ b/src/client/commands/create_db.rs @@ -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, }, }; diff --git a/src/client/commands/create_user.rs b/src/client/commands/create_user.rs index ea2ceca..84d7a38 100644 --- a/src/client/commands/create_user.rs +++ b/src/client/commands/create_user.rs @@ -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, }, }; diff --git a/src/client/commands/drop_db.rs b/src/client/commands/drop_db.rs index e9c1aec..f9a929f 100644 --- a/src/client/commands/drop_db.rs +++ b/src/client/commands/drop_db.rs @@ -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, }, }; diff --git a/src/client/commands/drop_user.rs b/src/client/commands/drop_user.rs index 2cfeeab..745b9ef 100644 --- a/src/client/commands/drop_user.rs +++ b/src/client/commands/drop_user.rs @@ -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, }, }; diff --git a/src/client/commands/edit_db_privs.rs b/src/client/commands/edit_db_privs.rs index cc1f668..3864c07 100644 --- a/src/client/commands/edit_db_privs.rs +++ b/src/client/commands/edit_db_privs.rs @@ -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, }, }; diff --git a/src/client/commands/lock_user.rs b/src/client/commands/lock_user.rs index 334c950..86258af 100644 --- a/src/client/commands/lock_user.rs +++ b/src/client/commands/lock_user.rs @@ -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, }, }; diff --git a/src/client/commands/passwd_user.rs b/src/client/commands/passwd_user.rs index 307cc8c..45a532b 100644 --- a/src/client/commands/passwd_user.rs +++ b/src/client/commands/passwd_user.rs @@ -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, }, }; diff --git a/src/client/commands/show_db.rs b/src/client/commands/show_db.rs index d1590bf..0b80a9a 100644 --- a/src/client/commands/show_db.rs +++ b/src/client/commands/show_db.rs @@ -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)] diff --git a/src/client/commands/show_db_privs.rs b/src/client/commands/show_db_privs.rs index e2d172b..ad90691 100644 --- a/src/client/commands/show_db_privs.rs +++ b/src/client/commands/show_db_privs.rs @@ -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, }, }; diff --git a/src/client/commands/show_user.rs b/src/client/commands/show_user.rs index a215cb4..a413c79 100644 --- a/src/client/commands/show_user.rs +++ b/src/client/commands/show_user.rs @@ -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)] diff --git a/src/client/commands/unlock_user.rs b/src/client/commands/unlock_user.rs index 58021c0..d280a6f 100644 --- a/src/client/commands/unlock_user.rs +++ b/src/client/commands/unlock_user.rs @@ -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, }, }; diff --git a/src/client/mysql_admutils_compatibility/common.rs b/src/client/mysql_admutils_compatibility/common.rs index d3d829d..5a293b5 100644 --- a/src/client/mysql_admutils_compatibility/common.rs +++ b/src/client/mysql_admutils_compatibility/common.rs @@ -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 { diff --git a/src/client/mysql_admutils_compatibility/mysql_dbadm.rs b/src/client/mysql_admutils_compatibility/mysql_dbadm.rs index 10ed8fa..d19b77c 100644 --- a/src/client/mysql_admutils_compatibility/mysql_dbadm.rs +++ b/src/client/mysql_admutils_compatibility/mysql_dbadm.rs @@ -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, }, }; diff --git a/src/client/mysql_admutils_compatibility/mysql_useradm.rs b/src/client/mysql_admutils_compatibility/mysql_useradm.rs index 72e1b6a..ac19fe3 100644 --- a/src/client/mysql_admutils_compatibility/mysql_useradm.rs +++ b/src/client/mysql_admutils_compatibility/mysql_useradm.rs @@ -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, }; diff --git a/src/core.rs b/src/core.rs index 565d038..5ef5054 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,3 +2,4 @@ pub mod bootstrap; pub mod common; pub mod database_privileges; pub mod protocol; +pub mod types; diff --git a/src/core/database_privileges/base.rs b/src/core/database_privileges/base.rs index ebea5c5..b2f2639 100644 --- a/src/core/database_privileges/base.rs +++ b/src/core/database_privileges/base.rs @@ -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 diff --git a/src/core/database_privileges/cli.rs b/src/core/database_privileges/cli.rs index ed30101..d045899 100644 --- a/src/core/database_privileges/cli.rs +++ b/src/core/database_privileges/cli.rs @@ -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. diff --git a/src/core/database_privileges/diff.rs b/src/core/database_privileges/diff.rs index 3edef4d..e5c6c7b 100644 --- a/src/core/database_privileges/diff.rs +++ b/src/core/database_privileges/diff.rs @@ -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::{ diff --git a/src/core/database_privileges/editor.rs b/src/core/database_privileges/editor.rs index 62cf8c3..c817c23 100644 --- a/src/core/database_privileges/editor.rs +++ b/src/core/database_privileges/editor.rs @@ -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; diff --git a/src/core/protocol/request_response.rs b/src/core/protocol/request_response.rs index 49b5c19..a0e831e 100644 --- a/src/core/protocol/request_response.rs +++ b/src/core/protocol/request_response.rs @@ -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, @@ -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 { - 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 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 { - 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 for MySQLDatabase { - fn from(s: String) -> Self { - MySQLDatabase(s) - } -} - #[non_exhaustive] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum Request { diff --git a/src/core/protocol/server_responses.rs b/src/core/protocol/server_responses.rs index 3cdd666..553a097 100644 --- a/src/core/protocol/server_responses.rs +++ b/src/core/protocol/server_responses.rs @@ -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)] diff --git a/src/core/types.rs b/src/core/types.rs new file mode 100644 index 0000000..1c0354a --- /dev/null +++ b/src/core/types.rs @@ -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 { + 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 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 { + 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 for MySQLDatabase { + fn from(s: String) -> Self { + MySQLDatabase(s) + } +} diff --git a/src/server/sql/database_operations.rs b/src/server/sql/database_operations.rs index d47a44e..64da8cc 100644 --- a/src/server/sql/database_operations.rs +++ b/src/server/sql/database_operations.rs @@ -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, diff --git a/src/server/sql/database_privilege_operations.rs b/src/server/sql/database_privilege_operations.rs index 957e643..5eecc2e 100644 --- a/src/server/sql/database_privilege_operations.rs +++ b/src/server/sql/database_privilege_operations.rs @@ -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}, diff --git a/src/server/sql/user_operations.rs b/src/server/sql/user_operations.rs index 5c6047e..bbb71e4 100644 --- a/src/server/sql/user_operations.rs +++ b/src/server/sql/user_operations.rs @@ -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},