core: split mysql user/db into separate types module
This commit is contained in:
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -4,10 +4,13 @@ use tokio_stream::StreamExt;
|
||||
|
||||
use crate::{
|
||||
client::commands::erroneous_server_response,
|
||||
core::protocol::{
|
||||
ClientToServerMessageStream, MySQLUser, Request, Response, print_drop_users_output_status,
|
||||
core::{
|
||||
protocol::{
|
||||
ClientToServerMessageStream, Request, Response, print_drop_users_output_status,
|
||||
print_drop_users_output_status_json,
|
||||
},
|
||||
types::MySQLUser,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -4,10 +4,13 @@ use tokio_stream::StreamExt;
|
||||
|
||||
use crate::{
|
||||
client::commands::erroneous_server_response,
|
||||
core::protocol::{
|
||||
ClientToServerMessageStream, MySQLUser, Request, Response, print_lock_users_output_status,
|
||||
core::{
|
||||
protocol::{
|
||||
ClientToServerMessageStream, Request, Response, print_lock_users_output_status,
|
||||
print_lock_users_output_status_json,
|
||||
},
|
||||
types::MySQLUser,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
|
||||
@@ -6,10 +6,13 @@ use tokio_stream::StreamExt;
|
||||
|
||||
use crate::{
|
||||
client::commands::erroneous_server_response,
|
||||
core::protocol::{
|
||||
ClientToServerMessageStream, ListUsersError, MySQLUser, Request, Response,
|
||||
core::{
|
||||
protocol::{
|
||||
ClientToServerMessageStream, ListUsersError, Request, Response,
|
||||
print_set_password_output_status,
|
||||
},
|
||||
types::MySQLUser,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -2,3 +2,4 @@ pub mod bootstrap;
|
||||
pub mod common;
|
||||
pub mod database_privileges;
|
||||
pub mod protocol;
|
||||
pub mod types;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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::{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
93
src/core/types.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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},
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user