Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Codegen issues with string enums

Aperta
#862 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Inizia da xtask/src/generate_structs.rs alla riga 68 e dal punto di ingresso della generazione in xtask/src/main.rs alla riga 28, quindi riproduci il problema dello schema glTF. Traccia come vengono rappresentate le voci anyOf contenenti valori const di tipo string e una string generale. Il lavoro è completato quando la generazione riesce e produce un enum con le varianti denominate documentate e una variante string di fallback, invece dei campi di sottotipo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

fancy types

First up, thank you for your work on typify, great crate. Helped me a few times already.

Right now I'm attempting to build a system to handle glTF files, and apparently typify has some issues with their string enum definitions.

Their definitions look as follows (this is one example, there are multiple of those in their schemas):

[...]
"interpolation": {
            "description": "Interpolation algorithm.",
            "default": "LINEAR",
            "gltf_detailedDescription": "Interpolation algorithm.",
            "anyOf": [
                {
                    "const": "LINEAR",
                    "description": "The animated values are linearly interpolated between keyframes. When targeting a rotation, spherical linear interpolation (slerp) **SHOULD** be used to interpolate quaternions. The number of output elements **MUST** equal the number of input elements."
                },
                {
                    "const": "STEP",
                    "description": "The animated values remain constant to the output of the first keyframe, until the next keyframe. The number of output elements **MUST** equal the number of input elements."
                },
                {
                    "const": "CUBICSPLINE",
                    "description": "The animation's interpolation is computed using a cubic spline with specified tangents. The number of output elements **MUST** equal three times the number of input elements. For each input element, the output stores three elements, an in-tangent, a spline vertex, and an out-tangent. There **MUST** be at least two keyframes when using this interpolation."
                },
                {
                    "type": "string"
                }
            ]
        }
[...]

where I'd expect a codegen such as:

#[derive(Deserialize, Serialize, Default)]
pub enum Interpolation {
  #[default]
  #[serde(rename = "LINEAR")]
  #[doc = "The animated values are linearly interpolated between keyframes. When targeting a rotation, spherical linear interpolation (slerp) **SHOULD** be used to interpolate quaternions. The number of output elements **MUST** equal the number of input elements."]
  Linear,
  #[serde(rename = "STEP")]
  #[doc = "BLAH"]
  Step,
  #[serde(rename = "CUBICSPLINE")]
  #[doc = "BLAH x2"]
  Cubicspline,
  #[serde(other)]
  Other(String),
}

In its current state, typify just errors out (which is better than producing faulty definitions, I guess).

Backtrace
Error: value does not conform to the given schema

Stack backtrace:
   0: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from
             at /home/aumetra/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anyhow-1.0.98/src/backtrace.rs:27:14
   1: <core::result::Result<T,F> as core::ops::try_trait::FromResidual<core::result::Result<core::convert::Infallible,E>>>::from_residual
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:2050:27
   2: xtask::generate_structs::do_it
             at ./xtask/src/generate_structs.rs:68:5
   3: xtask::main
             at ./xtask/src/main.rs:28:44
   4: core::ops::function::FnOnce::call_once
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
   5: std::sys::backtrace::__rust_begin_short_backtrace
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:152:18
   6: std::rt::lang_start::{{closure}}
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:199:18
   7: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/core/src/ops/function.rs:284:13
   8: std::panicking::try::do_call
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:589:40
   9: std::panicking::try
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:552:19
  10: std::panic::catch_unwind
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panic.rs:359:14
  11: std::rt::lang_start_internal::{{closure}}
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/rt.rs:168:24
  12: std::panicking::try::do_call
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:589:40
  13: std::panicking::try
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panicking.rs:552:19
  14: std::panic::catch_unwind
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/panic.rs:359:14
  15: std::rt::lang_start_internal
             at /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/std/src/rt.rs:164:5
  16: std::rt::lang_start
             at /home/aumetra/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:198:5
  17: main
  18: <unknown>
  19: __libc_start_main
  20: _start

By patching out some of the sanity checks, I got it to emit some definitions, and the definitions look like this:

#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct AnimationSamplerInterpolation {
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_0: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_1: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_2: ::std::option::Option<::serde_json::Value>,
    #[serde(
        flatten,
        default,
        skip_serializing_if = "::std::option::Option::is_none"
    )]
    pub subtype_3: ::std::option::Option<::std::string::String>,
}

Where I can sorta get where the generator is coming from when it generated it.


Unfortunately I don't have too much knowledge about the internals to be able to come up with a solution that generates correct types in this scenario.

Lingua principale
Rust
Stelle
898
Fork
114
Merge medio
4h 18m
PR unite (30g)
14

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di oxidecomputer/typify

Tutte le issue di oxidecomputer/typify

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.