From 7bf8609983d11d2790a06d7a1e78b83f9f32e4e1 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 7 Jun 2023 15:31:22 +0200 Subject: [PATCH] feat: self-referencing impl that doesn't work --- src/model.rs | 83 ++++++++++++++++++++++++++++++++-------------------- src/proto.rs | 72 +++++++++++++++++++++++++++++---------------- 2 files changed, 98 insertions(+), 57 deletions(-) diff --git a/src/model.rs b/src/model.rs index fa57352..ecca014 100644 --- a/src/model.rs +++ b/src/model.rs @@ -2,40 +2,59 @@ use std::str::FromStr; use reqwest::{Method, Result, Client, Response, Url}; -use crate::proto::Item; +impl Request { + pub async fn send(self) -> Result { + match self { + Self::Object { url, method, header, body, description: _ } => { + let method = Method::from_bytes( + method.as_bytes() // TODO lol? + ).unwrap_or(Method::GET); // TODO throw an error rather than replacing it silently -impl Item { - pub async fn send(&self) -> Result { - if let Some(items) = self.item { - let tasks = vec![]; - for i in items { - tasks.push( - tokio::spawn(async { i.send().await }) - ) - } - for t in tasks { - t.await.unwrap()?; - } + let url = Url::from_str(&url.to_string()).unwrap(); + let mut req = Client::new().request(method, url); + + if let Some(headers) = header { + for h in headers { + req = req.header(h.key.clone(), h.value.clone()) + } + } + + if let Some(body) = body { + req = req.body(body.to_string()); + } + + req.send().await + }, + Self::String(url) => reqwest::get(url).await } - let method = Method::from_bytes( - self.request.method.as_bytes() // TODO lol? - ).unwrap_or(Method::GET); // TODO throw an error rather than replacing it silently - - let url = Url::from_str(&self.request.url.to_string()).unwrap(); - - let mut req = Client::new().request(method, url); - - if let Some(headers) = &self.request.header { - for h in headers { - req = req.header(h.key.clone(), h.value.clone()) - } - } - - if let Some(body) = &self.request.body { - req = req.body(body.to_string()); - } - - req.send().await + } +} + +impl PostWomanCollection { // TODO repeated code from Item.collect() + pub fn collect(&self) -> Vec { + let mut out = Vec::new(); + for i in &self.item { + out.append(&mut i.collect()) + } + out + } +} + +impl Item { + pub fn collect(&self) -> Vec { + let mut out = Vec::new(); // TODO very inefficient to always allocate + + if let Some(items) = &self.item { + for item in items { + out.append(&mut item.collect()); + } + } + + if let Some(req) = &self.request { + out.push(req.clone()); + } + + out } } diff --git a/src/proto.rs b/src/proto.rs index 560a20f..f055890 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -1,58 +1,80 @@ use serde::{Serialize, Deserialize}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct PostWomanCollection { pub variables: Vec, // TODO these sure aren't just strings for sure... pub info: CollectionInfo, pub item: Vec, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct CollectionInfo { pub name: String, pub description: Option, - pub schema: String, - - pub _postman_id: Option, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Item { pub name: String, pub event: Option>, pub request: Option, - pub response: Option>, // TODO surely isn't just strings + pub response: Option>, pub item: Option>, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Response { + pub name: String, + pub code: u16, + pub header: String, + // pub cookie: Vec, + pub body: Body, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Event { pub listen: String, pub script: Script, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Script { pub r#type: String, pub exec: Vec, } -#[derive(Serialize, Deserialize, Debug)] -pub struct Request { - pub url: Url, - pub method: String, - pub header: Option>, - pub body: Option, - pub description: Option, +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum Request { + Object { + url: Url, + method: String, + header: Option>, + body: Option, + description: Option, + }, + String(String), } -#[derive(Serialize, Deserialize, Debug)] +impl ToString for Request { + fn to_string(&self) -> String { + match self { + Self::String(x) => x.clone(), + Self::Object { + url, + method: _, header: _, body: _, description: _ + } => url.to_string(), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Header { pub key: String, pub value: String, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Query { pub key: String, pub value: String, @@ -66,23 +88,23 @@ impl ToString for Query { } } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum Body { - Json(serde_json::Value), - Text(String), + Object(serde_json::Value), + String(String), } impl ToString for Body { fn to_string(&self) -> String { match self { - Body::Json(v) => serde_json::to_string(v).unwrap(), - Body::Text(s) => s.clone(), + Body::Object(v) => serde_json::to_string(v).unwrap(), + Body::String(s) => s.clone(), } } } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum Url { Object { @@ -131,7 +153,7 @@ impl ToString for Url { // barebones custom error -// #[derive(Debug)] +// #[derive(Debug, Clone)] // pub struct PostWomanError { // msg : String, // }