Skip to content

Commit

Permalink
Introduced forbid unsafe and unwrap on all crates
Browse files Browse the repository at this point in the history
Refactored: Mobilized types into own modules
Refactored: Bundled 4 callbacks into own trait to shorten argument
count in some functions
  • Loading branch information
BoolPurist committed Mar 28, 2024
1 parent 6404b53 commit 991c714
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 281 deletions.
4 changes: 3 additions & 1 deletion usermgmt/src/ldap_cli_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl LdapCredential for LdapCliCredential {

fn set_password(&mut self, new: String) {
self.password = OnceCell::new();
self.password.set(new).unwrap();
self.password
.set(new)
.expect("Once cell is cleared the line above");
}
}
3 changes: 3 additions & 0 deletions usermgmt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![deny(clippy::unwrap_used)]
#![forbid(unsafe_code)]

use clap::Parser;
use cli_ssh_credential::CliSshCredential;
use ldap_cli_credential::LdapCliCredential;
Expand Down
33 changes: 29 additions & 4 deletions usermgmt_gui/src/drawing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::prelude::*;

pub mod draw_add_state;
pub mod draw_delete_state;
pub mod draw_listing_of_users;
Expand All @@ -9,6 +7,33 @@ pub mod about;
pub mod configuration;
pub mod draw_utils;

pub fn draw_ssh_connection(window: &mut UsermgmtWindow, ui: &mut egui::Ui) {
draw_utils::draw_ssh_credentials(ui, &window.settings, &mut window.ssh_state);
pub trait ProduceIoStatusMessages<T> {
fn msg_init(&mut self) -> String;
fn msg_loading(&mut self) -> String;
fn msg_success(&mut self, resource: &T) -> String;
fn msg_error(&mut self) -> String;
}

impl<T, I, L, S, E> ProduceIoStatusMessages<T> for (I, L, S, E)
where
I: FnMut() -> String,
L: FnMut() -> String,
S: FnMut(&T) -> String,
E: FnMut() -> String,
{
fn msg_init(&mut self) -> String {
self.0()
}

fn msg_loading(&mut self) -> String {
self.1()
}

fn msg_success(&mut self, resource: &T) -> String {
self.2(resource)
}

fn msg_error(&mut self) -> String {
self.3()
}
}
67 changes: 37 additions & 30 deletions usermgmt_gui/src/drawing/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
pub type CacheForConfFields = Rc<RefCell<HashMap<&'static str, LabelTyp>>>;

type ToolTippLabel<'a> = Option<&'a str>;
type LabelTyp = Rc<str>;
pub type CacheForConfFields = Rc<RefCell<HashMap<&'static str, LabelTyp>>>;
use crate::{current_selected_view::ConfigurationState, prelude::*};
use usermgmt_lib::config::{LoadedMgmtConfig, MgmtConfig};

use super::draw_utils::{GroupDrawing, TextFieldEntry};
use crate::{current_selected_view::ConfigurationState, prelude::*};
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
use usermgmt_lib::config::{LoadedMgmtConfig, MgmtConfig};

pub fn draw(window: &mut UsermgmtWindow, ui: &mut egui::Ui) {
let mut can_reload = true;
Expand All @@ -16,30 +17,34 @@ pub fn draw(window: &mut UsermgmtWindow, ui: &mut egui::Ui) {
settings,
texts.conf_load_group(),
window.conf_state.io_conf.status(),
|| texts.conf_load_init_msg().to_owned(),
|| {
can_reload = false;
texts.conf_load_loading_msg().to_owned()
},
|loaded| {
format!(
"{}:\n{:?}",
texts.conf_load_success_msg().to_owned(),
&loaded.path
)
},
|| texts.conf_load_err_msg().to_owned(),
(
|| texts.conf_load_init_msg().to_owned(),
|| {
can_reload = false;
texts.conf_load_loading_msg().to_owned()
},
|loaded: &LoadedMgmtConfig| {
format!(
"{}:\n{:?}",
texts.conf_load_success_msg().to_owned(),
&loaded.path
)
},
|| texts.conf_load_err_msg().to_owned(),
),
);

draw_utils::draw_status_msg_w_label(
ui,
settings,
texts.conf_save_group(),
window.conf_state.io_save_conf.status(),
|| texts.conf_save_init_msg().to_owned(),
|| texts.conf_save_loading_msg().to_owned(),
|path| format!("{}:\n{:?}", texts.conf_save_success_msg().to_owned(), path),
|| texts.conf_save_err_msg().to_owned(),
(
|| texts.conf_save_init_msg().to_owned(),
|| texts.conf_save_loading_msg().to_owned(),
|path: &PathBuf| format!("{}:\n{:?}", texts.conf_save_success_msg().to_owned(), path),
|| texts.conf_save_err_msg().to_owned(),
),
);

draw_utils::draw_file_path(ui, window);
Expand Down Expand Up @@ -115,7 +120,7 @@ fn draw_fields(window: &mut ConfigurationState, settings: &Settings, ui: &mut eg
draw_utils::entry_field(
ui,
settings,
&mut TextFieldEntry::new_opt(&label, val).tool_tip(tool_tip),
&mut TextFieldEntry::new_opt(&label, val).with_tool_tip(tool_tip),
);
}
ConfiField::Single {
Expand All @@ -126,7 +131,7 @@ fn draw_fields(window: &mut ConfigurationState, settings: &Settings, ui: &mut eg
draw_utils::entry_field(
ui,
settings,
&mut TextFieldEntry::new(&label, val).tool_tip(tool_tip),
&mut TextFieldEntry::new(&label, val).with_tool_tip(tool_tip),
);
}
ConfiField::List {
Expand All @@ -138,7 +143,7 @@ fn draw_fields(window: &mut ConfigurationState, settings: &Settings, ui: &mut eg
ui,
settings,
val,
&GroupDrawing::new(&label).tooltip(tool_tip),
&GroupDrawing::new(&label).with_tooltip(tool_tip),
);
draw_sep = false;
}
Expand Down Expand Up @@ -183,7 +188,7 @@ fn draw_fields(window: &mut ConfigurationState, settings: &Settings, ui: &mut eg
ui,
settings,
&mut TextFieldEntry::new_opt(&label, &mut as_string)
.tool_tip(tool_tip),
.with_tool_tip(tool_tip),
);
*val = as_string.map(PathBuf::from);
}
Expand All @@ -206,9 +211,13 @@ fn snake_to_label(input: &'static str, repos: CacheForConfFields) -> Rc<str> {
input
.split(SPLIT_BY)
.map(|word| {
let first = word.chars().next().unwrap().to_uppercase();
let list: String = first.chain(word.chars().skip(1)).collect();
list
let mut chars = word.chars();
if let Some(first) = chars.next() {
let upper_first = first.to_uppercase().next().unwrap_or(first);
std::iter::once(upper_first).chain(chars).collect()
} else {
String::default()
}
})
.collect::<Vec<String>>()
.join(JOIN_BY),
Expand Down Expand Up @@ -358,8 +367,6 @@ impl PartialOrd for ConfiField<'_> {
}
}

type ToolTippLabel<'a> = Option<&'a str>;

/// Generates conversion from a rust value into ConfiField which
/// is used to render a value in egui.
/// # Usage
Expand Down
12 changes: 7 additions & 5 deletions usermgmt_gui/src/drawing/draw_add_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) {
ui,
&window.settings,
&mut adding_fields.qos,
&GroupDrawing::new(texts.qos()).with_tooltip(tooltips.qos()),
&GroupDrawing::new(texts.qos()).add_tooltip(tooltips.qos()),
);
});
}
Expand All @@ -72,10 +72,12 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) {
ui,
&window.settings,
adding_fields.adding_res_io.status(),
|| "No user added yet".to_string(),
|| format!("User ({}) is being added", last_username),
|username| format!("User ({}) was added", username),
|| format!("Failed to add user ({})", last_username),
(
|| "No user added yet".to_string(),
|| format!("User ({}) is being added", last_username),
|username: &String| format!("User ({}) was added", username),
|| format!("Failed to add user ({})", last_username),
),
);
let allow_adding_user = adding_fields.all_needed_fields_filled();

Expand Down
10 changes: 6 additions & 4 deletions usermgmt_gui/src/drawing/draw_delete_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ pub fn draw(ui: &mut egui::Ui, window: &mut UsermgmtWindow) {
ui,
&window.settings,
remove_state.remove_res_io.status(),
|| "No user remove yet".to_owned(),
|| format!("In the process of removing user ({}).", last_username),
|username| format!("Removed user ({}) !", username),
|| format!("Failed to remove user ({}).", last_username),
(
|| "No user remove yet".to_owned(),
|| format!("In the process of removing user ({}).", last_username),
|username: &String| format!("Removed user ({}) !", username),
|| format!("Failed to remove user ({}).", last_username),
),
);
}

Expand Down
Loading

0 comments on commit 991c714

Please sign in to comment.