fix: make rust-analyzer happy

This commit is contained in:
əlemi 2023-06-15 15:59:03 +02:00
parent 3a619ecfb0
commit 2effe880dc
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 81 additions and 72 deletions

View file

@ -3,10 +3,7 @@ mod collector;
use clap::{Parser, Subcommand};
use postman_collection::{PostmanCollection, v2_1_0::Spec};
use crate::collector::{collect, url, send};
// use crate::proto::{Item, Request, Header};
use crate::{collector::{collect, url, send}, model::PostWomanCollection};
/// API tester and debugger from your CLI
#[derive(Parser, Debug)]
@ -59,34 +56,16 @@ pub enum PostWomanActions {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = PostWomanArgs::parse();
let collection =
match postman_collection::from_path(args.collection) {
Ok(PostmanCollection::V2_1_0(spec)) => spec,
Ok(PostmanCollection::V1_0_0(_)) => {
eprintln!("collection using v1.0.0 format! only 2.1.0 is allowed");
Spec::default()
},
Ok(PostmanCollection::V2_0_0(_)) => {
eprintln!("collection using v2.0.0 format! only 2.1.0 is allowed");
Spec::default()
},
Err(e) => {
eprintln!("error loading collection: {}", e);
Spec::default()
}
};
let collection = PostWomanCollection::from_path(&args.collection)?;
if args.verbose {
println!("╶┐ * {}", collection.info.name);
if let Some(descr) = &collection.info.description {
match descr {
postman_collection::v2_1_0::DescriptionUnion::Description(x) => {
if let Some(d) = &x.content { println!("{}", d) };
if let Some(v) = &x.version { println!("{}", v) };
},
postman_collection::v2_1_0::DescriptionUnion::String(x) => println!("{}", x),
}
println!("╶┐ * {}", collection.name());
if let Some(descr) = &collection.description() {
println!("{}", descr);
}
// if let Some(version) = &collection.version() {
// println!(" │ {}", version);
// }
println!("");
}
@ -139,26 +118,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// if args.verbose { println!(" │╵") }
// },
PostWomanActions::Test { } => {
let reqs = collect(collection);
let reqs = collection.requests();
let mut tasks = Vec::new();
// let mut tasks = Vec::new();
for req in reqs {
let t = tokio::spawn(async move {
let url = url(&req);
let r = send(req).await?;
println!("{} >> {}", url, r.status());
if args.verbose {
println!("{}", r.text().await?.replace("\n", "\n"));
}
Ok::<(), reqwest::Error>(())
});
tasks.push(t);
}
// for req in reqs {
// let t = tokio::spawn(async move {
// let url = url(&req);
// let r = send(req).await?;
// println!(" ├ {} >> {}", url, r.status());
// if args.verbose {
// println!(" │ {}", r.text().await?.replace("\n", "\n │ "));
// }
// Ok::<(), reqwest::Error>(())
// });
// tasks.push(t);
// }
for t in tasks {
t.await??;
}
// for t in tasks {
// t.await??;
// }
},
PostWomanActions::Show { } => {
println!("{:?}", collection);

View file

@ -1,19 +1,47 @@
use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0};
use super::request::IntoRequest;
pub trait CollectRequests {
fn from_self(&self) -> Vec<reqwest::Request>;
fn collect_requests(&self) -> Vec<reqwest::Request>;
}
impl CollectRequests for v1_0_0::Spec {
fn from_self(&self) -> Vec<reqwest::Request> {
fn collect_requests(&self) -> Vec<reqwest::Request> {
todo!()
}
}
impl CollectRequests for v2_0_0::Spec {
fn from_self(&self) -> &reqwest::Request {
fn collect_requests(&self) -> Vec<reqwest::Request> {
let mut requests = Vec::new();
if let Some(r) = root.request {
for item in &self.item {
requests.append(&mut item.collect_requests());
}
requests
}
}
impl CollectRequests for v2_1_0::Spec {
fn collect_requests(&self) -> Vec<reqwest::Request> {
let mut requests = Vec::new();
for item in &self.item {
requests.append(&mut item.collect_requests());
}
requests
}
}
// impl CollectRequests for v1_0_0::Items {
// fn collect_requests(&self) -> Vec<reqwest::Request> {
// todo!()
// }
// }
impl CollectRequests for v2_0_0::Items {
fn collect_requests(&self) -> Vec<reqwest::Request> {
let mut requests = Vec::new();
if let Some(r) = &self.request {
let clazz = match r {
v2_0_0::RequestUnion::String(url) => v2_0_0::RequestClass {
auth: None,
@ -23,15 +51,15 @@ impl CollectRequests for v2_0_0::Spec {
header: None,
method: None,
proxy: None,
url: Some(v2_0_0::Url::String(url)),
url: Some(v2_0_0::Url::String(url.clone())),
},
v2_0_0::RequestUnion::RequestClass(r) => r,
v2_0_0::RequestUnion::RequestClass(r) => r.clone(),
};
requests.push(clazz.make_request());
}
if let Some(sub) = root.item {
if let Some(sub) = &self.item {
for item in sub {
requests.append(&mut self.from_self());
requests.append(&mut item.collect_requests());
}
}
@ -39,8 +67,8 @@ impl CollectRequests for v2_0_0::Spec {
}
}
impl CollectRequests for v2_1_0::Spec {
fn from_self(&self) -> Vec<reqwest::Request> {
impl CollectRequests for v2_1_0::Items {
fn collect_requests(&self) -> Vec<reqwest::Request> {
todo!()
}
}

View file

@ -5,6 +5,7 @@ use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0};
use self::collector::CollectRequests;
#[derive(Debug)]
pub struct PostWomanCollection {
collection: PostmanCollection
}
@ -16,6 +17,19 @@ impl From<PostmanCollection> for PostWomanCollection {
}
impl PostWomanCollection {
pub fn from_path(path: &str) -> postman_collection::errors::Result<Self> {
Ok(postman_collection::from_path(path)?.into())
}
pub fn name(&self) -> &String {
match &self.collection {
PostmanCollection::V1_0_0(spec) => todo!(),
PostmanCollection::V2_0_0(spec) => &spec.info.name,
PostmanCollection::V2_1_0(spec) => &spec.info.name,
}
}
pub fn description(&self) -> Option<&String> {
match &self.collection {
PostmanCollection::V1_0_0(spec) => {
@ -39,22 +53,10 @@ impl PostWomanCollection {
}
pub fn requests(&self) -> Vec<reqwest::Request> {
match self.collection {
match &self.collection {
PostmanCollection::V1_0_0(_) => todo!(),
PostmanCollection::V2_0_0(spec) => {
let mut out = Vec::new();
for item in spec.item {
out.append(&mut spec.from_self());
}
out
},
PostmanCollection::V2_1_0(spec) => {
let mut out = Vec::new();
for item in spec.item {
out.append(&mut spec.from_self());
}
out
},
PostmanCollection::V2_0_0(spec) => spec.collect_requests(),
PostmanCollection::V2_1_0(spec) => spec.collect_requests(),
}
}
}

View file

@ -16,7 +16,7 @@ impl IntoRequest for v2_1_0::RequestClass {
}
}
impl IntoRequest for v1_0_0::RequestClass {
impl IntoRequest for v1_0_0::Request {
fn make_request(&self) -> reqwest::Request {
todo!()
}