Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (draft) import from cURL #49

Merged
merged 5 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jsonxf = "1.1.1"
toml = "0.8.11"
envfile = "0.2.1"
parse_postman_collection = "0.2.3"
parser4curls = "0.1"
clap = { version = "4.5.0", features = ["derive", "color"] }
arboard = "3.3.2"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread", "macros"] }
Expand Down
80 changes: 52 additions & 28 deletions src/app/app_logic/collection.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::sync::{Arc, RwLock};
use crate::app::app::App;
use crate::app::startup::args::ARGS;
use crate::request::auth::Auth;
use crate::request::body::ContentType;
use crate::request::collection::Collection;
use crate::request::request::{DEFAULT_HEADERS, Request};
use crate::request::request::{Request, DEFAULT_HEADERS};
use crate::request::settings::RequestSettings;
use std::sync::{Arc, RwLock};

impl App<'_> {
pub fn reset_inputs(&mut self) {
Expand All @@ -25,7 +25,8 @@ impl App<'_> {
let local_selected_request = self.get_selected_request_as_local();
let selected_request = local_selected_request.read().unwrap();

self.url_text_input.enter_str(&selected_request.url_with_params_to_string());
self.url_text_input
.enter_str(&selected_request.url_with_params_to_string());
self.query_params_table.rows = selected_request.params.clone();
self.headers_table.rows = selected_request.headers.clone();

Expand All @@ -35,10 +36,12 @@ impl App<'_> {
let param_text = match selection {
(x, 0) => selected_request.params[x].data.0.clone(),
(x, 1) => selected_request.params[x].data.1.clone(),
_ => String::new() // Should not happen
_ => String::new(), // Should not happen
};

self.query_params_table.selection_text_input.enter_str(&param_text);
self.query_params_table
.selection_text_input
.enter_str(&param_text);
}

match &selected_request.auth {
Expand Down Expand Up @@ -67,10 +70,12 @@ impl App<'_> {
let header_text = match selection {
(x, 0) => selected_request.headers[x].data.0.clone(),
(x, 1) => selected_request.headers[x].data.1.clone(),
_ => String::new() // Should not happen
_ => String::new(), // Should not happen
};

self.headers_table.selection_text_input.enter_str(&header_text);
self.headers_table
.selection_text_input
.enter_str(&header_text);
}

match &selected_request.body {
Expand All @@ -87,24 +92,30 @@ impl App<'_> {
let form_text = match selection {
(x, 0) => form[x].data.0.clone(),
(x, 1) => form[x].data.1.clone(),
_ => String::new() // Should not happen
_ => String::new(), // Should not happen
};

self.body_form_table.selection_text_input.enter_str(&form_text);
self.body_form_table
.selection_text_input
.enter_str(&form_text);
}

self.refresh_body_textarea(&String::new());
}
ContentType::File(file_path) => {
ContentType::File(file_path) => {
self.body_file_text_input.enter_str(file_path);
},
ContentType::Raw(body) | ContentType::Json(body) | ContentType::Xml(body) | ContentType::Html(body) | ContentType::Javascript(body) => {
}
ContentType::Raw(body)
| ContentType::Json(body)
| ContentType::Xml(body)
| ContentType::Html(body)
| ContentType::Javascript(body) => {
self.body_form_table.rows = Vec::new();
self.refresh_body_textarea(body);
}
}
}

pub fn reset_cursors(&mut self) {
self.url_text_input.reset_cursor();
self.query_params_table.selection_text_input.reset_cursor();
Expand All @@ -123,7 +134,7 @@ impl App<'_> {
self.update_headers_selection();
self.update_body_table_selection();
self.refresh_result_scrollbars();

self.select_request_state();
}
}
Expand All @@ -138,22 +149,22 @@ impl App<'_> {
match self.collections_tree.state.selected().len() {
1 => {
self.collections_tree.state.toggle_selected();
},
}
2 => {
self.select_request();
},
}
_ => {}
}
}

pub fn new_element(&mut self) {
match self.creation_popup.selection {
0 => self.create_new_collection_state(),
1 => self.create_new_request_state(),
_ => {}
}
}

pub fn new_collection(&mut self) {
let new_collection_name = &self.new_collection_input.text;

Expand All @@ -171,12 +182,14 @@ impl App<'_> {
let new_collection = Collection {
name: new_collection_name.clone(),
requests: vec![],
path: ARGS.directory.join(format!("{}.json", new_collection_name.clone()))
path: ARGS
.directory
.join(format!("{}.json", new_collection_name.clone())),
};

self.collections.push(new_collection);

let collection_index= self.collections.len() - 1;
let collection_index = self.collections.len() - 1;

self.save_collection_to_file(collection_index);
self.normal_state();
Expand All @@ -198,7 +211,9 @@ impl App<'_> {

let selected_collection = self.new_request_popup.selected_collection;

self.collections[selected_collection].requests.push(Arc::new(RwLock::new(new_request)));
self.collections[selected_collection]
.requests
.push(Arc::new(RwLock::new(new_request)));

self.save_collection_to_file(selected_collection);
self.normal_state();
Expand Down Expand Up @@ -228,7 +243,9 @@ impl App<'_> {

pub fn delete_request(&mut self) {
let selected_request_index = self.collections_tree.state.selected();
self.collections[selected_request_index[0]].requests.remove(selected_request_index[1]);
self.collections[selected_request_index[0]]
.requests
.remove(selected_request_index[1]);

self.collections_tree.state.select(Vec::new());
self.collections_tree.selected = None;
Expand Down Expand Up @@ -270,14 +287,17 @@ impl App<'_> {
}

let selected_request_index = self.collections_tree.state.selected();
let local_selected_request = self.get_request_as_local_from_indexes(&(selected_request_index[0], selected_request_index[1]));
let local_selected_request = self.get_request_as_local_from_indexes(&(
selected_request_index[0],
selected_request_index[1],
));

{
let mut selected_request = local_selected_request.write().unwrap();

selected_request.name = new_request_name.to_string();
}

self.save_collection_to_file(selected_request_index[0]);
self.normal_state();
}
Expand All @@ -301,7 +321,9 @@ impl App<'_> {
selection[1] -= 1;

// Insert the request at its new index
self.collections[selection[0]].requests.insert(selection[1], request);
self.collections[selection[0]]
.requests
.insert(selection[1], request);

// Update the selection in order to move with the element
self.collections_tree.state.select(selection.clone());
Expand All @@ -328,11 +350,13 @@ impl App<'_> {
selection[1] += 1;

// Insert the request at its new index
self.collections[selection[0]].requests.insert(selection[1], request);
self.collections[selection[0]]
.requests
.insert(selection[1], request);

// Update the selection in order to move with the element
self.collections_tree.state.select(selection.clone());

self.save_collection_to_file(selection[0]);
}
}
}