Merge remote-tracking branch 'origin/5.0.x'

This commit is contained in:
William Cheng 2020-05-07 14:27:56 +08:00
commit 98e54f0cc1
34 changed files with 2839 additions and 648 deletions

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;

View File

@ -332,15 +332,23 @@
// Header parameters // Header parameters
{{/-first}} {{/-first}}
{{^isMapContainer}} {{^isMapContainer}}
{{#required}} {{^required}}
match param_{{{paramName}}} {
Some(param_{{{paramName}}}) => {
{{/required}}
request.headers_mut().append( request.headers_mut().append(
HeaderName::from_static("{{{nameInLowerCase}}}"), HeaderName::from_static("{{{nameInLowerCase}}}"),
header::IntoHeaderValue(param_{{{paramName}}}.clone()).into()); match header::IntoHeaderValue(param_{{{paramName}}}.clone()).try_into() {
{{/required}} Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header {{{paramName}}} - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
{{^required}} {{^required}}
param_{{{paramName}}}.map(|value| request.headers_mut().append( },
HeaderName::from_static("{{{nameInLowerCase}}}"), None => {}
header::IntoHeaderValue(value.clone()).into())); }
{{/required}} {{/required}}
{{/isMapContainer}} {{/isMapContainer}}
{{#isMapContainer}} {{#isMapContainer}}
@ -359,6 +367,14 @@
Some(response_{{{name}}}) => response_{{{name}}}.clone(), Some(response_{{{name}}}) => response_{{{name}}}.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header {{{baseName}}} for response {{{code}}} was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header {{{baseName}}} for response {{{code}}} was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_{{{name}}} = match TryInto::<header::IntoHeaderValue<{{{dataType}}}>>::try_into(response_{{{name}}}) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header {{baseName}} for response {{code}} - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_{{{name}}} = response_{{{name}}}.0;
{{/headers}} {{/headers}}
let body = response.into_body(); let body = response.into_body();
Box::new( Box::new(
@ -402,7 +418,7 @@
{ {
body: body, body: body,
{{/-first}} {{/-first}}
{{{name}}}: (*Into::<header::IntoHeaderValue<{{{dataType}}}>>::into(response_{{{name}}})).clone(), {{{name}}}: response_{{name}},
{{#-last}} {{#-last}}
} }
{{/-last}} {{/-last}}
@ -416,7 +432,7 @@
{{#-first}} {{#-first}}
{ {
{{/-first}} {{/-first}}
{{{name}}}: (*Into::<header::IntoHeaderValue<{{{dataType}}}>>::into(response_{{{name}}})).clone(), {{{name}}}: response_{{name}},
{{#-last}} {{#-last}}
} }
{{/-last}} {{/-last}}

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -124,16 +124,38 @@ impl ::std::str::FromStr for {{{classname}}} {
// Methods for converting between header::IntoHeaderValue<{{{classname}}}> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<{{{classname}}}> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<{{{classname}}}>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<{{{classname}}}>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<{{{classname}}}>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<{{{classname}}}>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for {{classname}} - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<{{{classname}}}> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<{{{classname}}}> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<{{{classname}}} as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <{{{classname}}} as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into {{classname}} - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]

View File

@ -87,20 +87,35 @@
{{/-first}} {{/-first}}
let param_{{{paramName}}} = headers.get(HeaderName::from_static("{{{nameInLowerCase}}}")); let param_{{{paramName}}} = headers.get(HeaderName::from_static("{{{nameInLowerCase}}}"));
{{#required}}
let param_{{{paramName}}} = match param_{{{paramName}}} { let param_{{{paramName}}} = match param_{{{paramName}}} {
Some(v) => header::IntoHeaderValue::<{{{dataType}}}>::from((*v).clone()).0, Some(v) => match header::IntoHeaderValue::<{{{dataType}}}>::try_from((*v).clone()) {
None => return Box::new(future::ok(Response::builder() Ok(result) =>
.status(StatusCode::BAD_REQUEST) {{#required}}
.body(Body::from("Missing or invalid required header {{{baseName}}}")) result.0,
.expect("Unable to create Bad Request response for missing required header {{{baseName}}}"))),
};
{{/required}} {{/required}}
{{^required}} {{^required}}
let param_{{{paramName}}} = param_{{{paramName}}}.map(|p| { Some(result.0),
header::IntoHeaderValue::<{{{dataType}}}>::from((*p).clone()).0
});
{{/required}} {{/required}}
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Invalid header {{{baseName}}} - {}", err)))
.expect("Unable to create Bad Request response for invalid header {{{baseName}}}")));
},
},
None => {
{{#required}}
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Missing required header {{{baseName}}}"))
.expect("Unable to create Bad Request response for missing required header {{{baseName}}}")));
{{/required}}
{{^required}}
None
{{/required}}
}
};
{{#-last}} {{#-last}}
{{/-last}} {{/-last}}
@ -486,11 +501,23 @@
{{/headers}} {{/headers}}
{{/dataType}} {{/dataType}}
=> { => {
{{#headers}}
let {{{name}}} = match header::IntoHeaderValue({{{name}}}).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling {{name}} header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
{{/headers}}
*response.status_mut() = StatusCode::from_u16({{{code}}}).expect("Unable to turn {{{code}}} into a StatusCode"); *response.status_mut() = StatusCode::from_u16({{{code}}}).expect("Unable to turn {{{code}}} into a StatusCode");
{{#headers}} {{#headers}}
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("{{{nameInLowerCase}}}"), HeaderName::from_static("{{{nameInLowerCase}}}"),
header::IntoHeaderValue({{name}}).into() {{name}}
); );
{{/headers}} {{/headers}}
{{#produces}} {{#produces}}

View File

@ -227,6 +227,9 @@ paths:
Success-Info: Success-Info:
schema: schema:
type: String type: String
Bool-Header:
schema:
type: bool
Object-Header: Object-Header:
schema: schema:
$ref: "#/components/schemas/ObjectHeader" $ref: "#/components/schemas/ObjectHeader"

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -8,16 +8,38 @@ use crate::header;
// Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<InlineObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<InlineObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for InlineObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<InlineObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <InlineObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into InlineObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -111,16 +133,38 @@ impl std::str::FromStr for InlineObject {
// Methods for converting between header::IntoHeaderValue<MultipartRelatedRequest> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<MultipartRelatedRequest> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<MultipartRelatedRequest>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<MultipartRelatedRequest>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<MultipartRelatedRequest>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<MultipartRelatedRequest>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for MultipartRelatedRequest - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRelatedRequest> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRelatedRequest> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<MultipartRelatedRequest as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <MultipartRelatedRequest as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into MultipartRelatedRequest - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -223,16 +267,38 @@ impl std::str::FromStr for MultipartRelatedRequest {
// Methods for converting between header::IntoHeaderValue<MultipartRequest> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<MultipartRequest> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<MultipartRequest>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<MultipartRequest>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<MultipartRequest>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<MultipartRequest>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for MultipartRequest - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRequest> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRequest> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<MultipartRequest as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <MultipartRequest as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into MultipartRequest - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -349,16 +415,38 @@ impl std::str::FromStr for MultipartRequest {
// Methods for converting between header::IntoHeaderValue<MultipartRequestObjectField> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<MultipartRequestObjectField> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<MultipartRequestObjectField>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<MultipartRequestObjectField>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<MultipartRequestObjectField>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<MultipartRequestObjectField>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for MultipartRequestObjectField - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRequestObjectField> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<MultipartRequestObjectField> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<MultipartRequestObjectField as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <MultipartRequestObjectField as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into MultipartRequestObjectField - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -8,16 +8,38 @@ use crate::header;
// Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<InlineObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<InlineObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for InlineObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<InlineObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <InlineObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into InlineObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]

View File

@ -214,6 +214,11 @@ paths:
schema: schema:
type: String type: String
style: simple style: simple
Bool-Header:
explode: false
schema:
type: bool
style: simple
Object-Header: Object-Header:
explode: false explode: false
schema: schema:

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -151,9 +153,22 @@ where
// Header parameters // Header parameters
let param_information = headers.get(HeaderName::from_static("information")); let param_information = headers.get(HeaderName::from_static("information"));
let param_information = param_information.map(|p| { let param_information = match param_information {
header::IntoHeaderValue::<String>::from((*p).clone()).0 Some(v) => match header::IntoHeaderValue::<String>::try_from((*v).clone()) {
}); Ok(result) =>
Some(result.0),
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Invalid header Information - {}", err)))
.expect("Unable to create Bad Request response for invalid header Information")));
},
},
None => {
None
}
};
Box::new({ Box::new({
{{ {{

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -537,7 +538,13 @@ impl<C, F> Api<C> for Client<F> where
// Header parameters // Header parameters
request.headers_mut().append( request.headers_mut().append(
HeaderName::from_static("x-header"), HeaderName::from_static("x-header"),
header::IntoHeaderValue(param_x_header.clone()).into()); match header::IntoHeaderValue(param_x_header.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header x_header - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
Box::new(self.client_service.request(request) Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e))) .map_err(|e| ApiError(format!("No response received: {}", e)))
@ -1396,10 +1403,38 @@ impl<C, F> Api<C> for Client<F> where
Some(response_success_info) => response_success_info.clone(), Some(response_success_info) => response_success_info.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header Success-Info for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header Success-Info for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_success_info = match TryInto::<header::IntoHeaderValue<String>>::try_into(response_success_info) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header Success-Info for response 200 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_success_info = response_success_info.0;
let response_bool_header = match response.headers().get(HeaderName::from_static("bool-header")) {
Some(response_bool_header) => response_bool_header.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header Bool-Header for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
};
let response_bool_header = match TryInto::<header::IntoHeaderValue<bool>>::try_into(response_bool_header) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header Bool-Header for response 200 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_bool_header = response_bool_header.0;
let response_object_header = match response.headers().get(HeaderName::from_static("object-header")) { let response_object_header = match response.headers().get(HeaderName::from_static("object-header")) {
Some(response_object_header) => response_object_header.clone(), Some(response_object_header) => response_object_header.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header Object-Header for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header Object-Header for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_object_header = match TryInto::<header::IntoHeaderValue<models::ObjectHeader>>::try_into(response_object_header) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header Object-Header for response 200 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_object_header = response_object_header.0;
let body = response.into_body(); let body = response.into_body();
Box::new( Box::new(
body body
@ -1417,8 +1452,9 @@ impl<C, F> Api<C> for Client<F> where
ResponsesWithHeadersGetResponse::Success ResponsesWithHeadersGetResponse::Success
{ {
body: body, body: body,
success_info: (*Into::<header::IntoHeaderValue<String>>::into(response_success_info)).clone(), success_info: response_success_info,
object_header: (*Into::<header::IntoHeaderValue<models::ObjectHeader>>::into(response_object_header)).clone(), bool_header: response_bool_header,
object_header: response_object_header,
} }
}) })
) as Box<dyn Future<Item=_, Error=_> + Send> ) as Box<dyn Future<Item=_, Error=_> + Send>
@ -1428,17 +1464,33 @@ impl<C, F> Api<C> for Client<F> where
Some(response_further_info) => response_further_info.clone(), Some(response_further_info) => response_further_info.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header Further-Info for response 412 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header Further-Info for response 412 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_further_info = match TryInto::<header::IntoHeaderValue<String>>::try_into(response_further_info) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header Further-Info for response 412 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_further_info = response_further_info.0;
let response_failure_info = match response.headers().get(HeaderName::from_static("failure-info")) { let response_failure_info = match response.headers().get(HeaderName::from_static("failure-info")) {
Some(response_failure_info) => response_failure_info.clone(), Some(response_failure_info) => response_failure_info.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header Failure-Info for response 412 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header Failure-Info for response 412 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_failure_info = match TryInto::<header::IntoHeaderValue<String>>::try_into(response_failure_info) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header Failure-Info for response 412 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_failure_info = response_failure_info.0;
let body = response.into_body(); let body = response.into_body();
Box::new( Box::new(
future::ok( future::ok(
ResponsesWithHeadersGetResponse::PreconditionFailed ResponsesWithHeadersGetResponse::PreconditionFailed
{ {
further_info: (*Into::<header::IntoHeaderValue<String>>::into(response_further_info)).clone(), further_info: response_further_info,
failure_info: (*Into::<header::IntoHeaderValue<String>>::into(response_failure_info)).clone(), failure_info: response_failure_info,
} }
) )
) as Box<dyn Future<Item=_, Error=_> + Send> ) as Box<dyn Future<Item=_, Error=_> + Send>

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -119,6 +119,7 @@ pub enum ResponsesWithHeadersGetResponse {
{ {
body: String, body: String,
success_info: String, success_info: String,
bool_header: bool,
object_header: models::ObjectHeader object_header: models::ObjectHeader
} }
, ,

View File

@ -8,16 +8,38 @@ use crate::header;
// Methods for converting between header::IntoHeaderValue<AnotherXmlArray> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<AnotherXmlArray> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<AnotherXmlArray>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<AnotherXmlArray>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<AnotherXmlArray>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<AnotherXmlArray>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for AnotherXmlArray - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<AnotherXmlArray> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<AnotherXmlArray> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<AnotherXmlArray as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <AnotherXmlArray as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into AnotherXmlArray - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -187,16 +209,38 @@ impl AnotherXmlInner {
// Methods for converting between header::IntoHeaderValue<AnotherXmlObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<AnotherXmlObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<AnotherXmlObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<AnotherXmlObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<AnotherXmlObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<AnotherXmlObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for AnotherXmlObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<AnotherXmlObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<AnotherXmlObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<AnotherXmlObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <AnotherXmlObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into AnotherXmlObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -301,16 +345,38 @@ impl AnotherXmlObject {
// Methods for converting between header::IntoHeaderValue<DuplicateXmlObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<DuplicateXmlObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<DuplicateXmlObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<DuplicateXmlObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<DuplicateXmlObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<DuplicateXmlObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for DuplicateXmlObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<DuplicateXmlObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<DuplicateXmlObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<DuplicateXmlObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <DuplicateXmlObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into DuplicateXmlObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -577,16 +643,38 @@ impl Error {
// Methods for converting between header::IntoHeaderValue<InlineResponse201> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<InlineResponse201> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<InlineResponse201>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<InlineResponse201>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<InlineResponse201>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<InlineResponse201>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for InlineResponse201 - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineResponse201> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineResponse201> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<InlineResponse201 as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <InlineResponse201 as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into InlineResponse201 - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -720,16 +808,38 @@ impl MyId {
// Methods for converting between header::IntoHeaderValue<MyIdList> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<MyIdList> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<MyIdList>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<MyIdList>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<MyIdList>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<MyIdList>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for MyIdList - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<MyIdList> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<MyIdList> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<MyIdList as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <MyIdList as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into MyIdList - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -835,16 +945,38 @@ impl MyIdList {
// Methods for converting between header::IntoHeaderValue<NullableTest> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<NullableTest> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<NullableTest>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<NullableTest>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<NullableTest>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<NullableTest>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for NullableTest - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<NullableTest> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<NullableTest> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<NullableTest as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <NullableTest as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into NullableTest - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -999,16 +1131,38 @@ impl NullableTest {
// Methods for converting between header::IntoHeaderValue<ObjectHeader> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectHeader> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectHeader>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectHeader>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectHeader>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectHeader>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectHeader - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectHeader> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectHeader> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectHeader as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectHeader as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectHeader - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -1113,16 +1267,38 @@ impl ObjectHeader {
// Methods for converting between header::IntoHeaderValue<ObjectParam> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectParam> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectParam>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectParam>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectParam>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectParam>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectParam - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectParam> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectParam> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectParam as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectParam as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectParam - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -1227,16 +1403,38 @@ impl ObjectParam {
// Methods for converting between header::IntoHeaderValue<ObjectUntypedProps> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectUntypedProps> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectUntypedProps>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectUntypedProps>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectUntypedProps>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectUntypedProps>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectUntypedProps - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectUntypedProps> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectUntypedProps> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectUntypedProps as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectUntypedProps as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectUntypedProps - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -1354,16 +1552,38 @@ impl ObjectUntypedProps {
// Methods for converting between header::IntoHeaderValue<ObjectWithArrayOfObjects> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectWithArrayOfObjects> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectWithArrayOfObjects>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectWithArrayOfObjects>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectWithArrayOfObjects>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectWithArrayOfObjects>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectWithArrayOfObjects - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectWithArrayOfObjects> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectWithArrayOfObjects> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectWithArrayOfObjects as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectWithArrayOfObjects as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectWithArrayOfObjects - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -1778,16 +1998,38 @@ impl UuidObject {
// Methods for converting between header::IntoHeaderValue<XmlArray> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<XmlArray> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<XmlArray>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<XmlArray>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<XmlArray>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<XmlArray>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for XmlArray - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<XmlArray> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<XmlArray> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<XmlArray as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <XmlArray as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into XmlArray - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -1957,16 +2199,38 @@ impl XmlInner {
// Methods for converting between header::IntoHeaderValue<XmlObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<XmlObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<XmlObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<XmlObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<XmlObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<XmlObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for XmlObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<XmlObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<XmlObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<XmlObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <XmlObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into XmlObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -211,9 +212,20 @@ impl<C, F> CallbackApi<C> for Client<F> where
}); });
// Header parameters // Header parameters
param_information.map(|value| request.headers_mut().append( match param_information {
Some(param_information) => {
request.headers_mut().append(
HeaderName::from_static("information"), HeaderName::from_static("information"),
header::IntoHeaderValue(value.clone()).into())); match header::IntoHeaderValue(param_information.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header information - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
},
None => {}
}
Box::new(self.client_service.request(request) Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e))) .map_err(|e| ApiError(format!("No response received: {}", e)))

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -365,11 +367,23 @@ where
let param_x_header = headers.get(HeaderName::from_static("x-header")); let param_x_header = headers.get(HeaderName::from_static("x-header"));
let param_x_header = match param_x_header { let param_x_header = match param_x_header {
Some(v) => header::IntoHeaderValue::<String>::from((*v).clone()).0, Some(v) => match header::IntoHeaderValue::<String>::try_from((*v).clone()) {
None => return Box::new(future::ok(Response::builder() Ok(result) =>
result.0,
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST) .status(StatusCode::BAD_REQUEST)
.body(Body::from("Missing or invalid required header X-Header")) .body(Body::from(format!("Invalid header X-Header - {}", err)))
.expect("Unable to create Bad Request response for missing required header X-Header"))), .expect("Unable to create Bad Request response for invalid header X-Header")));
},
},
None => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Missing required header X-Header"))
.expect("Unable to create Bad Request response for missing required header X-Header")));
}
}; };
Box::new({ Box::new({
@ -920,17 +934,52 @@ where
{ {
body, body,
success_info, success_info,
bool_header,
object_header object_header
} }
=> { => {
let success_info = match header::IntoHeaderValue(success_info).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling success_info header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
let bool_header = match header::IntoHeaderValue(bool_header).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling bool_header header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
let object_header = match header::IntoHeaderValue(object_header).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling object_header header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
*response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode");
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("success-info"), HeaderName::from_static("success-info"),
header::IntoHeaderValue(success_info).into() success_info
);
response.headers_mut().insert(
HeaderName::from_static("bool-header"),
bool_header
); );
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("object-header"), HeaderName::from_static("object-header"),
header::IntoHeaderValue(object_header).into() object_header
); );
response.headers_mut().insert( response.headers_mut().insert(
CONTENT_TYPE, CONTENT_TYPE,
@ -945,14 +994,34 @@ where
failure_info failure_info
} }
=> { => {
let further_info = match header::IntoHeaderValue(further_info).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling further_info header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
let failure_info = match header::IntoHeaderValue(failure_info).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling failure_info header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
*response.status_mut() = StatusCode::from_u16(412).expect("Unable to turn 412 into a StatusCode"); *response.status_mut() = StatusCode::from_u16(412).expect("Unable to turn 412 into a StatusCode");
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("further-info"), HeaderName::from_static("further-info"),
header::IntoHeaderValue(further_info).into() further_info
); );
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("failure-info"), HeaderName::from_static("failure-info"),
header::IntoHeaderValue(failure_info).into() failure_info
); );
}, },
}, },

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -1358,13 +1359,35 @@ impl<C, F> Api<C> for Client<F> where
}); });
// Header parameters // Header parameters
param_enum_header_string_array.map(|value| request.headers_mut().append( match param_enum_header_string_array {
Some(param_enum_header_string_array) => {
request.headers_mut().append(
HeaderName::from_static("enum_header_string_array"), HeaderName::from_static("enum_header_string_array"),
header::IntoHeaderValue(value.clone()).into())); match header::IntoHeaderValue(param_enum_header_string_array.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header enum_header_string_array - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
},
None => {}
}
param_enum_header_string.map(|value| request.headers_mut().append( match param_enum_header_string {
Some(param_enum_header_string) => {
request.headers_mut().append(
HeaderName::from_static("enum_header_string"), HeaderName::from_static("enum_header_string"),
header::IntoHeaderValue(value.clone()).into())); match header::IntoHeaderValue(param_enum_header_string.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header enum_header_string - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
},
None => {}
}
Box::new(self.client_service.request(request) Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e))) .map_err(|e| ApiError(format!("No response received: {}", e)))
@ -1836,9 +1859,20 @@ impl<C, F> Api<C> for Client<F> where
} }
// Header parameters // Header parameters
param_api_key.map(|value| request.headers_mut().append( match param_api_key {
Some(param_api_key) => {
request.headers_mut().append(
HeaderName::from_static("api_key"), HeaderName::from_static("api_key"),
header::IntoHeaderValue(value.clone()).into())); match header::IntoHeaderValue(param_api_key.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Box::new(future::err(ApiError(format!(
"Invalid header api_key - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
});
},
None => {}
}
Box::new(self.client_service.request(request) Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e))) .map_err(|e| ApiError(format!("No response received: {}", e)))
@ -3431,10 +3465,26 @@ impl<C, F> Api<C> for Client<F> where
Some(response_x_rate_limit) => response_x_rate_limit.clone(), Some(response_x_rate_limit) => response_x_rate_limit.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header X-Rate-Limit for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header X-Rate-Limit for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_x_rate_limit = match TryInto::<header::IntoHeaderValue<i32>>::try_into(response_x_rate_limit) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header X-Rate-Limit for response 200 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_x_rate_limit = response_x_rate_limit.0;
let response_x_expires_after = match response.headers().get(HeaderName::from_static("x-expires-after")) { let response_x_expires_after = match response.headers().get(HeaderName::from_static("x-expires-after")) {
Some(response_x_expires_after) => response_x_expires_after.clone(), Some(response_x_expires_after) => response_x_expires_after.clone(),
None => return Box::new(future::err(ApiError(String::from("Required response header X-Expires-After for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>, None => return Box::new(future::err(ApiError(String::from("Required response header X-Expires-After for response 200 was not found.")))) as Box<dyn Future<Item=_, Error=_> + Send>,
}; };
let response_x_expires_after = match TryInto::<header::IntoHeaderValue<chrono::DateTime::<chrono::Utc>>>::try_into(response_x_expires_after) {
Ok(value) => value,
Err(e) => {
return Box::new(future::err(ApiError(format!("Invalid response header X-Expires-After for response 200 - {}", e)))) as Box<dyn Future<Item=_, Error=_> + Send>;
},
};
let response_x_expires_after = response_x_expires_after.0;
let body = response.into_body(); let body = response.into_body();
Box::new( Box::new(
body body
@ -3454,8 +3504,8 @@ impl<C, F> Api<C> for Client<F> where
LoginUserResponse::SuccessfulOperation LoginUserResponse::SuccessfulOperation
{ {
body: body, body: body,
x_rate_limit: (*Into::<header::IntoHeaderValue<i32>>::into(response_x_rate_limit)).clone(), x_rate_limit: response_x_rate_limit,
x_expires_after: (*Into::<header::IntoHeaderValue<chrono::DateTime::<chrono::Utc>>>::into(response_x_expires_after)).clone(), x_expires_after: response_x_expires_after,
} }
}) })
) as Box<dyn Future<Item=_, Error=_> + Send> ) as Box<dyn Future<Item=_, Error=_> + Send>

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1026,14 +1028,40 @@ where
// Header parameters // Header parameters
let param_enum_header_string_array = headers.get(HeaderName::from_static("enum_header_string_array")); let param_enum_header_string_array = headers.get(HeaderName::from_static("enum_header_string_array"));
let param_enum_header_string_array = param_enum_header_string_array.map(|p| { let param_enum_header_string_array = match param_enum_header_string_array {
header::IntoHeaderValue::<Vec<String>>::from((*p).clone()).0 Some(v) => match header::IntoHeaderValue::<Vec<String>>::try_from((*v).clone()) {
}); Ok(result) =>
Some(result.0),
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Invalid header enum_header_string_array - {}", err)))
.expect("Unable to create Bad Request response for invalid header enum_header_string_array")));
},
},
None => {
None
}
};
let param_enum_header_string = headers.get(HeaderName::from_static("enum_header_string")); let param_enum_header_string = headers.get(HeaderName::from_static("enum_header_string"));
let param_enum_header_string = param_enum_header_string.map(|p| { let param_enum_header_string = match param_enum_header_string {
header::IntoHeaderValue::<String>::from((*p).clone()).0 Some(v) => match header::IntoHeaderValue::<String>::try_from((*v).clone()) {
}); Ok(result) =>
Some(result.0),
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Invalid header enum_header_string - {}", err)))
.expect("Unable to create Bad Request response for invalid header enum_header_string")));
},
},
None => {
None
}
};
// Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response) // Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response)
let query_params = form_urlencoded::parse(uri.query().unwrap_or_default().as_bytes()).collect::<Vec<_>>(); let query_params = form_urlencoded::parse(uri.query().unwrap_or_default().as_bytes()).collect::<Vec<_>>();
@ -1487,9 +1515,22 @@ where
// Header parameters // Header parameters
let param_api_key = headers.get(HeaderName::from_static("api_key")); let param_api_key = headers.get(HeaderName::from_static("api_key"));
let param_api_key = param_api_key.map(|p| { let param_api_key = match param_api_key {
header::IntoHeaderValue::<String>::from((*p).clone()).0 Some(v) => match header::IntoHeaderValue::<String>::try_from((*v).clone()) {
}); Ok(result) =>
Some(result.0),
Err(err) => {
return Box::new(future::ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Invalid header api_key - {}", err)))
.expect("Unable to create Bad Request response for invalid header api_key")));
},
},
None => {
None
}
};
Box::new({ Box::new({
{{ {{
@ -2890,14 +2931,34 @@ where
x_expires_after x_expires_after
} }
=> { => {
let x_rate_limit = match header::IntoHeaderValue(x_rate_limit).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling x_rate_limit header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
let x_expires_after = match header::IntoHeaderValue(x_expires_after).try_into() {
Ok(val) => val,
Err(e) => {
return future::ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("An internal server error occurred handling x_expires_after header - {}", e)))
.expect("Unable to create Internal Server Error for invalid response header"))
}
};
*response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode");
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("x-rate-limit"), HeaderName::from_static("x-rate-limit"),
header::IntoHeaderValue(x_rate_limit).into() x_rate_limit
); );
response.headers_mut().insert( response.headers_mut().insert(
HeaderName::from_static("x-expires-after"), HeaderName::from_static("x-expires-after"),
header::IntoHeaderValue(x_expires_after).into() x_expires_after
); );
response.headers_mut().insert( response.headers_mut().insert(
CONTENT_TYPE, CONTENT_TYPE,

View File

@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response};
use hyper_openssl::HttpsConnector; use hyper_openssl::HttpsConnector;
use serde_json; use serde_json;
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind}; use std::io::{Read, Error, ErrorKind};
use std::error; use std::error;
use std::fmt; use std::fmt;

View File

@ -1,5 +1,6 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
@ -19,19 +20,31 @@ impl<T> Deref for IntoHeaderValue<T> {
} }
} }
// Derive for each From<T> in hyper::header::HeaderValue // Derive for each TryFrom<T> in hyper::header::HeaderValue
macro_rules! ihv_generate { macro_rules! ihv_generate {
($t:ident) => { ($t:ident) => {
impl From<HeaderValue> for IntoHeaderValue<$t> { impl TryFrom<HeaderValue> for IntoHeaderValue<$t> {
fn from(hdr_value: HeaderValue) -> Self { type Error = String;
IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap())
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match hdr_value.parse::<$t>() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse {} as a string: {}",
stringify!($t), e)),
},
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<$t>> for HeaderValue { impl TryFrom<IntoHeaderValue<$t>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<$t>) -> Self { type Error = String;
hdr_value.0.into()
fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result<Self, Self::Error> {
Ok(hdr_value.0.into())
} }
} }
}; };
@ -48,52 +61,120 @@ ihv_generate!(i32);
// Custom derivations // Custom derivations
impl From<HeaderValue> for IntoHeaderValue<Vec<String>> { // Vec<String>
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue( impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
hdr_value type Error = String;
.to_str()
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(
hdr_value
.split(',') .split(',')
.filter_map(|x| match x.trim() { .filter_map(|x| match x.trim() {
"" => None, "" => None,
y => Some(y.to_string()), y => Some(y.to_string()),
}) })
.collect(), .collect())),
) Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<Vec<String>>> for HeaderValue { impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<Vec<String>>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap()
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<String> { // String
fn from(hdr_value: HeaderValue) -> Self {
IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
Err(e) => Err(format!("Unable to convert header {:?} to {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<String>> for HeaderValue { impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<String>) -> Self { type Error = String;
HeaderValue::from_str(&hdr_value.0).unwrap()
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e))
}
} }
} }
impl From<HeaderValue> for IntoHeaderValue<DateTime<Utc>> { // bool
fn from(hdr_value: HeaderValue) -> Self { impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
IntoHeaderValue( type Error = String;
DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap())
.unwrap() fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
.with_timezone(&Utc), match hdr_value.to_str() {
) Ok(hdr_value) => match hdr_value.parse() {
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
Err(e) => Err(format!("Unable to parse bool from {} - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
hdr_value, e)),
}
} }
} }
impl From<IntoHeaderValue<DateTime<Utc>>> for HeaderValue { impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
fn from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Self { type Error = String;
HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap()
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(&hdr_value.0.to_string()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
hdr_value, e))
}
}
}
// DateTime
impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
type Error = String;
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
Err(e) => Err(format!("Unable to parse: {} as date - {}",
hdr_value, e)),
},
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
hdr_value, e)),
}
}
}
impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
type Error = String;
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
hdr_value, e)),
}
} }
} }

View File

@ -8,16 +8,38 @@ use crate::header;
// Methods for converting between header::IntoHeaderValue<ANullableContainer> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ANullableContainer> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ANullableContainer>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ANullableContainer>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ANullableContainer>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ANullableContainer>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ANullableContainer - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ANullableContainer> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ANullableContainer> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ANullableContainer as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ANullableContainer as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ANullableContainer - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -169,16 +191,38 @@ impl ::std::str::FromStr for AdditionalPropertiesObject {
// Methods for converting between header::IntoHeaderValue<AllOfObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<AllOfObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<AllOfObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<AllOfObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<AllOfObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<AllOfObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for AllOfObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<AllOfObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<AllOfObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<AllOfObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <AllOfObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into AllOfObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -278,16 +322,38 @@ impl std::str::FromStr for AllOfObject {
// Methods for converting between header::IntoHeaderValue<BaseAllOf> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<BaseAllOf> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<BaseAllOf>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<BaseAllOf>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<BaseAllOf>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<BaseAllOf>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for BaseAllOf - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<BaseAllOf> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<BaseAllOf> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<BaseAllOf as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <BaseAllOf as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into BaseAllOf - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -374,16 +440,38 @@ impl std::str::FromStr for BaseAllOf {
// Methods for converting between header::IntoHeaderValue<GetYamlResponse> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<GetYamlResponse> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<GetYamlResponse>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<GetYamlResponse>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<GetYamlResponse>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<GetYamlResponse>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for GetYamlResponse - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<GetYamlResponse> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<GetYamlResponse> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<GetYamlResponse as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <GetYamlResponse as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into GetYamlResponse - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -470,16 +558,38 @@ impl std::str::FromStr for GetYamlResponse {
// Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<InlineObject> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<InlineObject>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<InlineObject>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<InlineObject>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for InlineObject - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<InlineObject> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<InlineObject as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <InlineObject as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into InlineObject - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -577,16 +687,38 @@ impl std::str::FromStr for InlineObject {
// Methods for converting between header::IntoHeaderValue<ObjectOfObjects> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectOfObjects> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectOfObjects>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectOfObjects>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectOfObjects>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectOfObjects>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectOfObjects - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectOfObjects> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectOfObjects> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectOfObjects as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectOfObjects as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectOfObjects - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }
@ -668,16 +800,38 @@ impl std::str::FromStr for ObjectOfObjects {
// Methods for converting between header::IntoHeaderValue<ObjectOfObjectsInner> and hyper::header::HeaderValue // Methods for converting between header::IntoHeaderValue<ObjectOfObjectsInner> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<header::IntoHeaderValue<ObjectOfObjectsInner>> for hyper::header::HeaderValue { impl std::convert::TryFrom<header::IntoHeaderValue<ObjectOfObjectsInner>> for hyper::header::HeaderValue {
fn from(hdr_value: header::IntoHeaderValue<ObjectOfObjectsInner>) -> Self { type Error = String;
hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap()
fn try_from(hdr_value: header::IntoHeaderValue<ObjectOfObjectsInner>) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match hyper::header::HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(
format!("Invalid header value for ObjectOfObjectsInner - value: {} is invalid {}",
hdr_value, e))
}
} }
} }
#[cfg(any(feature = "client", feature = "server"))] #[cfg(any(feature = "client", feature = "server"))]
impl From<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectOfObjectsInner> { impl std::convert::TryFrom<hyper::header::HeaderValue> for header::IntoHeaderValue<ObjectOfObjectsInner> {
fn from(hdr_value: hyper::header::HeaderValue) -> Self { type Error = String;
header::IntoHeaderValue(<ObjectOfObjectsInner as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap())
fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <ObjectOfObjectsInner as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
std::result::Result::Err(err) => std::result::Result::Err(
format!("Unable to convert header value '{}' into ObjectOfObjectsInner - {}",
value, err))
}
},
std::result::Result::Err(e) => std::result::Result::Err(
format!("Unable to convert header: {:?} to string: {}",
hdr_value, e))
}
} }
} }

View File

@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use log::warn; use log::warn;
use serde_json; use serde_json;
#[allow(unused_imports)]
use std::convert::{TryFrom, TryInto};
use std::io; use std::io;
use url::form_urlencoded; use url::form_urlencoded;
#[allow(unused_imports)] #[allow(unused_imports)]