Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to rename enum variants? #749

Open
NicoSchmidt1703 opened this issue Jan 21, 2025 · 1 comment
Open

How to rename enum variants? #749

NicoSchmidt1703 opened this issue Jan 21, 2025 · 1 comment

Comments

@NicoSchmidt1703
Copy link

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"),

@NicoSchmidt1703 NicoSchmidt1703 changed the title How to rename rnum variants? How to rename enum variants? Jan 21, 2025
@ahl
Copy link
Collaborator

ahl commented Jan 22, 2025

Unfortunately, there isn't a way to do this currently. In a future version we intend to have a generic renaming by json path. In this case, that might look like:

{
    "#/enum/<": "gt",
   ..
}

We'd do something equivalent with the builder interface you're using and for the macro interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants