diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache index d9878a10465..1cfedfd0884 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache index f1a4659f2e0..cc65e176ac5 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache @@ -332,15 +332,23 @@ // Header parameters {{/-first}} {{^isMapContainer}} -{{#required}} +{{^required}} + match param_{{{paramName}}} { + Some(param_{{{paramName}}}) => { +{{/required}} request.headers_mut().append( HeaderName::from_static("{{{nameInLowerCase}}}"), - header::IntoHeaderValue(param_{{{paramName}}}.clone()).into()); -{{/required}} + match header::IntoHeaderValue(param_{{{paramName}}}.clone()).try_into() { + Ok(header) => header, + Err(e) => { + return Box::new(future::err(ApiError(format!( + "Invalid header {{{paramName}}} - {}", e)))) as Box + Send>; + }, + }); {{^required}} - param_{{{paramName}}}.map(|value| request.headers_mut().append( - HeaderName::from_static("{{{nameInLowerCase}}}"), - header::IntoHeaderValue(value.clone()).into())); + }, + None => {} + } {{/required}} {{/isMapContainer}} {{#isMapContainer}} @@ -359,6 +367,14 @@ 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 + Send>, }; + let response_{{{name}}} = match TryInto::>::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 + Send>; + }, + }; + let response_{{{name}}} = response_{{{name}}}.0; + {{/headers}} let body = response.into_body(); Box::new( @@ -402,7 +418,7 @@ { body: body, {{/-first}} - {{{name}}}: (*Into::>::into(response_{{{name}}})).clone(), + {{{name}}}: response_{{name}}, {{#-last}} } {{/-last}} @@ -416,7 +432,7 @@ {{#-first}} { {{/-first}} - {{{name}}}: (*Into::>::into(response_{{{name}}})).clone(), + {{{name}}}: response_{{name}}, {{#-last}} } {{/-last}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/header.mustache b/modules/openapi-generator/src/main/resources/rust-server/header.mustache index 92eee5d0c25..5bc6ebe929b 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/header.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/header.mustache @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/modules/openapi-generator/src/main/resources/rust-server/models.mustache b/modules/openapi-generator/src/main/resources/rust-server/models.mustache index e76ae269c4e..58618bef5ff 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/models.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/models.mustache @@ -124,16 +124,38 @@ impl ::std::str::FromStr for {{{classname}}} { // Methods for converting between header::IntoHeaderValue<{{{classname}}}> and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue<{{{classname}}}>) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue<{{{classname}}}>) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue<{{{classname}}}> { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(<{{{classname}}} as std::str::FromStr>::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue<{{{classname}}}> { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + 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)) + } } } diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-imports.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-imports.mustache index ade68d62fff..2edcdd9b98e 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-imports.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-imports.mustache @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache index 9b81b7c3771..cbb011fe9a3 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache @@ -87,20 +87,35 @@ {{/-first}} let param_{{{paramName}}} = headers.get(HeaderName::from_static("{{{nameInLowerCase}}}")); -{{#required}} let param_{{{paramName}}} = match param_{{{paramName}}} { - Some(v) => header::IntoHeaderValue::<{{{dataType}}}>::from((*v).clone()).0, - None => return Box::new(future::ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Missing or invalid required header {{{baseName}}}")) - .expect("Unable to create Bad Request response for missing required header {{{baseName}}}"))), - }; + Some(v) => match header::IntoHeaderValue::<{{{dataType}}}>::try_from((*v).clone()) { + Ok(result) => +{{#required}} + result.0, {{/required}} {{^required}} - let param_{{{paramName}}} = param_{{{paramName}}}.map(|p| { - header::IntoHeaderValue::<{{{dataType}}}>::from((*p).clone()).0 - }); + Some(result.0), {{/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}} @@ -486,11 +501,23 @@ {{/headers}} {{/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"); {{#headers}} response.headers_mut().insert( HeaderName::from_static("{{{nameInLowerCase}}}"), - header::IntoHeaderValue({{name}}).into() + {{name}} ); {{/headers}} {{#produces}} diff --git a/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml b/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml index 455e60c8be5..4f224edf40c 100644 --- a/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml @@ -227,6 +227,9 @@ paths: Success-Info: schema: type: String + Bool-Header: + schema: + type: bool Object-Header: schema: $ref: "#/components/schemas/ObjectHeader" diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs index 78d4402ad8c..ed3ab5d92b7 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/header.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/header.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/models.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/models.rs index e4aba1221cf..82c937bad8d 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/models.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/models.rs @@ -8,16 +8,38 @@ use crate::header; // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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)) + } } } diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs index 5eca081cadb..eb870f1833e 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs index fb53ab023a1..4e55ac6c63d 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/header.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/header.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/models.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/models.rs index 1f6e445cfee..7caa4ed9c1e 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/models.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/models.rs @@ -8,16 +8,38 @@ use crate::header; // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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)) + } } } diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs index 36b8b332988..3c1062d0f9b 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] diff --git a/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml b/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml index f147a13b20e..894bb1537ab 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml +++ b/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml @@ -214,6 +214,11 @@ paths: schema: type: String style: simple + Bool-Header: + explode: false + schema: + type: bool + style: simple Object-Header: explode: false schema: diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs index 4e601555e08..7ed0cb95aac 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] @@ -151,9 +153,22 @@ where // Header parameters let param_information = headers.get(HeaderName::from_static("information")); - let param_information = param_information.map(|p| { - header::IntoHeaderValue::::from((*p).clone()).0 - }); + let param_information = match param_information { + Some(v) => match header::IntoHeaderValue::::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({ {{ diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs index cd7c768a381..063ec490254 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; @@ -537,7 +538,13 @@ impl Api for Client where // Header parameters request.headers_mut().append( 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 + Send>; + }, + }); Box::new(self.client_service.request(request) .map_err(|e| ApiError(format!("No response received: {}", e))) @@ -1396,10 +1403,38 @@ impl Api for Client where 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 + Send>, }; + let response_success_info = match TryInto::>::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 + 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 + Send>, + }; + let response_bool_header = match TryInto::>::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 + Send>; + }, + }; + let response_bool_header = response_bool_header.0; + let response_object_header = match response.headers().get(HeaderName::from_static("object-header")) { 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 + Send>, }; + let response_object_header = match TryInto::>::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 + Send>; + }, + }; + let response_object_header = response_object_header.0; + let body = response.into_body(); Box::new( body @@ -1417,8 +1452,9 @@ impl Api for Client where ResponsesWithHeadersGetResponse::Success { body: body, - success_info: (*Into::>::into(response_success_info)).clone(), - object_header: (*Into::>::into(response_object_header)).clone(), + success_info: response_success_info, + bool_header: response_bool_header, + object_header: response_object_header, } }) ) as Box + Send> @@ -1428,17 +1464,33 @@ impl Api for Client where 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 + Send>, }; + let response_further_info = match TryInto::>::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 + Send>; + }, + }; + let response_further_info = response_further_info.0; + let response_failure_info = match response.headers().get(HeaderName::from_static("failure-info")) { 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 + Send>, }; + let response_failure_info = match TryInto::>::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 + Send>; + }, + }; + let response_failure_info = response_failure_info.0; + let body = response.into_body(); Box::new( future::ok( ResponsesWithHeadersGetResponse::PreconditionFailed { - further_info: (*Into::>::into(response_further_info)).clone(), - failure_info: (*Into::>::into(response_failure_info)).clone(), + further_info: response_further_info, + failure_info: response_failure_info, } ) ) as Box + Send> diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/header.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/header.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/lib.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/lib.rs index 7379ce171f3..1f786e6fbfe 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/lib.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/lib.rs @@ -119,6 +119,7 @@ pub enum ResponsesWithHeadersGetResponse { { body: String, success_info: String, + bool_header: bool, object_header: models::ObjectHeader } , diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs index 57b9fc04c45..9e0c52b5259 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs @@ -8,16 +8,38 @@ use crate::header; // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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)) + } } } diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/server/callbacks.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/server/callbacks.rs index f93d34726a7..1d52c326e27 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/server/callbacks.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/server/callbacks.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; @@ -211,9 +212,20 @@ impl CallbackApi for Client where }); // Header parameters - param_information.map(|value| request.headers_mut().append( + match param_information { + Some(param_information) => { + request.headers_mut().append( 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 + Send>; + }, + }); + }, + None => {} + } Box::new(self.client_service.request(request) .map_err(|e| ApiError(format!("No response received: {}", e))) diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs index ce43e975248..c13904fe064 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] @@ -365,11 +367,23 @@ where let param_x_header = headers.get(HeaderName::from_static("x-header")); let param_x_header = match param_x_header { - Some(v) => header::IntoHeaderValue::::from((*v).clone()).0, - None => return Box::new(future::ok(Response::builder() + Some(v) => match header::IntoHeaderValue::::try_from((*v).clone()) { + Ok(result) => + result.0, + Err(err) => { + return Box::new(future::ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from("Missing or invalid required header X-Header")) - .expect("Unable to create Bad Request response for missing required header X-Header"))), + .body(Body::from(format!("Invalid header X-Header - {}", err))) + .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({ @@ -920,17 +934,52 @@ where { body, success_info, + bool_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.headers_mut().insert( 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( HeaderName::from_static("object-header"), - header::IntoHeaderValue(object_header).into() + object_header ); response.headers_mut().insert( CONTENT_TYPE, @@ -945,14 +994,34 @@ where 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.headers_mut().insert( HeaderName::from_static("further-info"), - header::IntoHeaderValue(further_info).into() + further_info ); response.headers_mut().insert( HeaderName::from_static("failure-info"), - header::IntoHeaderValue(failure_info).into() + failure_info ); }, }, diff --git a/samples/server/petstore/rust-server/output/ops-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/ops-v3/src/client/mod.rs index f977f551cd5..05ee715c58f 100644 --- a/samples/server/petstore/rust-server/output/ops-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/ops-v3/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; diff --git a/samples/server/petstore/rust-server/output/ops-v3/src/header.rs b/samples/server/petstore/rust-server/output/ops-v3/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/ops-v3/src/header.rs +++ b/samples/server/petstore/rust-server/output/ops-v3/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs index bffd26a1247..39fc3172378 100644 --- a/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs index 695d2ffcc25..a6fd7282c34 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; @@ -1358,13 +1359,35 @@ impl Api for Client where }); // 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"), - 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 + 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"), - 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 + Send>; + }, + }); + }, + None => {} + } Box::new(self.client_service.request(request) .map_err(|e| ApiError(format!("No response received: {}", e))) @@ -1836,9 +1859,20 @@ impl Api for Client where } // 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"), - 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 + Send>; + }, + }); + }, + None => {} + } Box::new(self.client_service.request(request) .map_err(|e| ApiError(format!("No response received: {}", e))) @@ -3431,10 +3465,26 @@ impl Api for Client where 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 + Send>, }; + let response_x_rate_limit = match TryInto::>::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 + 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")) { 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 + Send>, }; + let response_x_expires_after = match TryInto::>>::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 + Send>; + }, + }; + let response_x_expires_after = response_x_expires_after.0; + let body = response.into_body(); Box::new( body @@ -3454,8 +3504,8 @@ impl Api for Client where LoginUserResponse::SuccessfulOperation { body: body, - x_rate_limit: (*Into::>::into(response_x_rate_limit)).clone(), - x_expires_after: (*Into::>>::into(response_x_expires_after)).clone(), + x_rate_limit: response_x_rate_limit, + x_expires_after: response_x_expires_after, } }) ) as Box + Send> diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/header.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/header.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/models.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/models.rs index 367187cc5fc..12dd045e216 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/models.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/models.rs @@ -8,16 +8,38 @@ use crate::header; // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 AdditionalPropertiesClass - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 AdditionalPropertiesClass - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -118,16 +140,38 @@ impl AdditionalPropertiesClass { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Animal - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Animal - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -232,16 +276,38 @@ impl Animal { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 AnimalFarm - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 AnimalFarm - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -347,16 +413,38 @@ impl AnimalFarm { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ApiResponse - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ApiResponse - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -478,16 +566,38 @@ impl ApiResponse { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ArrayOfArrayOfNumberOnly - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ArrayOfArrayOfNumberOnly - {}", + 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 ArrayOfArrayOfNumberOnly { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ArrayOfNumberOnly - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ArrayOfNumberOnly - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -680,16 +812,38 @@ impl ArrayOfNumberOnly { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ArrayTest - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ArrayTest - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -818,16 +972,38 @@ impl ArrayTest { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Capitalization - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Capitalization - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -992,16 +1168,38 @@ impl Capitalization { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Cat - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Cat - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1120,16 +1318,38 @@ impl Cat { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 CatAllOf - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 CatAllOf - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1223,16 +1443,38 @@ impl CatAllOf { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Category - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Category - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1342,16 +1584,38 @@ impl Category { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ClassModel - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ClassModel - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1445,16 +1709,38 @@ impl ClassModel { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Client - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Client - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1548,16 +1834,38 @@ impl Client { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Dog - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Dog - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1676,16 +1984,38 @@ impl Dog { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 DogAllOf - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 DogAllOf - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1779,16 +2109,38 @@ impl DogAllOf { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 EnumArrays - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 EnumArrays - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -1957,16 +2309,38 @@ impl EnumClass { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 EnumTest - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 EnumTest - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2113,16 +2487,38 @@ impl EnumTest { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 FormatTest - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 FormatTest - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2358,16 +2754,38 @@ impl FormatTest { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 HasOnlyReadOnly - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 HasOnlyReadOnly - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2475,16 +2893,38 @@ impl HasOnlyReadOnly { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 List - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 List - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2578,16 +3018,38 @@ impl List { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 MapTest - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 MapTest - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2701,16 +3163,38 @@ impl MapTest { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 MixedPropertiesAndAdditionalPropertiesClass - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 MixedPropertiesAndAdditionalPropertiesClass - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2822,16 +3306,38 @@ impl MixedPropertiesAndAdditionalPropertiesClass { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Model200Response - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Model200Response - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -2941,16 +3447,38 @@ impl Model200Response { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ModelReturn - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ModelReturn - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3046,16 +3574,38 @@ impl ModelReturn { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Name - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Name - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3189,16 +3739,38 @@ impl Name { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 NumberOnly - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 NumberOnly - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3292,16 +3864,38 @@ impl NumberOnly { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ObjectContainingObjectWithOnlyAdditionalProperties - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ObjectContainingObjectWithOnlyAdditionalProperties - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3451,16 +4045,38 @@ impl ObjectWithOnlyAdditionalProperties { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Order - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Order - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3663,16 +4279,38 @@ impl OuterBoolean { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 OuterComposite - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 OuterComposite - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -3934,16 +4572,38 @@ impl OuterString { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Pet - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Pet - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -4096,16 +4756,38 @@ impl Pet { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 ReadOnlyFirst - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 ReadOnlyFirst - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -4213,16 +4895,38 @@ impl ReadOnlyFirst { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 SpecialModelName - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 SpecialModelName - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -4317,16 +5021,38 @@ impl SpecialModelName { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 Tag - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 Tag - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } @@ -4435,16 +5161,38 @@ impl Tag { // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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 User - value: {} is invalid {}", + hdr_value, e)) + } } } #[cfg(any(feature = "client", feature = "server"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 User - {}", + value, err)) + } + }, + std::result::Result::Err(e) => std::result::Result::Err( + format!("Unable to convert header: {:?} to string: {}", + hdr_value, e)) + } } } diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs index 55db75689e8..723ac064003 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)] @@ -1026,14 +1028,40 @@ where // Header parameters 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| { - header::IntoHeaderValue::>::from((*p).clone()).0 - }); + let param_enum_header_string_array = match param_enum_header_string_array { + Some(v) => match header::IntoHeaderValue::>::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 = param_enum_header_string.map(|p| { - header::IntoHeaderValue::::from((*p).clone()).0 - }); + let param_enum_header_string = match param_enum_header_string { + Some(v) => match header::IntoHeaderValue::::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) let query_params = form_urlencoded::parse(uri.query().unwrap_or_default().as_bytes()).collect::>(); @@ -1487,9 +1515,22 @@ where // Header parameters let param_api_key = headers.get(HeaderName::from_static("api_key")); - let param_api_key = param_api_key.map(|p| { - header::IntoHeaderValue::::from((*p).clone()).0 - }); + let param_api_key = match param_api_key { + Some(v) => match header::IntoHeaderValue::::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({ {{ @@ -2890,14 +2931,34 @@ where 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.headers_mut().insert( HeaderName::from_static("x-rate-limit"), - header::IntoHeaderValue(x_rate_limit).into() + x_rate_limit ); response.headers_mut().insert( HeaderName::from_static("x-expires-after"), - header::IntoHeaderValue(x_expires_after).into() + x_expires_after ); response.headers_mut().insert( CONTENT_TYPE, diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs index 6deeda61b03..7ce4e8ad0ac 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs @@ -8,6 +8,7 @@ use hyper::{Body, Uri, Response}; use hyper_openssl::HttpsConnector; use serde_json; use std::borrow::Cow; +use std::convert::TryInto; use std::io::{Read, Error, ErrorKind}; use std::error; use std::fmt; diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/header.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/header.rs index 92eee5d0c25..5bc6ebe929b 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/header.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/header.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use hyper::header::HeaderValue; +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; @@ -19,19 +20,31 @@ impl Deref for IntoHeaderValue { } } -// Derive for each From in hyper::header::HeaderValue +// Derive for each TryFrom in hyper::header::HeaderValue macro_rules! ihv_generate { ($t:ident) => { - impl From for IntoHeaderValue<$t> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().parse::<$t>().unwrap()) + impl TryFrom for IntoHeaderValue<$t> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue<$t>) -> Self { - hdr_value.0.into() + impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue<$t>) -> Result { + Ok(hdr_value.0.into()) } } }; @@ -48,52 +61,120 @@ ihv_generate!(i32); // Custom derivations -impl From for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - hdr_value - .to_str() - .unwrap() +// Vec + +impl TryFrom for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + match hdr_value.to_str() { + Ok(hdr_value) => Ok(IntoHeaderValue( + hdr_value .split(',') .filter_map(|x| match x.trim() { "" => None, y => Some(y.to_string()), }) - .collect(), - ) + .collect())), + Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}", + hdr_value, e)), + } } } -impl From>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(&hdr_value.0.join(", ")).unwrap() +impl TryFrom>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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 for IntoHeaderValue { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue(hdr_value.to_str().unwrap().to_string()) +// String + +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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> for HeaderValue { - fn from(hdr_value: IntoHeaderValue) -> Self { - HeaderValue::from_str(&hdr_value.0).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { - fn from(hdr_value: HeaderValue) -> Self { - IntoHeaderValue( - DateTime::parse_from_rfc3339(hdr_value.to_str().unwrap()) - .unwrap() - .with_timezone(&Utc), - ) +// bool +impl TryFrom for IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { - fn from(hdr_value: IntoHeaderValue>) -> Self { - HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()).unwrap() +impl TryFrom> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue) -> Result { + 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 for IntoHeaderValue> { + type Error = String; + + fn try_from(hdr_value: HeaderValue) -> Result { + 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>> for HeaderValue { + type Error = String; + + fn try_from(hdr_value: IntoHeaderValue>) -> Result { + 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)), + } } } diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/models.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/models.rs index 58f1a505352..b8085bcd85a 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/models.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/models.rs @@ -8,16 +8,38 @@ use crate::header; // Methods for converting between header::IntoHeaderValue and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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 and hyper::header::HeaderValue #[cfg(any(feature = "client", feature = "server"))] -impl From> for hyper::header::HeaderValue { - fn from(hdr_value: header::IntoHeaderValue) -> Self { - hyper::header::HeaderValue::from_str(&hdr_value.to_string()).unwrap() +impl std::convert::TryFrom> for hyper::header::HeaderValue { + type Error = String; + + fn try_from(hdr_value: header::IntoHeaderValue) -> std::result::Result { + 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"))] -impl From for header::IntoHeaderValue { - fn from(hdr_value: hyper::header::HeaderValue) -> Self { - header::IntoHeaderValue(::from_str(hdr_value.to_str().unwrap()).unwrap()) +impl std::convert::TryFrom for header::IntoHeaderValue { + type Error = String; + + fn try_from(hdr_value: hyper::header::HeaderValue) -> std::result::Result { + match hdr_value.to_str() { + std::result::Result::Ok(value) => { + match ::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)) + } } } diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs index 9dc256aaeca..69fd7709b91 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs @@ -5,6 +5,8 @@ use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap}; use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use log::warn; use serde_json; +#[allow(unused_imports)] +use std::convert::{TryFrom, TryInto}; use std::io; use url::form_urlencoded; #[allow(unused_imports)]