All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rust.model.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>partial_header}}
{{#models}}
{{#model}}
{{#description}}
/// {{{classname}}} : {{{description}}}
{{/description}}

{{!-- 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}}
        }
    }
}

impl Default for {{{classname}}} {
    fn default() -> {{{classname}}} {
        {{#allowableValues}}
        Self::{{ enumVars.0.name }}
        {{/allowableValues}}
    }
}
{{/isEnum}}

{{!-- for schemas that have a discriminator --}}
{{#discriminator}}
#[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}}Box<{{{dataType}}}>{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^required}}>{{/required}},
    {{/vars}}
    },
    {{/x-mapped-models}}
{{/vendorExtensions}}
}

{{/discriminator}}

{{!-- for non-enum schemas --}}
{{^isEnum}}
{{^discriminator}}
#[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}}Box<{{{dataType}}}>{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{^required}}>{{/required}},
{{/vars}}
}

impl {{{classname}}} {
    {{#description}}
    /// {{{.}}}
    {{/description}}
    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}}{{#isContainer}}{{#isArray}}: None{{/isArray}}{{#isMap}}: None{{/isMap}}{{^isArray}}{{^isMap}}{{#isNullable}}: None{{/isNullable}}{{/isMap}}{{/isArray}}{{/isContainer}}{{^isContainer}}: None{{/isContainer}}{{/required}}{{#required}}{{#isModel}}: {{^isNullable}}Box::new({{{name}}}){{/isNullable}}{{#isNullable}}if let Some(x) = {{{name}}} {Some(Box::new(x))} else {None}{{/isNullable}}{{/isModel}}{{/required}},
            {{/vars}}
        }
    }
}
{{/discriminator}}
{{/isEnum}}

{{!-- for properties that are of enum type --}}
{{#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}}

{{/model}}
{{/models}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy