/*
DO NOT MODIFY THIS FILE {# unless you see this comment #}.  It is automatically generated and changes will be over-written
on the next generation.

It contains enumerations used by the {{stinger.name}} interface.
*/

{%if stinger.enums | length > 0%}
use std::fmt;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
{%endif%}
use serde::{Serialize, Deserialize};

{%for ie_name, ie in stinger.enums.items() %}
#[repr(u32)]
#[derive(Debug, FromPrimitive, ToPrimitive, Clone, Serialize, Deserialize)]
#[serde(into = "u32", try_from = "u32")]
pub enum {{ie_name | UpperCamelCase }} {
    {%-for value in ie.values %}
    {{value | UpperCamelCase}} = {{loop.index}}{%if not loop.last%},{%endif%}
    {%-endfor%}
}

#[allow(dead_code)]
impl {{ie_name | UpperCamelCase }} {
    pub fn from_u32(value: u32) -> Option<Self> {
        FromPrimitive::from_u32(value)
    }
}

impl From<{{ie_name | UpperCamelCase }}> for u32 {
    fn from(s: {{ie_name | UpperCamelCase }}) -> u32 {
        s as u32
    }
}

impl From<u32> for {{ie_name | UpperCamelCase }} {
    fn from(s: u32) -> {{ie_name | UpperCamelCase }} {
        {{ie_name | UpperCamelCase }}::from_u32(s).unwrap()
    }
}

impl fmt::Display for {{ie_name | UpperCamelCase }} {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
       write!(f, "{:?}", self)
    }
}
{%endfor%}

{%for struct_name, struct in stinger.structs.items() %}
#[allow(dead_code, non_snake_case)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct {{struct_name | UpperCamelCase}} {
    {%-for arg in struct.members%}
    {%-if arg.arg_type.name.lower() == 'primitive'%}
    pub {{arg.name}}: {{arg.rust_type}},
    {%-elif arg.arg_type.name.lower() == 'enum'%}
    pub {{arg.name}}: {{arg.rust_local_type}},
    {%endif-%}
    {%endfor%}
}
{%-endfor%}

#[allow(dead_code)]
#[derive(Debug)]
pub enum MethodResultCode { {%-for rc_i, rc_name in stinger.method_return_codes.items()%}
    {{rc_name | UpperCamelCase}} = {{rc_i}},
    {%-endfor%}
}

{%for method_name, method in stinger.methods.items()%}
// Structures for `{{method_name}}` method

#[allow(dead_code, non_snake_case)]
#[derive(Clone, Debug, Serialize, Deserialize)]
/// Request Object for `{{method_name}}`
pub struct {{method_name | UpperCamelCase}}RequestObject {
    {%-for arg in method.arg_list%}
    {%-if arg.arg_type.name.lower() == 'primitive'%}
    pub {{arg.name}}: {{arg.rust_type}},
    {%-elif arg.arg_type.name.lower() == 'enum'-%}
    pub {{arg.name}}: u32,
    {%endif-%}
    {%endfor%}
}

{%if method.return_value_type == 'struct' %}
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Return Object for `{{method_name}}`
pub struct {{method.return_value_name | UpperCamelCase}} {
{%-for arg in method.return_value %}
    pub {{arg.name}}: {{arg.rust_local_type}},
{%-endfor%}
}
{%elif method.return_value_type == 'primitive'%}
#[derive(Debug, Clone, Serialize)]
pub struct {{method.return_value_name | UpperCamelCase}} {
    pub {{method.return_value_property_name}}: {{method.return_value.rust_local_type}},
}
{%else%}
#[derive(Debug, Clone, Serialize)]
/// Empty (no parameters) return structure for the `{{method_name}}` method.
pub struct {{method.return_value_name | UpperCamelCase}} {
}
{%endif-%}
{%-endfor%}
{%for sig_name, sig in stinger.signals.items()%}
// Structures for `{{sig_name}}` signal
#[allow(dead_code, non_snake_case)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct {{sig_name | UpperCamelCase}}SignalPayload {
    {%-for arg in sig.arg_list%}
    {%-if arg.arg_type.name.lower() == 'primitive'%}
    pub {{arg.name}}: {{arg.rust_type}},
    {%elif arg.arg_type.name.lower() == 'enum'-%}
    pub {{arg.name}}: {{arg.rust_local_type}},
    {%endif-%}
    {%endfor%}
}
{%endfor%}

{%for prop_name, prop in stinger.properties.items()%}
// `{{prop_name}}` property structure.
#[allow(dead_code, non_snake_case)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct {{prop_name | UpperCamelCase}}Property {
    {%for arg in prop.arg_list%}
    {%-if arg.arg_type.name.lower() == 'primitive' -%}
    pub {{arg.name}}: {{arg.rust_type}},
    {%elif arg.arg_type.name.lower() == 'enum'-%}
    pub {{arg.name}}: {{arg.rust_local_type}},
    {%elif arg.arg_type.name.lower() == 'struct'-%}
    pub {{arg.name}}: {{arg.rust_local_type}},
    {%endif-%}
    {%endfor%}
}
{%endfor%}
