Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
rust-server.models.mustache Maven / Gradle / Ivy
#![allow(unused_qualifications)]
use crate::models;
#[cfg(any(feature = "client", feature = "server"))]
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}}
{{#description}}
/// {{{description}}}
{{/description}}
{{#isEnum}}
/// Enumeration of values.
/// Since this enum's variants do not hold data, we can easily define them them as `#[repr(C)]`
/// which helps with FFI.
#[allow(non_camel_case_types)]
#[repr(C)]
#[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}}})]
{{{name}}},
{{/enumVars}}
{{/allowableValues}}
}
impl std::fmt::Display for {{{classname}}} {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
{{#allowableValues}}
{{#enumVars}}
{{{classname}}}::{{{name}}} => write!(f, "{}", {{{value}}}),
{{/enumVars}}
{{/allowableValues}}
}
}
}
impl std::str::FromStr for {{{classname}}} {
type Err = String;
fn from_str(s: &str) -> std::result::Result {
match s {
{{#allowableValues}}
{{#enumVars}}
{{{value}}} => std::result::Result::Ok({{{classname}}}::{{{name}}}),
{{/enumVars}}
{{/allowableValues}}
_ => std::result::Result::Err(format!("Value not valid: {}", s)),
}
}
}
{{/isEnum}}
{{^isEnum}}
{{#dataType}}
{{#isMap}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
{{/isMap}}
{{^isMap}}
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
{{/isMap}}
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
{{#xmlName}}
#[serde(rename = "{{{xmlName}}}")]
{{/xmlName}}
pub struct {{{classname}}}({{{dataType}}});
impl std::convert::From<{{{dataType}}}> for {{{classname}}} {
fn from(x: {{{dataType}}}) -> Self {
{{{classname}}}(x)
}
}
{{#vendorExtensions.x-is-string}}
impl std::string::ToString for {{{classname}}} {
fn to_string(&self) -> String {
self.0.to_string()
}
}
impl std::str::FromStr for {{{classname}}} {
type Err = std::string::ParseError;
fn from_str(x: &str) -> std::result::Result {
std::result::Result::Ok({{{classname}}}(x.to_string()))
}
}
{{/vendorExtensions.x-is-string}}
impl std::convert::From<{{{classname}}}> for {{{dataType}}} {
fn from(x: {{{classname}}}) -> Self {
x.0
}
}
impl std::ops::Deref for {{{classname}}} {
type Target = {{{dataType}}};
fn deref(&self) -> &{{{dataType}}} {
&self.0
}
}
impl std::ops::DerefMut for {{{classname}}} {
fn deref_mut(&mut self) -> &mut {{{dataType}}} {
&mut self.0
}
}
{{#additionalPropertiesType}}
/// Converts the {{{classname}}} value to the Query Parameters representation (style=form, explode=false)
/// specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde serializer
impl ::std::string::ToString for {{{classname}}} {
fn to_string(&self) -> String {
// Skipping additionalProperties in query parameter serialization
"".to_string()
}
}
/// Converts Query Parameters representation (style=form, explode=false) to a {{{classname}}} value
/// as specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde deserializer
impl ::std::str::FromStr for {{{classname}}} {
type Err = &'static str;
fn from_str(s: &str) -> std::result::Result {
std::result::Result::Err("Parsing additionalProperties for {{{classname}}} is not supported")
}
}
{{/additionalPropertiesType}}
{{/dataType}}
{{^dataType}}
{{#arrayModelType}}
{{#vendorExtensions}}{{#x-item-xml-name}}// Utility function for wrapping list elements when serializing xml
#[allow(non_snake_case)]
fn wrap_in_{{{x-item-xml-name}}}(item: &Vec<{{{arrayModelType}}}>, serializer: S) -> std::result::Result
where
S: serde::ser::Serializer,
{
serde_xml_rs::wrap_primitives(item, serializer, "{{{x-item-xml-name}}}")
}
{{/x-item-xml-name}}
{{/vendorExtensions}}
{{! vec}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct {{{classname}}}(
{{#vendorExtensions}}
{{#x-item-xml-name}}
#[serde(serialize_with = "wrap_in_{{{x-item-xml-name}}}")]
{{/x-item-xml-name}}
{{/vendorExtensions}}
Vec<{{{arrayModelType}}}>
);
impl std::convert::From> for {{{classname}}} {
fn from(x: Vec<{{{arrayModelType}}}>) -> Self {
{{{classname}}}(x)
}
}
impl std::convert::From<{{{classname}}}> for Vec<{{{arrayModelType}}}> {
fn from(x: {{{classname}}}) -> Self {
x.0
}
}
impl std::iter::FromIterator<{{{arrayModelType}}}> for {{{classname}}} {
fn from_iter>(u: U) -> Self {
{{{classname}}}(Vec::<{{{arrayModelType}}}>::from_iter(u))
}
}
impl std::iter::IntoIterator for {{{classname}}} {
type Item = {{{arrayModelType}}};
type IntoIter = std::vec::IntoIter<{{{arrayModelType}}}>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> std::iter::IntoIterator for &'a {{{classname}}} {
type Item = &'a {{{arrayModelType}}};
type IntoIter = std::slice::Iter<'a, {{{arrayModelType}}}>;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}
impl<'a> std::iter::IntoIterator for &'a mut {{{classname}}} {
type Item = &'a mut {{{arrayModelType}}};
type IntoIter = std::slice::IterMut<'a, {{{arrayModelType}}}>;
fn into_iter(self) -> Self::IntoIter {
(&mut self.0).into_iter()
}
}
impl std::ops::Deref for {{{classname}}} {
type Target = Vec<{{{arrayModelType}}}>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for {{{classname}}} {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
/// Converts the {{{classname}}} value to the Query Parameters representation (style=form, explode=false)
/// specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde serializer
impl std::string::ToString for {{{classname}}} {
fn to_string(&self) -> String {
self.iter().map(|x| x.to_string()).collect::>().join(",").to_string()
}
}
/// Converts Query Parameters representation (style=form, explode=false) to a {{{classname}}} value
/// as specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde deserializer
impl std::str::FromStr for {{{classname}}} {
type Err = <{{{arrayModelType}}} as std::str::FromStr>::Err;
fn from_str(s: &str) -> std::result::Result {
let mut items = vec![];
for item in s.split(',')
{
items.push(item.parse()?);
}
std::result::Result::Ok({{{classname}}}(items))
}
}
{{/arrayModelType}}
{{^arrayModelType}}
{{! general struct}}
#[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}}
{{#x-item-xml-name}}
#[serde(serialize_with = "wrap_in_{{{x-item-xml-name}}}")]
{{/x-item-xml-name}}
{{/vendorExtensions}}
{{#required}}
pub {{{name}}}: {{#isNullable}}swagger::Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}},
{{/required}}
{{^required}}
{{#isNullable}}
#[serde(deserialize_with = "swagger::nullable_format::deserialize_optional_nullable")]
#[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}}
{{/vars}}
}
impl {{{classname}}} {
pub fn new({{#vars}}{{^defaultValue}}{{{name}}}: {{#isNullable}}swagger::Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}, {{/defaultValue}}{{/vars}}) -> {{{classname}}} {
{{{classname}}} {
{{#vars}} {{{name}}}: {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{{name}}}{{/defaultValue}},
{{/vars}}
}
}
}
/// Converts the {{{classname}}} value to the Query Parameters representation (style=form, explode=false)
/// specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde serializer
impl std::string::ToString for {{{classname}}} {
fn to_string(&self) -> String {
let mut params: Vec = vec![];
{{#vars}}
{{#isByteArray}}
// Skipping {{baseName}} in query parameter serialization
{{/isByteArray}}
{{#isBinary}}
// Skipping {{baseName}} in query parameter serialization
{{/isBinary}}
{{#isMap}}
// Skipping {{baseName}} in query parameter serialization
{{/isMap}}
{{^isPrimitiveType}}
// Skipping {{baseName}} in query parameter serialization
{{/isPrimitiveType}}
{{^isByteArray}}{{^isBinary}}{{^isMap}}{{#isPrimitiveType}}
{{#required}}
params.push("{{{baseName}}}".to_string());
{{^isArray}}
{{#isNullable}}
params.push(self.{{{name}}}.as_ref().map_or("null".to_string(), |x| x.to_string()));
{{/isNullable}}
{{^isNullable}}
params.push(self.{{{name}}}.to_string());
{{/isNullable}}
{{/isArray}}
{{#isArray}}
{{#isNullable}}
params.push(self.{{{name}}}.as_ref().map_or(vec!["null".to_string()], |x| x.iter().map(|x| x.to_string()).collect::>().join(",").to_string()));
{{/isNullable}}
{{^isNullable}}
params.push(self.{{{name}}}.iter().map(|x| x.to_string()).collect::>().join(",").to_string());
{{/isNullable}}
{{/isArray}}
{{/required}}
{{^required}}
if let Some(ref {{{name}}}) = self.{{{name}}} {
params.push("{{{baseName}}}".to_string());
{{^isArray}}
{{#isNullable}}
params.push({{{name}}}.as_ref().map_or("null".to_string(), |x| x.to_string()));
{{/isNullable}}
{{^isNullable}}
params.push({{{name}}}.to_string());
{{/isNullable}}
{{/isArray}}
{{#isArray}}
{{#isNullable}}
params.push({{{name}}}.as_ref().map_or("null".to_string(), |x| x.iter().map(|x| x.to_string()).collect::>().join(",").to_string()));
{{/isNullable}}
{{^isNullable}}
params.push({{{name}}}.iter().map(|x| x.to_string()).collect::>().join(",").to_string());
{{/isNullable}}
{{/isArray}}
}
{{/required}}
{{/isPrimitiveType}}{{/isMap}}{{/isBinary}}{{/isByteArray}}
{{/vars}}
params.join(",").to_string()
}
}
/// Converts Query Parameters representation (style=form, explode=false) to a {{{classname}}} value
/// as specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde deserializer
impl std::str::FromStr for {{{classname}}} {
type Err = String;
fn from_str(s: &str) -> std::result::Result {
#[derive(Default)]
// An intermediate representation of the struct to use for parsing.
struct IntermediateRep {
{{#vars}}
pub {{{name}}}: Vec<{{{dataType}}}>,
{{/vars}}
}
let mut intermediate_rep = IntermediateRep::default();
// Parse into intermediate representation
let mut string_iter = s.split(',').into_iter();
let mut key_result = string_iter.next();
while key_result.is_some() {
let val = match string_iter.next() {
Some(x) => x,
None => return std::result::Result::Err("Missing value while parsing {{{classname}}}".to_string())
};
if let Some(key) = key_result {
match key {
{{#vars}}
{{#isBinary}}
"{{{baseName}}}" => return std::result::Result::Err("Parsing binary data in this style is not supported in {{{classname}}}".to_string()),
{{/isBinary}}
{{^isBinary}}
{{#isByteArray}}
"{{{baseName}}}" => return std::result::Result::Err("Parsing binary data in this style is not supported in {{{classname}}}".to_string()),
{{/isByteArray}}
{{^isByteArray}}
{{#isContainer}}
"{{{baseName}}}" => return std::result::Result::Err("Parsing a container in this style is not supported in {{{classname}}}".to_string()),
{{/isContainer}}
{{^isContainer}}
{{#isNullable}}
"{{{baseName}}}" => return std::result::Result::Err("Parsing a nullable type in this style is not supported in {{{classname}}}".to_string()),
{{/isNullable}}
{{^isNullable}}
"{{{baseName}}}" => intermediate_rep.{{{name}}}.push(<{{{dataType}}} as std::str::FromStr>::from_str(val).map_err(|x| format!("{}", x))?),
{{/isNullable}}
{{/isContainer}}
{{/isByteArray}}
{{/isBinary}}
{{/vars}}
_ => return std::result::Result::Err("Unexpected key while parsing {{{classname}}}".to_string())
}
}
// Get the next key
key_result = string_iter.next();
}
// Use the intermediate representation to return the struct
std::result::Result::Ok({{{classname}}} {
{{#vars}}
{{#isNullable}}
{{{name}}}: std::result::Result::Err("Nullable types not supported in {{{classname}}}".to_string())?,
{{/isNullable}}
{{^isNullable}}
{{{name}}}: intermediate_rep.{{{name}}}.into_iter().next(){{#required}}.ok_or("{{{baseName}}} missing in {{{classname}}}".to_string())?{{/required}},
{{/isNullable}}
{{/vars}}
})
}
}
{{/arrayModelType}}
// Methods for converting between header::IntoHeaderValue<{{{classname}}}> and hyper::header::HeaderValue
#[cfg(any(feature = "client", feature = "server"))]
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 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))
}
}
}
{{/dataType}}
{{/isEnum}}
{{#usesXml}}
{{#usesXmlNamespaces}}
{{#xmlNamespace}}
impl {{{classname}}} {
/// Associated constant for this model's XML namespace.
#[allow(dead_code)]
pub const NAMESPACE: &'static str = "{{{xmlNamespace}}}";
}
{{/xmlNamespace}}
{{/usesXmlNamespaces}}
impl {{{classname}}} {
/// Helper function to allow us to convert this model to an XML string.
/// Will panic if serialisation fails.
#[allow(dead_code)]
pub(crate) fn to_xml(&self) -> String {
{{#xmlNamespace}}
let mut namespaces = std::collections::BTreeMap::new();
// An empty string is used to indicate a global namespace in xmltree.
namespaces.insert("".to_string(), Self::NAMESPACE.to_string());
serde_xml_rs::to_string_with_namespaces(&self, namespaces).expect("impossible to fail to serialize")
{{/xmlNamespace}}
{{^xmlNamespace}}
serde_xml_rs::to_string(&self).expect("impossible to fail to serialize")
{{/xmlNamespace}}
}
}
{{/usesXml}}
{{/model}}
{{/models}}