Remove randomly generated example, run clippy. Fixes #6

This commit is contained in:
Felix Albrigtsen 2024-07-12 21:59:44 +02:00
parent 1a5dc96f0d
commit 4a94265eae
4 changed files with 26 additions and 24 deletions

1
Cargo.lock generated
View File

@ -892,7 +892,6 @@ dependencies = [
"log",
"nix",
"prettytable",
"rand",
"ratatui",
"rpassword",
"serde",

View File

@ -13,7 +13,6 @@ itertools = "0.12.1"
log = "0.4.21"
nix = { version = "0.28.0", features = ["user"] }
prettytable = "0.10.0"
rand = "0.8.5"
ratatui = { version = "0.26.2", optional = true }
rpassword = "7.3.1"
serde = "1.0.198"
@ -33,4 +32,4 @@ path = "src/main.rs"
[profile.release]
strip = true
lto = true
codegen-units = 1
codegen-units = 1

View File

@ -3,7 +3,6 @@ use clap::Parser;
use indoc::indoc;
use itertools::Itertools;
use prettytable::{Cell, Row, Table};
use rand::prelude::*;
use sqlx::{Connection, MySqlConnection};
use crate::core::{
@ -357,29 +356,29 @@ fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<Data
}
Ok(DatabasePrivileges {
db: (*line_parts.get(0).unwrap()).to_owned(),
db: (*line_parts.first().unwrap()).to_owned(),
user: (*line_parts.get(1).unwrap()).to_owned(),
select_priv: parse_permission(*line_parts.get(2).unwrap())
select_priv: parse_permission(line_parts.get(2).unwrap())
.context("Could not parse SELECT privilege")?,
insert_priv: parse_permission(*line_parts.get(3).unwrap())
insert_priv: parse_permission(line_parts.get(3).unwrap())
.context("Could not parse INSERT privilege")?,
update_priv: parse_permission(*line_parts.get(4).unwrap())
update_priv: parse_permission(line_parts.get(4).unwrap())
.context("Could not parse UPDATE privilege")?,
delete_priv: parse_permission(*line_parts.get(5).unwrap())
delete_priv: parse_permission(line_parts.get(5).unwrap())
.context("Could not parse DELETE privilege")?,
create_priv: parse_permission(*line_parts.get(6).unwrap())
create_priv: parse_permission(line_parts.get(6).unwrap())
.context("Could not parse CREATE privilege")?,
drop_priv: parse_permission(*line_parts.get(7).unwrap())
drop_priv: parse_permission(line_parts.get(7).unwrap())
.context("Could not parse DROP privilege")?,
alter_priv: parse_permission(*line_parts.get(8).unwrap())
alter_priv: parse_permission(line_parts.get(8).unwrap())
.context("Could not parse ALTER privilege")?,
index_priv: parse_permission(*line_parts.get(9).unwrap())
index_priv: parse_permission(line_parts.get(9).unwrap())
.context("Could not parse INDEX privilege")?,
create_tmp_table_priv: parse_permission(*line_parts.get(10).unwrap())
create_tmp_table_priv: parse_permission(line_parts.get(10).unwrap())
.context("Could not parse CREATE TEMPORARY TABLE privilege")?,
lock_tables_priv: parse_permission(*line_parts.get(11).unwrap())
lock_tables_priv: parse_permission(line_parts.get(11).unwrap())
.context("Could not parse LOCK TABLES privilege")?,
references_priv: parse_permission(*line_parts.get(12).unwrap())
references_priv: parse_permission(line_parts.get(12).unwrap())
.context("Could not parse REFERENCES privilege")?,
})
})
@ -436,20 +435,26 @@ async fn edit_permissions(
} else {
let comment = indoc! {r#"
# Welcome to the permission editor.
# To add permissions
# Each line defines what permissions a single user has on a single database.
# The first two columns respectively represent the database name and the user, and the remaining columns are the permissions.
# If the user should have a permission, write 'Y', otherwise write 'N'.
#
# Lines starting with '#' are comments and will be ignored.
"#};
let header = DATABASE_PRIVILEGE_FIELDS
.map(db_priv_field_human_readable_name)
.join("\t");
let example_line = {
let unix_user = get_current_unix_user()?;
let mut rng = thread_rng();
let random_yes_nos = (0..(DATABASE_PRIVILEGE_FIELDS.len() - 2))
.map(|_| ['Y', 'N'].choose(&mut rng).unwrap())
.join("\t");
let permissions_bools = [
true, true, true, true, false, false, false, false, false, false, false,
]
.map(yn)
.join("\t");
format!(
"# {}_db\t{}_user\t{}",
unix_user.name, unix_user.name, random_yes_nos
unix_user.name, unix_user.name, permissions_bools
)
};

View File

@ -162,14 +162,13 @@ impl DatabasePrivileges {
diff: DATABASE_PRIVILEGE_FIELDS
.into_iter()
.skip(2)
.map(|field| {
.filter_map(|field| {
diff_single_priv(
self.get_privilege_by_name(field),
other.get_privilege_by_name(field),
field,
)
})
.flatten()
.collect(),
}
}