{{>partial_header}} use serde::{Deserialize, Serialize}; {{#models}} {{#model}} {{!-- for enum schemas --}} {{#isEnum}} /// {{{description}}} #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub enum {{{classname}}} { {{#allowableValues}} {{#enumVars}} #[serde(rename = "{{{value}}}")] {{{name}}}, {{/enumVars}} {{/allowableValues}} } impl ToString for {{{classname}}} { fn to_string(&self) -> String { match self { {{#allowableValues}} {{#enumVars}} Self::{{{name}}} => String::from("{{{value}}}"), {{/enumVars}} {{/allowableValues}} } } } {{/isEnum}} {{!-- for schemas that have a discriminator --}} {{#discriminator}} compiler_error!("not implemented") /// {{{description}}} #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "{{{vendorExtensions.x-tag-name}}}")] pub enum {{{classname}}} { {{#vendorExtensions}} {{#x-mapped-models}} #[serde(rename="{{mappingName}}")] {{{modelName}}} { {{#vars}} {{#description}} /// {{{.}}} {{/description}} #[serde(rename = "{{{baseName}}}"{{^required}}, skip_serializing_if = "Option::is_none"{{/required}})] {{{name}}}: {{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{^required}}Option<{{/required}}{{#isEnum}}{{{enumName}}}{{/isEnum}}{{^isEnum}}{{#isModel}}{{{dataType}}}{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^required}}>{{/required}}, {{/vars}} }, {{/x-mapped-models}} {{/vendorExtensions}} } {{/discriminator}} {{!-- for non-enum schemas --}} {{^isEnum}} {{^discriminator}} {{#oneOf.isEmpty}} /// {{{description}}} #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct {{{classname}}} { {{#vars}} {{#description}} /// {{{.}}} {{/description}} #[serde(rename = "{{{baseName}}}"{{^required}}{{#isNullable}}, default, with = "::serde_with::rust::double_option"{{/isNullable}}{{/required}}{{^required}}, skip_serializing_if = "Option::is_none"{{/required}}{{#required}}{{#isNullable}}, deserialize_with = "Option::deserialize"{{/isNullable}}{{/required}})] pub {{{name}}}: {{#isNullable}}Option<{{/isNullable}}{{^required}}Option<{{/required}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isModel}}{{{dataType}}}{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{^required}}>{{/required}}, {{/vars}} } {{^requiredVars.0}} impl Default for {{{classname}}} { fn default() -> Self { Self::new() } } {{/requiredVars.0}} impl {{{classname}}} { /// Create value with optional fields set to `None`. #[allow(clippy::too_many_arguments)] pub fn new({{#requiredVars}}{{{name}}}: {{#isNullable}}Option<{{/isNullable}}{{#isEnum}}{{#isArray}}Vec<{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{^-last}}, {{/-last}}{{/requiredVars}}) -> {{{classname}}} { {{{classname}}} { {{#vars}} {{{name}}} {{^required}} {{#isArray}}: None{{/isArray}} {{#isMap}}: None{{/isMap}} {{#isContainer}}{{#isNullable}}: None{{/isNullable}}{{/isContainer}} {{^isContainer}}: None{{/isContainer}} {{/required}}, {{/vars}} } } } {{/oneOf.isEmpty}} {{/discriminator}} {{/isEnum}} {{!-- for properties that are of enum type --}} {{#oneOf.isEmpty}} {{#vars}} {{#isEnum}} /// {{{description}}} #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub enum {{{enumName}}} { {{#allowableValues}} {{#enumVars}} #[serde(rename = "{{{value}}}")] {{{name}}}, {{/enumVars}} {{/allowableValues}} } impl Default for {{{enumName}}} { fn default() -> {{{enumName}}} { {{#allowableValues}} Self::{{ enumVars.0.name }} {{/allowableValues}} } } {{/isEnum}} {{/vars}} {{/oneOf.isEmpty}} {{!-- OneOf generation --}} {{^oneOf.isEmpty}} /// {{{description}}} #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum {{{classname}}} { {{#composedSchemas.oneOf}} {{^isContainer}} {{complexType}}({{{dataType}}}), {{/isContainer}} {{#isContainer}} {{complexType}}Array({{{dataType}}}), {{/isContainer}} {{/composedSchemas.oneOf}} } impl ToString for {{{classname}}} { fn to_string(&self) -> String { {{!-- We have to extract the inner, as the api does not care --}} match self { {{#composedSchemas.oneOf}} {{^isContainer}} Self::{{complexType}}(val) => serde_json::to_string(&val).unwrap(), {{/isContainer}} {{#isContainer}} Self::{{complexType}}Array(val) => serde_json::to_string(&val).unwrap(), {{/isContainer}} {{/composedSchemas.oneOf}} } } } {{/oneOf.isEmpty}} {{/model}} {{/models}}