chore: return request tree representing original nesting

This commit is contained in:
əlemi 2023-06-21 00:23:55 +02:00
parent aa13cfac10
commit 15dca26744
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 57 additions and 26 deletions

View file

@ -2,33 +2,49 @@ use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
use super::request::IntoRequest;
pub enum RequestNode {
Leaf(reqwest::RequestBuilder),
Branch(Vec<RequestTree>),
}
pub struct RequestTree {
pub name: String,
pub request: RequestNode,
}
pub trait CollectRequests {
fn collect_requests(&self) -> Vec<reqwest::Request>;
fn collect_requests(&self) -> RequestTree;
}
impl CollectRequests for v1_0_0::Spec {
fn collect_requests(&self) -> Vec<reqwest::Request> {
fn collect_requests(&self) -> RequestTree {
todo!()
}
}
impl CollectRequests for v2_0_0::Spec {
fn collect_requests(&self) -> Vec<reqwest::Request> {
fn collect_requests(&self) -> RequestTree {
let mut requests = Vec::new();
for item in &self.item {
requests.append(&mut item.collect_requests());
requests.push(item.collect_requests());
}
RequestTree {
name: self.info.name.clone(),
request: RequestNode::Branch(requests),
}
requests
}
}
impl CollectRequests for v2_1_0::Spec {
fn collect_requests(&self) -> Vec<reqwest::Request> {
fn collect_requests(&self) -> RequestTree {
let mut requests = Vec::new();
for item in &self.item {
requests.append(&mut item.collect_requests());
requests.push(item.collect_requests());
}
RequestTree {
name: self.info.name.clone(),
request: RequestNode::Branch(requests),
}
requests
}
}
@ -39,8 +55,11 @@ impl CollectRequests for v2_1_0::Spec {
// }
impl CollectRequests for v2_0_0::Items {
fn collect_requests(&self) -> Vec<reqwest::Request> {
let mut requests = Vec::new();
fn collect_requests(&self) -> RequestTree {
let request : RequestNode;
if self.request.is_some() && self.item.is_some() {
panic!("some node has both a single request and child requests!");
}
if let Some(r) = &self.request {
let clazz = match r {
v2_0_0::RequestUnion::String(url) => v2_0_0::RequestClass {
@ -49,20 +68,26 @@ impl CollectRequests for v2_0_0::Items {
},
v2_0_0::RequestUnion::RequestClass(r) => r.clone(),
};
requests.push(clazz.make_request());
}
if let Some(sub) = &self.item {
request = RequestNode::Leaf(clazz.make_request());
} else if let Some(sub) = &self.item {
let mut requests = Vec::new();
for item in sub {
requests.append(&mut item.collect_requests());
requests.push(item.collect_requests());
}
request = RequestNode::Branch(requests);
} else {
request = RequestNode::Branch(Vec::new()); // TODO make if/elseif/else nicer?
}
RequestTree {
name: self.name.as_ref().unwrap_or(&"".to_string()).to_string(), // TODO meme
request,
}
requests
}
}
impl CollectRequests for v2_1_0::Items {
fn collect_requests(&self) -> Vec<reqwest::Request> {
let mut requests = Vec::new();
fn collect_requests(&self) -> RequestTree {
let request : RequestNode;
if let Some(r) = &self.request {
let clazz = match r {
v2_1_0::RequestUnion::String(url) => v2_1_0::RequestClass {
@ -77,13 +102,19 @@ impl CollectRequests for v2_1_0::Items {
},
v2_1_0::RequestUnion::RequestClass(r) => r.clone(),
};
requests.push(clazz.make_request());
}
if let Some(sub) = &self.item {
request = RequestNode::Leaf(clazz.make_request());
} else if let Some(sub) = &self.item {
let mut requests = Vec::new();
for item in sub {
requests.append(&mut item.collect_requests());
requests.push(item.collect_requests());
}
request = RequestNode::Branch(requests);
} else {
request = RequestNode::Branch(Vec::new()); // TODO make if/elseif/else nicer?
}
RequestTree {
name: self.name.as_ref().unwrap_or(&"".to_string()).to_string(), // TODO meme
request,
}
requests
}
}

View file

@ -1,9 +1,9 @@
mod request;
mod collector;
pub mod request;
pub mod collector;
use postman_collection::{PostmanCollection, v2_0_0, v2_1_0};
use self::collector::CollectRequests;
use self::collector::{CollectRequests, RequestTree};
#[derive(Debug)]
pub struct PostWomanCollection {
@ -52,7 +52,7 @@ impl PostWomanCollection {
}
}
pub fn requests(&self) -> Vec<reqwest::Request> {
pub fn requests(&self) -> RequestTree {
match &self.collection {
PostmanCollection::V1_0_0(_) => todo!(),
PostmanCollection::V2_0_0(spec) => spec.collect_requests(),