/// Print an enum defined in the schema
Print an enum defined in the schema
Print an enum defined in the schema
fn print_enum(out: &mut StringWriter, _lookup: &HashMap<String, SchemaUnion>, class: &Class) {
    if let Some(comment) = class.comment.as_ref() {
        out.line(format!("/// {}", comment));
        out.line("///");
    }
    out.line(format!("/// ID: {}", class.id_aliased()));
    out.line("#[derive(Debug, PartialEq, Clone)]");
    out.line(r#"#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]"#);
    out.line(format!("pub enum {} {{", class.name));
    out.inc_indent();
    for val in &class.enum_vals {
        if let Some(comment) = val.comment.as_ref() {
            out.line(format!("/// {}", comment));
        }
        let label = if let Some(label) = val.label.as_ref() {
            label
        } else {
            &val.name
        };
        let has_type = if let Some(ty) = val.ty.as_ref() {
            format!("({})", ty)
        } else {
            "".to_string()
        };
        out.line(format!(r#"#[cfg_attr(feature = "with_serde", serde(rename = "{}"))]"#, label.to_kebab_case()));
        out.line(format!("{}{},", val.name, has_type));
    }
    out.dec_indent();
    out.line("}");
    if class.properties().len() == 0 { return; }
    // now print our impls
    out.nl();
    out.line(format!("impl {} {{", class.name));
    out.inc_indent();
    for prop in &class.properties() {
        let prop_enum_vals = class.prop_enum_vals(prop);
        let returnclass = prop.ty.to_string().to_camel_case();
        let partial_impl = prop_enum_vals.len() != class.enum_vals.len();
        let returntype = if partial_impl {
            format!("Option<{}>", returnclass)
        } else {
            returnclass.clone()
        };
        out.line("#[allow(dead_code)]");
        out.line(format!("pub fn {}(&self) -> {} {{", prop.name.to_snake_case(), returntype));
        out.inc_indent();
        out.line("match self {");
        out.inc_indent();
        for enumval in prop_enum_vals {
            let implval = enumval.find_impl(&prop.name).expect("missing enum impl val!");
            let implreturn = format!("{}::{}", returnclass, implval.enumval);
            let implreturn = if partial_impl {
                format!("Some({})", implreturn)
            } else {
                implreturn
            };
            out.line(format!("Self::{} => {},", enumval.name, implreturn));
        }
        if partial_impl {
            out.line("_ => None,");
        }
        out.dec_indent();
        out.line("}");
        out.dec_indent();
        out.line("}");
        out.nl();
    }
    out.dec_indent();
    out.line("}");
}
fn print_enum ( out : & mut StringWriter , _lookup : & HashMap < String , SchemaUnion > , class : & Class ) { if let Some ( comment ) = class . comment . as_ref ( ) { out . line ( format ! ( " " comment ) ) ; out . line ( " " ) ; } out . line ( format ! ( " " class id_aliased ( ) ) ) ; out . line ( " " ) ; out . line ( r#"#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]"# ) ; out . line ( format ! ( " " class name ) ) ; out . inc_indent ( ) ; for val in & class . enum_vals { if let Some ( comment ) = val . comment . as_ref ( ) { out . line ( format ! ( " " comment ) ) ; } let label = if let Some ( label ) = val . label . as_ref ( ) { label } else { & val . name } ; let has_type = if let Some ( ty ) = val . ty . as_ref ( ) { format ! ( " " ty ) } else { " " . to_string ( ) } ; out . line ( format ! ( r#"#[cfg_attr(feature = "with_serde", serde(rename = "{}"))]"# label to_kebab_case ( ) ) ) ; out . line ( format ! ( " " val name has_type ) ) ; } out . dec_indent ( ) ; out . line ( " " ) ; if class . properties ( ) . len ( ) == 0 { return ; } // now print our impls out . nl ( ) ; out . line ( format ! ( " " class name ) ) ; out . inc_indent ( ) ; for prop in & class . properties ( ) { let prop_enum_vals = class . prop_enum_vals ( prop ) ; let returnclass = prop . ty . to_string ( ) . to_camel_case ( ) ; let partial_impl = prop_enum_vals . len ( ) != class . enum_vals . len ( ) ; let returntype = if partial_impl { format ! ( " " returnclass ) } else { returnclass . clone ( ) } ; out . line ( " " ) ; out . line ( format ! ( " " prop name to_snake_case ( ) returntype ) ) ; out . inc_indent ( ) ; out . line ( " " ) ; out . inc_indent ( ) ; for enumval in prop_enum_vals { let implval = enumval . find_impl ( & prop . name ) . expect ( " " ) ; let implreturn = format ! ( " " returnclass implval enumval ) ; let implreturn = if partial_impl { format ! ( " " implreturn ) } else { implreturn } ; out . line ( format ! ( " " enumval name implreturn ) ) ; } if partial_impl { out . line ( " " ) ; } out . dec_indent ( ) ; out . line ( " " ) ; out . dec_indent ( ) ; out . line ( " " ) ; out . nl ( ) ; } out . dec_indent ( ) ; out . line ( " " ) ; }
Print an enum defined in the schema
Print an enum defined in the schema
out
&mut StringWriter
_lookup
&HashMap<String, SchemaUnion>
class
&Class