oxidecomputer / oxidecomputer/typify

Enums of named integer constants/enums with "other" variant

Open
#574 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

This schema:

{
    "title": "My enum",
    "oneOf": [
        {
            "const": 1,
            "description": "Foo",
            "type": "integer"
        },
        {
            "const": 5,
            "description": "Bar",
            "type": "integer"
        }
    ]
}

generates this non-functional output:

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum MyEnum {
    Variant0(i64),
    Variant1(i64),
}

Desured output would be:

pub enum MyEnum {
    Foo = 1,
    Bar = 5,
}

I think the serde_repr crate could be used in this case.

Similarly, this:

{
    "title": "My enum",
    "anyOf": [
        {
            "const": 1,
            "description": "Foo",
            "type": "integer"
        },
        {
            "const": 5,
            "description": "Bar",
            "type": "integer"
        },
        {
            "type": "integer"
        }
    ]
}

generates

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MyEnum {
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub subtype_0: Option<i64>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub subtype_1: Option<i64>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub subtype_2: Option<i64>,
}

Desired output would be something like

pub enum MyEnum {
    Foo = 1,
    Bar = 5,
    Other(i64)
}

I think for integer variants this would require custom (de)serialization code. However, this Other feature would also be useful for enums with string variants, where I believe it might be accomplished with #[serde(other)]
Examples in the wild:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the enum-generation entry point and existing Serde handling; the issue names serde_repr and serde(other) as relevant approaches. Done means integer const variants generate functional Rust enums and schemas with an unconstrained integer or string variant produce an Other-style fallback with correct serialization and deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.