feat: self-referencing impl that doesn't work

This commit is contained in:
əlemi 2023-06-07 15:31:22 +02:00
parent e92dd51505
commit 7bf8609983
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 98 additions and 57 deletions

View file

@ -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<Response> {
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<Response> {
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<Request> {
let mut out = Vec::new();
for i in &self.item {
out.append(&mut i.collect())
}
out
}
}
impl Item {
pub fn collect(&self) -> Vec<Request> {
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
}
}

View file

@ -1,58 +1,80 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PostWomanCollection {
pub variables: Vec<String>, // TODO these sure aren't just strings for sure...
pub info: CollectionInfo,
pub item: Vec<Item>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CollectionInfo {
pub name: String,
pub description: Option<String>,
pub schema: String,
pub _postman_id: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Item {
pub name: String,
pub event: Option<Vec<Event>>,
pub request: Option<Request>,
pub response: Option<Vec<String>>, // TODO surely isn't just strings
pub response: Option<Vec<Response>>,
pub item: Option<Vec<Item>>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Response {
pub name: String,
pub code: u16,
pub header: String,
// pub cookie: Vec<String>,
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<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Request {
pub url: Url,
pub method: String,
pub header: Option<Vec<Header>>,
pub body: Option<Body>,
pub description: Option<String>,
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Request {
Object {
url: Url,
method: String,
header: Option<Vec<Header>>,
body: Option<Body>,
description: Option<String>,
},
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,
// }