From 3a619ecfb05564ec0ae5e100526de4dd5815a56e Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Jun 2023 15:42:07 +0200 Subject: [PATCH] chore: modularized it better --- src/main.rs | 3 +- src/model/collector.rs | 46 +++++++++++++++++++++++++++ src/{model.rs => model/mod.rs} | 58 ++++------------------------------ src/model/request.rs | 23 ++++++++++++++ 4 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 src/model/collector.rs rename src/{model.rs => model/mod.rs} (53%) create mode 100644 src/model/request.rs diff --git a/src/main.rs b/src/main.rs index d190582..bafcba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ -// mod proto; -// mod model; +mod model; mod collector; use clap::{Parser, Subcommand}; diff --git a/src/model/collector.rs b/src/model/collector.rs new file mode 100644 index 0000000..a2931d9 --- /dev/null +++ b/src/model/collector.rs @@ -0,0 +1,46 @@ +use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0}; + +pub trait CollectRequests { + fn from_self(&self) -> Vec; +} + +impl CollectRequests for v1_0_0::Spec { + fn from_self(&self) -> Vec { + todo!() + } +} + +impl CollectRequests for v2_0_0::Spec { + fn from_self(&self) -> &reqwest::Request { + let mut requests = Vec::new(); + if let Some(r) = root.request { + let clazz = match r { + v2_0_0::RequestUnion::String(url) => v2_0_0::RequestClass { + auth: None, + body: None, + certificate: None, + description: None, + header: None, + method: None, + proxy: None, + url: Some(v2_0_0::Url::String(url)), + }, + v2_0_0::RequestUnion::RequestClass(r) => r, + }; + requests.push(clazz.make_request()); + } + if let Some(sub) = root.item { + for item in sub { + requests.append(&mut self.from_self()); + } + + } + requests + } +} + +impl CollectRequests for v2_1_0::Spec { + fn from_self(&self) -> Vec { + todo!() + } +} diff --git a/src/model.rs b/src/model/mod.rs similarity index 53% rename from src/model.rs rename to src/model/mod.rs index 72d043a..6044dc3 100644 --- a/src/model.rs +++ b/src/model/mod.rs @@ -1,5 +1,10 @@ +mod request; +mod collector; + use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0}; +use self::collector::CollectRequests; + pub struct PostWomanCollection { collection: PostmanCollection } @@ -39,66 +44,17 @@ impl PostWomanCollection { PostmanCollection::V2_0_0(spec) => { let mut out = Vec::new(); for item in spec.item { - out.append(&mut collect_requests_2_0_0_r(&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 collect_requests_2_1_0_r(&item)); + out.append(&mut spec.from_self()); } out }, } } } - -pub fn collect_requests_1_0_0_r(root: &v1_0_0::Spec) -> Vec { - todo!() -} - -pub trait IntoRequest { - fn make_request(&self) -> reqwest::Request; -} - -impl IntoRequest for v2_0_0::RequestClass { - fn make_request(&self) -> reqwest::Request { - todo!() - } -} - -pub trait CollectRequests { - fn from_self(&self) -> &reqwest::Request; -} - -pub fn collect_requests_2_0_0_r(root: &v2_0_0::Items) -> Vec { - let mut requests = Vec::new(); - if let Some(r) = root.request { - let clazz = match r { - v2_0_0::RequestUnion::String(url) => v2_0_0::RequestClass { - auth: None, - body: None, - certificate: None, - description: None, - header: None, - method: None, - proxy: None, - url: Some(v2_0_0::Url::String(url)), - }, - v2_0_0::RequestUnion::RequestClass(r) => r, - }; - requests.push(clazz.make_request()); - } - if let Some(sub) = root.item { - for item in sub { - requests.append(&mut collect_requests_2_0_0_r(&item)); - } - - } - requests -} - -pub fn collect_requests_2_1_0_r(root: &v2_1_0::Items) -> Vec { - todo!() -} diff --git a/src/model/request.rs b/src/model/request.rs new file mode 100644 index 0000000..ad3e670 --- /dev/null +++ b/src/model/request.rs @@ -0,0 +1,23 @@ +use postman_collection::{PostmanCollection, v1_0_0, v2_0_0, v2_1_0}; + +pub trait IntoRequest { + fn make_request(&self) -> reqwest::Request; +} + +impl IntoRequest for v2_0_0::RequestClass { + fn make_request(&self) -> reqwest::Request { + todo!() + } +} + +impl IntoRequest for v2_1_0::RequestClass { + fn make_request(&self) -> reqwest::Request { + todo!() + } +} + +impl IntoRequest for v1_0_0::RequestClass { + fn make_request(&self) -> reqwest::Request { + todo!() + } +}