Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
fix: remove clone from screen stack
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Mar 27, 2024
1 parent 1bd55b8 commit f55ebd1
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 172 deletions.
98 changes: 20 additions & 78 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl<'a> App<'a> {
pub fn redraw(&mut self) {
if self.selected.is_some() {
let selected = (self.selected, self.cursor);
self.goto_screen(self.current_screen.clone());
let current = self.current_screen.clone();
self.goto_screen(&current);
self.state.as_mut().unwrap().select(selected.0);
self.cursor = selected.1;
}
Expand Down Expand Up @@ -121,12 +122,9 @@ impl<'a> App<'a> {
}
}

pub fn goto_screen(&mut self, screen: Screen) {
pub fn goto_screen(&mut self, screen: &Screen) {
self.input.reset();
// Push New/Next Screen Onto The Screen Stack
self.screen_stack.push(screen.clone());

// Set The Current Screen
self.current_screen = screen.clone();

self.cursor = 0;
Expand All @@ -149,7 +147,7 @@ impl<'a> App<'a> {
}
Screen::SavedCommands(col_name) => {
self.items = self
.get_saved_commands(col_name)
.get_saved_commands(*col_name)
.unwrap_or_default()
.iter()
.map(|cmd| {
Expand Down Expand Up @@ -177,23 +175,25 @@ impl<'a> App<'a> {
pub fn go_back_screen(&mut self) {
let last = self.screen_stack.pop().unwrap_or_default(); // current screen
match self.screen_stack.last() {
// is that recursion in prod????? o_0
Some(screen) if screen == &last => self.go_back_screen(),
Some(
Screen::InputMenu(_)
| Screen::CmdMenu(_)
| Screen::ColMenu(_)
| Screen::KeysMenu(_),
) => self.go_back_screen(),
Some(Screen::RequestBodyInput) => self.goto_screen(Screen::Method),
Some(Screen::RequestMenu(_)) => {
// This is to remove errors from the stack
self.goto_screen(Screen::RequestMenu(String::new()));
Some(Screen::RequestBodyInput) => self.goto_screen(&Screen::Method),
Some(Screen::RequestMenu(ref e)) => {
if e.to_lowercase().contains("error") || e.to_lowercase().contains("alert") {
self.goto_screen(&Screen::RequestMenu(String::new()));
} else {
self.goto_screen(&Screen::Method);
}
}
Some(screen) => {
self.goto_screen(screen.clone());
self.goto_screen(&screen.clone());
}
_ => self.goto_screen(Screen::Home),
_ => self.goto_screen(&Screen::Home),
}
}

Expand Down Expand Up @@ -303,12 +303,12 @@ impl<'a> App<'a> {
Ok(_) => self.set_response(&command.get_response()),
Err(e) => self.set_response(&e),
};
self.goto_screen(Screen::Response(self.response.clone().unwrap()));
self.goto_screen(&Screen::Response(self.response.clone().unwrap()));
}
None => self.goto_screen(Screen::Error("Saved command not found".to_string())),
None => self.goto_screen(&Screen::Error("Saved command not found".to_string())),
}
} else {
self.goto_screen(Screen::Error("Saved command not found".to_string()));
self.goto_screen(&Screen::Error("Saved command not found".to_string()));
}
}

Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a> App<'a> {

pub fn delete_saved_command(&mut self, ind: i32) -> Result<(), rusqlite::Error> {
self.db.delete_command(ind)?;
self.goto_screen(Screen::SavedCommands(None));
self.goto_screen(&Screen::SavedCommands(None));
Ok(())
}

Expand All @@ -362,19 +362,19 @@ impl<'a> App<'a> {

pub fn delete_saved_key(&mut self, index: i32) -> Result<(), rusqlite::Error> {
self.db.as_ref().delete_key(index)?;
self.goto_screen(Screen::SavedKeys);
self.goto_screen(&Screen::SavedKeys);
Ok(())
}

pub fn delete_collection(&mut self, id: i32) -> Result<(), rusqlite::Error> {
self.db.delete_collection(id)?;
self.goto_screen(Screen::SavedCommands(None));
self.goto_screen(&Screen::SavedCommands(None));
Ok(())
}

pub fn rename_collection(&mut self, id: i32, name: &str) -> Result<(), rusqlite::Error> {
self.db.rename_collection(id, name)?;
self.goto_screen(Screen::SavedCollections);
self.goto_screen(&Screen::SavedCollections);
Ok(())
}

Expand Down Expand Up @@ -622,61 +622,3 @@ impl<'a> App<'a> {
}
}
}

#[cfg(test)]
mod tests {

/*
use super::App;
use crate::display::AppOptions;
use crate::request::command::Cmd;
use crate::request::curl::Curl;
// helper return app instance with curl command
fn return_app_cmd() -> App<'static> {
let mut app = App::default();
app.set_command(Box::new(Cmd::Curl(Curl::new())));
app
}
#[test]
fn test_add_app_option() {
let mut app = return_app_cmd();
let url = "https://www.google.com";
app.add_app_option(AppOptions::URL(String::from(url)));
assert!(app.command.get_url() == url);
}
#[test]
fn test_toggle_verbose() {
let mut app = return_app_cmd();
// Add one.
app.add_app_option(crate::display::AppOptions::Verbose);
assert!(app.has_app_option(&AppOptions::Verbose));
// this should toggle
app.add_app_option(AppOptions::Verbose);
assert!(!app.has_app_option(&AppOptions::Verbose));
}
#[test]
fn test_replace_app_opt() {
let mut app = return_app_cmd();
let url = "https://www.google.com".to_string();
app.add_app_option(AppOptions::URL(url.clone()));
assert!(app.command.get_url() == url);
// overwrite the url
let new_url = "https://www.github.com".to_string();
app.add_app_option(AppOptions::URL(new_url.clone()));
assert!(app.command.get_url() == new_url);
}
#[test]
fn test_remove_app_option() {
let mut app = return_app_cmd();
let url = "https://www.google.com";
app.add_app_option(AppOptions::URL(String::from(url)));
app.remove_app_option(&AppOptions::URL(String::from(url)));
}
*/
}
2 changes: 1 addition & 1 deletion src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
KeyCode::Char('a') => {
if app.current_screen == Screen::SavedKeys {
app.goto_screen(Screen::InputMenu(InputOpt::ApiKey));
app.goto_screen(&Screen::InputMenu(InputOpt::ApiKey));
}
}
KeyCode::Char('i') => match app.current_screen {
Expand Down
14 changes: 7 additions & 7 deletions src/screens/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ pub fn handle_authentication_screen(app: &mut App, frame: &mut Frame<'_>) {
handle_screen_defaults(app, frame);
if let Some(num) = app.selected {
match num {
0 => app.goto_screen(Screen::InputMenu(InputOpt::Auth(AuthType::Basic))),
1 => app.goto_screen(Screen::InputMenu(InputOpt::Auth(AuthType::Bearer))),
2 => app.goto_screen(Screen::InputMenu(InputOpt::Auth(AuthType::Digest))),
0 => app.goto_screen(&Screen::InputMenu(InputOpt::Auth(AuthType::Basic))),
1 => app.goto_screen(&Screen::InputMenu(InputOpt::Auth(AuthType::Bearer))),
2 => app.goto_screen(&Screen::InputMenu(InputOpt::Auth(AuthType::Digest))),
3 => {
if varify_aws_auth() {
app.goto_screen(Screen::RequestMenu(String::from(AWS_AUTH_MSG)));
app.goto_screen(&Screen::RequestMenu(String::from(AWS_AUTH_MSG)));
app.add_app_option(AppOptions::Auth(AuthKind::AwsSigv4));
} else {
app.goto_screen(Screen::RequestMenu(String::from(AWS_AUTH_ERROR_MSG)));
app.goto_screen(&Screen::RequestMenu(String::from(AWS_AUTH_ERROR_MSG)));
}
}
4 => {
if app.has_auth() {
app.remove_app_option(&AppOptions::Auth(AuthKind::None));
}
app.add_app_option(AppOptions::Auth(AuthKind::Spnego));
app.goto_screen(Screen::RequestMenu(String::from("")));
app.goto_screen(&Screen::RequestMenu(String::from("")));
}
5 => {
if app.has_auth() {
app.remove_app_option(&AppOptions::Auth(AuthKind::None));
}
app.add_app_option(AppOptions::Auth(AuthKind::Ntlm));
app.goto_screen(Screen::RequestMenu(String::from(
app.goto_screen(&Screen::RequestMenu(String::from(
"Alert: NTLM Auth Enabled",
)));
}
Expand Down
20 changes: 10 additions & 10 deletions src/screens/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ pub fn handle_collection_menu(app: &mut App, frame: &mut Frame<'_>) {
handle_screen_defaults(app, frame);
match app.selected {
// Import New Collection
Some(0) => app.goto_screen(Screen::InputMenu(InputOpt::ImportCollection)),
Some(0) => app.goto_screen(&Screen::InputMenu(InputOpt::ImportCollection)),
// Create New Collection
Some(1) => app.goto_screen(Screen::InputMenu(InputOpt::CreateCollection)),
Some(1) => app.goto_screen(&Screen::InputMenu(InputOpt::CreateCollection)),
// View Saved Collections
Some(2) => app.goto_screen(Screen::ViewSavedCollections),
Some(2) => app.goto_screen(&Screen::ViewSavedCollections),
// Cancel
Some(3) => {
app.goto_screen(Screen::Home);
app.goto_screen(&Screen::Home);
}
_ => {}
}
Expand All @@ -49,7 +49,7 @@ pub fn handle_collections_screen(app: &mut App, frame: &mut Frame<'_>) {
frame.size(),
);
if let Some(selected) = app.selected {
app.goto_screen(Screen::ColMenu(selected));
app.goto_screen(&Screen::ColMenu(selected));
}
}

Expand Down Expand Up @@ -110,20 +110,20 @@ pub fn handle_collection_alert_menu(app: &mut App, frame: &mut Frame<'_>, cmd: u
match app.selected {
// View Requests in collection
Some(0) => {
app.goto_screen(Screen::SavedCommands(Some(selected.id)));
app.goto_screen(&Screen::SavedCommands(Some(selected.id)));
}
// Rename Collection
Some(1) => app.goto_screen(Screen::InputMenu(InputOpt::RenameCollection(selected.id))),
Some(1) => app.goto_screen(&Screen::InputMenu(InputOpt::RenameCollection(selected.id))),
// delete collection
Some(2) => {
if let Err(e) = app.delete_collection(selected.id) {
app.goto_screen(Screen::Error(e.to_string()));
app.goto_screen(&Screen::Error(e.to_string()));
}
app.goto_screen(Screen::Success);
app.goto_screen(&Screen::Success);
}
// cancel
Some(3) => {
app.goto_screen(Screen::ViewSavedCollections);
app.goto_screen(&Screen::ViewSavedCollections);
}
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions src/screens/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn handle_headers_screen(app: &mut App, frame: &mut Frame<'_>) {

match app.selected {
// add custom headers
Some(0) => app.goto_screen(Screen::InputMenu(InputOpt::Headers)),
Some(0) => app.goto_screen(&Screen::InputMenu(InputOpt::Headers)),
//
// add content-type application/json
Some(1) => {
Expand All @@ -30,7 +30,7 @@ pub fn handle_headers_screen(app: &mut App, frame: &mut Frame<'_>) {
}
//
// return to request menu
Some(4) => app.goto_screen(Screen::RequestMenu(String::new())),
Some(4) => app.goto_screen(&Screen::RequestMenu(String::new())),
_ => {}
}
}
8 changes: 4 additions & 4 deletions src/screens/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub fn handle_home_screen(app: &mut App, frame: &mut Frame<'_>) {
if let Some(num) = app.selected {
match num {
0 => {
app.goto_screen(Screen::Method);
app.goto_screen(&Screen::Method);
}
1 => app.goto_screen(Screen::SavedCollections),
2 => app.goto_screen(Screen::SavedKeys),
3 => app.goto_screen(Screen::SavedCommands(None)),
1 => app.goto_screen(&Screen::SavedCollections),
2 => app.goto_screen(&Screen::SavedKeys),
3 => app.goto_screen(&Screen::SavedCommands(None)),
_ => {}
}
}
Expand Down
Loading

0 comments on commit f55ebd1

Please sign in to comment.