feat: initial work on traits for url generation

This commit is contained in:
əlemi 2023-06-21 00:32:57 +02:00
parent 55118ef3a0
commit 39c446485f
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 170 additions and 0 deletions

32
src/model/host.rs Normal file
View file

@ -0,0 +1,32 @@
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
pub trait IntoHost {
fn make_host(&self) -> String;
}
impl IntoHost for v2_1_0::Host {
fn make_host(&self) -> String {
match self {
v2_1_0::Host::String(x) => x.clone(),
v2_1_0::Host::StringArray(v) => v.join("."),
}
}
}
impl IntoHost for v2_0_0::Host {
fn make_host(&self) -> String {
match self {
v2_0_0::Host::String(x) => x.clone(),
v2_0_0::Host::StringArray(v) => v.join("."),
}
}
}
impl IntoHost for v1_0_0::Host {
fn make_host(&self) -> String {
match self {
v1_0_0::Host::String(x) => x.clone(),
v1_0_0::Host::StringArray(v) => v.join("."),
}
}
}

37
src/model/path.rs Normal file
View file

@ -0,0 +1,37 @@
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
pub trait IntoPath {
fn make_path(&self) -> String;
}
impl IntoPath for v1_0_0::UrlPath {
fn make_path(&self) -> String {
todo!()
}
}
impl IntoPath for v2_0_0::UrlPath {
fn make_path(&self) -> String {
todo!()
}
}
impl IntoPath for v2_1_0::UrlPath {
fn make_path(&self) -> String {
match self {
v2_1_0::UrlPath::String(x) => x.clone(),
v2_1_0::UrlPath::UnionArray(v) => {
let mut out = String::new();
for p in v {
match p {
v2_1_0::PathElement::PathClass(v2_1_0::PathClass { value: Some(x), ..}) => out.push_str(&x),
v2_1_0::PathElement::String(x) => out.push_str(&x),
_ => {},
}
out.push('/');
}
out
},
}
}
}

32
src/model/query.rs Normal file
View file

@ -0,0 +1,32 @@
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
pub trait IntoQueryString {
fn make_query(&self) -> String;
}
impl IntoQueryString for Vec<v2_1_0::QueryParam> {
fn make_query(&self) -> String {
self.iter()
.filter_map(|x| Some(format!("{}={}", x.key?, x.value?)))
.collect::<Vec<String>>()
.join("&")
}
}
impl IntoQueryString for Vec<v2_0_0::QueryParam> {
fn make_query(&self) -> String {
self.iter()
.filter_map(|x| Some(format!("{}={}", x.key?, x.value?)))
.collect::<Vec<String>>()
.join("&")
}
}
impl IntoQueryString for Vec<v1_0_0::QueryParam> {
fn make_query(&self) -> String {
self.iter()
.filter_map(|x| Some(format!("{}={}", x.key?, x.value?)))
.collect::<Vec<String>>()
.join("&")
}
}

69
src/model/url.rs Normal file
View file

@ -0,0 +1,69 @@
use postman_collection::{v1_0_0, v2_0_0, v2_1_0};
use super::{query::IntoQueryString, host::IntoHost, path::IntoPath};
pub trait IntoUrl {
fn make_url(&self) -> String;
}
impl IntoUrl for v1_0_0::Url {
fn make_url(&self) -> String {
todo!()
}
}
impl IntoUrl for v2_0_0::Url {
fn make_url(&self) -> String {
todo!()
}
}
impl IntoUrl for v2_1_0::Url {
fn make_url(&self) -> String {
match self {
v2_1_0::Url::String(x) => x.clone(),
v2_1_0::Url::UrlClass(x) => {
match x {
v2_1_0::UrlClass { raw: Some(x), .. } => x.clone(),
v2_1_0::UrlClass {
raw: None,
hash, host, path, port, protocol, query, variable
} => build_url(
&protocol.unwrap_or("http".into()),
&host.map(|x| x.make_host()).unwrap_or("localhost".into()),
&path.map(|x| x.make_path().as_str()),
&query.map(|x| x.make_query().as_str()),
&hash.map(|x| x.as_str())
)
}
},
}
}
}
fn build_url(
proto: &str,
host: &str,
path: &Option<&str>,
query: &Option<&str>,
hash: &Option<&str>
) -> String {
let mut url = format!("{}://{}", proto, host);
if let Some(p) = path {
url.push('/');
url.push_str(p);
}
if let Some(q) = query {
url.push('?');
url.push_str(&q);
}
if let Some(h) = hash {
url.push('#');
url.push_str(&h);
}
url
}