diff --git a/src/model/host.rs b/src/model/host.rs new file mode 100644 index 0000000..fe206fd --- /dev/null +++ b/src/model/host.rs @@ -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("."), + } + } +} diff --git a/src/model/path.rs b/src/model/path.rs new file mode 100644 index 0000000..e580f31 --- /dev/null +++ b/src/model/path.rs @@ -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 + }, + } + } +} diff --git a/src/model/query.rs b/src/model/query.rs new file mode 100644 index 0000000..edea562 --- /dev/null +++ b/src/model/query.rs @@ -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 { + fn make_query(&self) -> String { + self.iter() + .filter_map(|x| Some(format!("{}={}", x.key?, x.value?))) + .collect::>() + .join("&") + } +} + +impl IntoQueryString for Vec { + fn make_query(&self) -> String { + self.iter() + .filter_map(|x| Some(format!("{}={}", x.key?, x.value?))) + .collect::>() + .join("&") + } +} + +impl IntoQueryString for Vec { + fn make_query(&self) -> String { + self.iter() + .filter_map(|x| Some(format!("{}={}", x.key?, x.value?))) + .collect::>() + .join("&") + } +} diff --git a/src/model/url.rs b/src/model/url.rs new file mode 100644 index 0000000..842b54d --- /dev/null +++ b/src/model/url.rs @@ -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 +}