From 21c2fc2e8b8d8e479d208558c7060b9bcadd004c Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 6 Aug 2024 23:48:31 +0200 Subject: [PATCH] cargo fmt + clippy --- src/cli.rs | 2 +- src/cli/database_command.rs | 11 ++++++----- src/cli/mysql_admutils_compatibility.rs | 2 +- .../mysql_dbadm.rs | 8 +------- src/core/common.rs | 10 ++++++++-- src/core/database_operations.rs | 3 ++- src/core/user_operations.rs | 18 +++++++++++------- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e558735..7c80cfc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,3 @@ pub mod database_command; -pub mod user_command; pub mod mysql_admutils_compatibility; +pub mod user_command; diff --git a/src/cli/database_command.rs b/src/cli/database_command.rs index 998524e..23c9fc3 100644 --- a/src/cli/database_command.rs +++ b/src/cli/database_command.rs @@ -11,7 +11,8 @@ use crate::core::{ database_operations::{ apply_permission_diffs, db_priv_field_human_readable_name, diff_permissions, yn, DatabasePrivileges, DATABASE_PRIVILEGE_FIELDS, - }, user_operations::user_exists, + }, + user_operations::user_exists, }; // TODO: Support batch creation/dropping,showing of databases, @@ -520,10 +521,10 @@ pub async fn edit_permissions( }; for row in permissions_to_change.iter() { - if !user_exists(&row.user, conn).await? { - // TODO: allow user to return and correct their mistake - anyhow::bail!("User {} does not exist", row.user); - } + if !user_exists(&row.user, conn).await? { + // TODO: allow user to return and correct their mistake + anyhow::bail!("User {} does not exist", row.user); + } } let diffs = diff_permissions(permission_data, &permissions_to_change).await; diff --git a/src/cli/mysql_admutils_compatibility.rs b/src/cli/mysql_admutils_compatibility.rs index fa5c322..df4bf14 100644 --- a/src/cli/mysql_admutils_compatibility.rs +++ b/src/cli/mysql_admutils_compatibility.rs @@ -1,3 +1,3 @@ pub mod common; pub mod mysql_dbadm; -pub mod mysql_useradm; \ No newline at end of file +pub mod mysql_useradm; diff --git a/src/cli/mysql_admutils_compatibility/mysql_dbadm.rs b/src/cli/mysql_admutils_compatibility/mysql_dbadm.rs index 49c8bb2..9a0081e 100644 --- a/src/cli/mysql_admutils_compatibility/mysql_dbadm.rs +++ b/src/cli/mysql_admutils_compatibility/mysql_dbadm.rs @@ -54,12 +54,7 @@ pub struct Args { /// This is a compatibility layer for the mysql-dbadm command. /// Please consider using the newer mysqladm command instead. #[derive(Parser)] -#[command( - version, - about, - disable_help_subcommand = true, - verbatim_doc_comment, -)] +#[command(version, about, disable_help_subcommand = true, verbatim_doc_comment)] pub enum Command { /// create the DATABASE(s). Create(CreateArgs), @@ -73,7 +68,6 @@ pub enum Command { // TODO: make this output more verbatim_doc_comment-like, // without messing up the indentation. - /// change permissions for the DATABASE(s). Your /// favorite editor will be started, allowing you /// to make changes to the permission table. diff --git a/src/core/common.rs b/src/core/common.rs index e5060c7..c641b14 100644 --- a/src/core/common.rs +++ b/src/core/common.rs @@ -71,7 +71,10 @@ pub fn validate_name_token(name: &str) -> anyhow::Result<()> { anyhow::bail!("Database name is too long. Maximum length is 64 characters."); } - if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') { + if !name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') + { anyhow::bail!( indoc! {r#" Invalid characters in name: '{}' @@ -85,7 +88,10 @@ pub fn validate_name_token(name: &str) -> anyhow::Result<()> { Ok(()) } -pub fn validate_ownership_by_user_prefix<'a>(name: &'a str, user: &User) -> anyhow::Result<&'a str> { +pub fn validate_ownership_by_user_prefix<'a>( + name: &'a str, + user: &User, +) -> anyhow::Result<&'a str> { let user_groups = get_unix_groups(user)?; let mut split_name = name.split('_'); diff --git a/src/core/database_operations.rs b/src/core/database_operations.rs index 49ff651..744e843 100644 --- a/src/core/database_operations.rs +++ b/src/core/database_operations.rs @@ -8,7 +8,8 @@ use serde::{Deserialize, Serialize}; use sqlx::{mysql::MySqlRow, prelude::*, MySqlConnection}; use super::common::{ - create_user_group_matching_regex, get_current_unix_user, quote_identifier, validate_name_token, validate_ownership_by_user_prefix + create_user_group_matching_regex, get_current_unix_user, quote_identifier, validate_name_token, + validate_ownership_by_user_prefix, }; pub async fn create_database(name: &str, conn: &mut MySqlConnection) -> anyhow::Result<()> { diff --git a/src/core/user_operations.rs b/src/core/user_operations.rs index ab7c0bf..eaf7966 100644 --- a/src/core/user_operations.rs +++ b/src/core/user_operations.rs @@ -5,7 +5,10 @@ use sqlx::{prelude::*, MySqlConnection}; use crate::core::common::quote_literal; -use super::common::{create_user_group_matching_regex, get_current_unix_user, validate_name_token, validate_ownership_by_user_prefix}; +use super::common::{ + create_user_group_matching_regex, get_current_unix_user, validate_name_token, + validate_ownership_by_user_prefix, +}; pub async fn user_exists(db_user: &str, conn: &mut MySqlConnection) -> anyhow::Result { let unix_user = get_current_unix_user()?; @@ -102,11 +105,11 @@ pub async fn password_is_set_for_database_user( validate_user_name(db_user, &unix_user)?; let user_has_password = sqlx::query_as::<_, PasswordIsSet>( - "SELECT authentication_string != '' FROM mysql.user WHERE User = ?" - ) - .bind(db_user) - .fetch_optional(conn) - .await?; + "SELECT authentication_string != '' FROM mysql.user WHERE User = ?", + ) + .bind(db_user) + .fetch_optional(conn) + .await?; Ok(user_has_password.map(|PasswordIsSet(is_set)| is_set)) } @@ -167,7 +170,8 @@ pub async fn get_database_user_for_user( /// to validate the database name ourselves to prevent SQL injection. pub fn validate_user_name(name: &str, user: &User) -> anyhow::Result<()> { validate_name_token(name).context(format!("Invalid username: '{}'", name))?; - validate_ownership_by_user_prefix(name, user).context(format!("Invalid username: '{}'", name))?; + validate_ownership_by_user_prefix(name, user) + .context(format!("Invalid username: '{}'", name))?; Ok(()) }