[Rust Server] Rust 2018 Edition (#5942)

* [Rust Server] Rust 2018 Edition
* [Rust Server] Fix unused warning
* Update samples
This commit is contained in:
Richard Whitehouse
2020-04-26 12:00:46 +01:00
committed by GitHub
parent d911fd73bd
commit f36a319316
72 changed files with 512 additions and 1111 deletions

View File

@@ -6,6 +6,7 @@ authors = [{{#infoEmail}}"{{{infoEmail}}}"{{/infoEmail}}]
description = "{{{appDescription}}}"
{{/appDescription}}
license = "Unlicense"
edition = "2018"
[features]
default = ["client", "server"]
@@ -25,6 +26,7 @@ client = [
{{#hasCallbacks}}
"serde_ignored", "regex", "percent-encoding", "lazy_static",
{{/hasCallbacks}}
{{! Anything added to the list below, should probably be added to the callbacks list below }}
"hyper", "hyper-openssl", "native-tls", "openssl", "url"
]
server = [
@@ -57,11 +59,10 @@ openssl = {version = "0.10", optional = true }
chrono = { version = "0.4", features = ["serde"] }
futures = "0.1"
swagger = "4.0"
log = "0.3.0"
log = "0.4.0"
mime = "0.3"
serde = "1.0"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"]}
serde_json = "1.0"
# Crates included if required by the API definition

View File

@@ -1,5 +1,5 @@
{{>server-imports}}
use CallbackApi as Api;
use crate::CallbackApi as Api;
{{#apiInfo}}
{{#apis}}
{{#operations}}
@@ -7,7 +7,7 @@ use CallbackApi as Api;
{{#callbacks}}
{{#urls}}
{{#requests}}
use {{{operationId}}}Response;
use crate::{{{operationId}}}Response;
{{/requests}}
{{/urls}}
{{/callbacks}}

View File

@@ -27,7 +27,6 @@ use multipart::client::lazy::Multipart;
{{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}}
use hyper_0_10::header::{Headers, ContentType};
header! { (ContentId, "Content-ID") => [String] }
use mime_multipart::{Node, Part, generate_boundary, write_multipart};
{{/apiUsesMultipartRelated}}
{{#apiUsesUuid}}
@@ -37,10 +36,10 @@ use uuid;
use serde_xml_rs;
{{/usesXml}}
use models;
use header;
use crate::models;
use crate::header;
define_encode_set! {
url::define_encode_set! {
/// This encode set is used for object IDs
///
/// Aside from the special characters defined in the `PATH_SEGMENT_ENCODE_SET`,

View File

@@ -1,5 +1,5 @@
{{>client-imports}}
use {Api{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}},
use crate::{Api{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}},
{{{operationId}}}Response{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
};
@@ -38,7 +38,7 @@ pub struct Client<F>
impl<F> fmt::Debug for Client<F>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Client {{ base_path: {} }}", self.base_path)
}
}
@@ -205,7 +205,7 @@ impl From<hyper::http::uri::InvalidUri> for ClientInitError {
}
impl fmt::Display for ClientInitError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: &dyn fmt::Debug = self;
s.fmt(f)
}

View File

@@ -179,7 +179,7 @@
headers: {
let mut h = Headers::new();
h.set(ContentType("{{{contentType}}}".parse().unwrap()));
h.set(ContentId("{{{baseName}}}".parse().unwrap()));
h.set_raw("Content-ID", vec![b"{{{baseName}}}".to_vec()]);
h
},
{{#isBinary}}

View File

@@ -9,7 +9,7 @@ use std::marker::PhantomData;
use swagger::auth::{AuthData, Authorization, Bearer, Scopes};
use swagger::context::ContextualPayload;
use swagger::{EmptyContext, Has, Pop, Push, XSpanIdString};
use Api;
use crate::Api;
pub struct MakeAddContext<T, A> {
inner: T,

View File

@@ -1,30 +1,6 @@
#![allow(missing_docs, unused_variables, trivial_casts)]
extern crate {{{externCrateName}}};
extern crate clap;
extern crate env_logger;
extern crate futures;
// log may be unused if there are no examples
#[allow(unused_imports)]
#[macro_use]
extern crate log;
#[macro_use]
extern crate swagger;
extern crate tokio;
{{#hasCallbacks}}
extern crate chrono;
#[macro_use]
extern crate error_chain;
extern crate hyper;
{{#apiUsesUuid}}
extern crate uuid;
{{/apiUsesUuid}}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
extern crate openssl;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
extern crate tokio_openssl;
mod server;
{{/hasCallbacks}}
@@ -37,6 +13,9 @@ use {{{externCrateName}}}::{Api, ApiNoContext, Client, ContextWrapperExt, models
};
use clap::{App, Arg};
#[allow(unused_imports)]
use log::info;
// swagger::Has may be unused if there are no examples
#[allow(unused_imports)]
use swagger::{ContextBuilder, EmptyContext, XSpanIdString, Has, Push, AuthData};
@@ -98,8 +77,8 @@ fn main() {
.expect("Failed to create HTTP client")
};
let context: make_context_ty!(ContextBuilder, EmptyContext, Option<AuthData>, XSpanIdString) =
make_context!(ContextBuilder, EmptyContext, None as Option<AuthData>, XSpanIdString::default());
let context: swagger::make_context_ty!(ContextBuilder, EmptyContext, Option<AuthData>, XSpanIdString) =
swagger::make_context!(ContextBuilder, EmptyContext, None as Option<AuthData>, XSpanIdString::default());
let client = client.with_context(context);

View File

@@ -3,7 +3,7 @@
#![allow(unused_imports)]
mod errors {
error_chain!{}
error_chain::error_chain!{}
}
pub use self::errors::*;
@@ -12,6 +12,7 @@ use chrono;
use futures::{future, Future, Stream};
use hyper::server::conn::Http;
use hyper::service::MakeService as _;
use log::info;
use openssl::ssl::SslAcceptorBuilder;
use std::marker::PhantomData;
use std::net::SocketAddr;

View File

@@ -2,33 +2,6 @@
#![allow(missing_docs)]
// Imports required by this file.
// extern crate <name of this crate>;
extern crate {{{externCrateName}}};
extern crate clap;
extern crate env_logger;
extern crate hyper;
#[macro_use]
extern crate log;
extern crate swagger;
// Imports required by server library.
// extern crate {{{externCrateName}}};
extern crate chrono;
#[macro_use]
extern crate error_chain;
extern crate futures;
// extern crate swagger;
extern crate tokio;
{{#apiUsesUuid}}
extern crate uuid;
{{/apiUsesUuid}}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
extern crate openssl;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
extern crate tokio_openssl;
use clap::{App, Arg};
mod server;

View File

@@ -1,86 +1,5 @@
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]
// Crates with macros
#[macro_use]
extern crate serde_derive;
{{#hasCallbacks}}
#[cfg(any(feature = "client", feature = "server"))]
{{/hasCallbacks}}
{{^hasCallbacks}}
#[cfg(feature = "server")]
{{/hasCallbacks}}
#[macro_use]
extern crate lazy_static;
#[cfg(any(feature = "client", feature = "server"))]
#[macro_use]
extern crate url;
#[macro_use]
extern crate log;
{{#apiUsesMultipartRelated}}
#[cfg(any(feature = "client", feature = "server"))]
#[macro_use]
extern crate hyper_0_10;
{{/apiUsesMultipartRelated}}
// Crates for conversion support
#[cfg(feature = "conversion")]
#[macro_use]
extern crate frunk_derives;
#[cfg(feature = "conversion")]
#[macro_use]
extern crate frunk_enum_derive;
#[cfg(feature = "conversion")]
extern crate frunk_core;
extern crate mime;
extern crate serde;
extern crate futures;
extern crate chrono;
extern crate swagger;
extern crate serde_json;
#[cfg(any(feature = "client", feature = "server"))]
extern crate hyper;
{{#hasCallbacks}}
#[cfg(any(feature = "client", feature = "server"))]
{{/hasCallbacks}}
{{^hasCallbacks}}
#[cfg(feature = "server")]
{{/hasCallbacks}}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
extern crate hyper_openssl;
{{#apiUsesMultipart}}
#[cfg(any(feature = "client", feature = "server"))]
extern crate mime_0_2;
{{/apiUsesMultipart}}
{{#apiUsesMultipartRelated}}
#[cfg(any(feature = "client", feature = "server"))]
extern crate mime_multipart;
{{/apiUsesMultipartRelated}}
{{#hasCallbacks}}
#[cfg(any(feature = "client", feature = "server"))]
{{/hasCallbacks}}
{{^hasCallbacks}}
#[cfg(feature = "server")]
{{/hasCallbacks}}
extern crate percent_encoding;
#[cfg(any(feature = "client", feature = "server"))]
extern crate serde_ignored;
#[cfg(any(feature = "client", feature = "server"))]
{{#apiUsesUuid}}extern crate uuid;{{/apiUsesUuid}}
{{#usesXml}}extern crate serde_xml_rs;{{/usesXml}}
{{#apiUsesMultipartFormData}}
#[cfg(any(feature = "client", feature = "server"))]
extern crate multipart;
{{/apiUsesMultipartFormData}}
#[cfg(any(feature = "client", feature = "server"))]
{{#usesUrlEncodedForm}}extern crate serde_urlencoded;{{/usesUrlEncodedForm}}
use futures::Stream;
use std::io::Error;

View File

@@ -1,8 +1,8 @@
#![allow(unused_qualifications)]
use models;
use crate::models;
#[cfg(any(feature = "client", feature = "server"))]
use header;
use crate::header;
{{! Don't "use" structs here - they can conflict with the names of models, and mean that the code won't compile }}
{{#models}}{{#model}}
@@ -12,8 +12,8 @@ use header;
/// which helps with FFI.
#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGenericEnum))]{{#xmlName}}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk_enum_derive::LabelledGenericEnum))]{{#xmlName}}
#[serde(rename = "{{{xmlName}}}")]{{/xmlName}}
pub enum {{{classname}}} { {{#allowableValues}}{{#enumVars}}
#[serde(rename = {{{value}}})]
@@ -21,7 +21,7 @@ pub enum {{{classname}}} { {{#allowableValues}}{{#enumVars}}
}
impl std::fmt::Display for {{{classname}}} {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self { {{#allowableValues}}{{#enumVars}}
{{{classname}}}::{{{name}}} => write!(f, "{}", {{{value}}}),{{/enumVars}}{{/allowableValues}}
}
@@ -46,12 +46,12 @@ impl std::str::FromStr for {{{classname}}} {
{{^isEnum}}
{{#dataType}}
{{#isMapModel}}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
{{/isMapModel}}
{{^isMapModel}}
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
{{/isMapModel}}
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
{{#xmlName}}
#[serde(rename = "{{{xmlName}}}")]
{{/xmlName}}
@@ -146,9 +146,19 @@ where
serde_xml_rs::wrap_primitives(item, serializer, "{{{itemXmlName}}}")
}
{{/itemXmlName}}{{/vendorExtensions}}{{! vec}}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
pub struct {{{classname}}}({{#vendorExtensions}}{{#itemXmlName}}#[serde(serialize_with = "wrap_in_{{{itemXmlName}}}")]{{/itemXmlName}}{{/vendorExtensions}}Vec<{{{arrayModelType}}}>);
{{/itemXmlName}}
{{/vendorExtensions}}
{{! vec}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct {{{classname}}}(
{{#vendorExtensions}}
{{#itemXmlName}}
#[serde(serialize_with = "wrap_in_{{{itemXmlName}}}")]
{{/itemXmlName}}
{{/vendorExtensions}}
Vec<{{{arrayModelType}}}>
);
impl std::convert::From<Vec<{{{arrayModelType}}}>> for {{{classname}}} {
fn from(x: Vec<{{{arrayModelType}}}>) -> Self {
@@ -234,20 +244,29 @@ impl std::str::FromStr for {{{classname}}} {
}
{{/arrayModelType}}{{^arrayModelType}}{{! general struct}}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(LabelledGeneric))]
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
{{#xmlName}}
#[serde(rename = "{{{xmlName}}}")]
{{/xmlName}}
pub struct {{{classname}}} {
{{#vars}}{{#description}} /// {{{description}}}
{{/description}}{{#isEnum}} // Note: inline enums are not fully supported by openapi-generator
{{/isEnum}} #[serde(rename = "{{{baseName}}}")]{{#vendorExtensions}}{{#itemXmlName}}
#[serde(serialize_with = "wrap_in_{{{itemXmlName}}}")]{{/itemXmlName}}{{/vendorExtensions}}{{#required}}
{{/isEnum}}
#[serde(rename = "{{{baseName}}}")]
{{#vendorExtensions}}
{{#itemXmlName}}
#[serde(serialize_with = "wrap_in_{{{itemXmlName}}}")]
{{/itemXmlName}}
{{/vendorExtensions}}
{{#required}}
pub {{{name}}}: {{#isNullable}}swagger::Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}},
{{/required}}{{^required}}{{#isNullable}}
{{/required}}
{{^required}}
{{#isNullable}}
#[serde(deserialize_with = "swagger::nullable_format::deserialize_optional_nullable")]
#[serde(default = "swagger::nullable_format::default_optional_nullable")]{{/isNullable}}
#[serde(default = "swagger::nullable_format::default_optional_nullable")]
{{/isNullable}}
#[serde(skip_serializing_if="Option::is_none")]
pub {{{name}}}: Option<{{#isNullable}}swagger::Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}>,
{{/required}}

View File

@@ -1,5 +1,5 @@
{{>client-imports}}
use CallbackApi;
use crate::CallbackApi;
{{#apiInfo}}
{{#apis}}
{{#operations}}
@@ -7,7 +7,7 @@ use CallbackApi;
{{#callbacks}}
{{#urls}}
{{#requests}}
use {{{operationId}}}Response;
use crate::{{{operationId}}}Response;
{{/requests}}
{{/urls}}
{{/callbacks}}
@@ -25,7 +25,7 @@ pub struct Client<F>
impl<F> fmt::Debug for Client<F>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Client")
}
}

View File

@@ -3,9 +3,10 @@ use futures::{Future, future, Stream, stream};
use hyper;
use hyper::{Request, Response, Error, StatusCode, Body, HeaderMap};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use url::form_urlencoded;
use log::warn;
use serde_json;
use std::io;
use url::form_urlencoded;
#[allow(unused_imports)]
use swagger;
use swagger::{ApiError, XSpanIdString, Has, RequestParser};
@@ -14,7 +15,6 @@ use swagger::auth::Scopes;
use swagger::context::ContextualPayload;
{{#apiUsesMultipartRelated}}
use hyper_0_10::header::{Headers, ContentType};
header! { (ContentId, "Content-ID") => [String] }
use mime_0_2::{TopLevel, SubLevel, Mime as Mime2};
use mime_multipart::{read_multipart_body, Node, Part};
{{/apiUsesMultipartRelated}}
@@ -30,7 +30,7 @@ use serde_xml_rs;
{{/usesXml}}
#[allow(unused_imports)]
use models;
use header;
use crate::models;
use crate::header;
pub use crate::context;

View File

@@ -1,5 +1,5 @@
{{>server-imports}}
use {Api{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}},
use crate::{Api{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}},
{{{operationId}}}Response{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
};

View File

@@ -1,5 +1,5 @@
mod paths {
extern crate regex;
use lazy_static::lazy_static;
lazy_static! {
pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(vec![
@@ -10,7 +10,7 @@ mod paths {
.expect("Unable to create global regex set");
}
{{#pathSet}}
pub static ID_{{{PATH_ID}}}: usize = {{{index}}};
pub(crate) static ID_{{{PATH_ID}}}: usize = {{{index}}};
{{#hasPathParams}}
lazy_static! {
pub static ref REGEX_{{{PATH_ID}}}: regex::Regex =