Skip to content

How to rename enum variants? #749

@NicoSchmidt1703

Description

@NicoSchmidt1703

Hey,

I tried to use typify to create types from my json schema. Currently, I'm creating my types via build.rs.

Here you see the result, which is created:

///xx
///
/// <details><summary>JSON schema</summary>
///
/// ```json
///{
///  "description": "xx",
///  "type": "string",
///  "enum": [
///    "=",
///    "!=",
///    "in",
///    "out",
///    "<",
///    ">",
///    "<=",
///    ">=",
///    "contains"
///  ],
///  "example": "="
///}
/// ```
/// </details>
#[derive(
    ::serde::Deserialize,
    ::serde::Serialize,
    Clone,
    Copy,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd
)]
pub enum Operator {
    #[serde(rename = "=")]
    X,
    #[serde(rename = "!=")]
    X,
    #[serde(rename = "in")]
    In,
    #[serde(rename = "out")]
    Out,
    #[serde(rename = "<")]
    X,
    #[serde(rename = ">")]
    X,
    #[serde(rename = "<=")]
    X,
    #[serde(rename = ">=")]
    X,
    #[serde(rename = "contains")]
    Contains,
}

As you see, my enum (which I sadly cannot change) has values like "=", "!=" etc., which are invalid rust identifier. Therefor, typify will replace it with an capital X. The issue is now, that my enum have multiple variants which called X. Therefor, the following won't work as designed and by build fails:

impl ::std::fmt::Display
for CrmDiscountCustomerConditionsSelectionsItemConditionsItemOperator {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        match *self {
            Self::X => write!(f, "="),
            Self::X => write!(f, "!="),
            Self::In => write!(f, "in"),
            Self::Out => write!(f, "out"),
            Self::X => write!(f, "<"),
            Self::X => write!(f, ">"),
            Self::X => write!(f, "<="),
            Self::X => write!(f, ">="),
            Self::Contains => write!(f, "contains"),
        }
    }
}

How can I rename my enum variants? I tired the following:

    let mut settings = TypeSpaceSettings::default();
    settings.with_derive(String::from("PartialEq"));
    settings.with_derive(String::from("Debug"));
    settings.with_derive(String::from("Clone"));
    settings.with_patch(
        "Operator::=",
        TypeSpacePatch::default().with_rename("OperatorEq".to_owned()),
    );

    let mut type_space = TypeSpace::new(&settings);
    type_space.add_root_schema(schema).unwrap();

    let output = prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap());

    let mut out_file = std::path::Path::new(&out_dir).to_path_buf();
    out_file.push("schema.rs");
    std::fs::write(out_file, output).unwrap();

Thanks!

see:

None => to_case("x"),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions