Skip to content

feat(Rust): Add integer type inference option#2791

Merged
schani merged 1 commit into
glideapps:masterfrom
gamkay:feat/rust
Jul 20, 2026
Merged

feat(Rust): Add integer type inference option#2791
schani merged 1 commit into
glideapps:masterfrom
gamkay:feat/rust

Conversation

@jonashao

Copy link
Copy Markdown
Contributor

Description

This PR introduces integer type inference for Rust targets, providing better control over generated integer types (i32/i64):

  1. Added IntegerType enum with variants:

    • conservative: Selects i32/i64 based on JSON sample ranges
    • force-i32: Always uses i32 (caution: risk of overflow)
    • force-i64: Default behavior (current output)
  2. Added integerType configuration option for CLI and programmatic interfaces:

    quicktype --integer-type conservative  # Smart selection (default)
    quicktype --integer-type force-i32

Related Issue

#2790
(Original feature request: #2790)

Motivation and Context

  • ⚠️ Problem: Current behavior always generates i64 even when values fit in i32, causing:
    • Memory bloat (4-byte vs 8-byte overhead)
    • Compatibility issues with Rust libraries expecting i32
    • Serialization/deserialization performance penalties
  • Solution: Gives users control to optimize for:
    • Memory efficiency (force-i32)
    • Safety (force-i64)
    • Smart balancing (conservative)

Previous Behaviour / Output

All integers unconditionally became i64:

// JSON schema input:  {"id": {"type": "integer","minimum": 0, "maximum": 100}}
pub struct Data {
    pub id: i64,  // ← Always i64 even for small values
}

New Behaviour / Output

With --integer-type conservative:

// JSON schema input: {"id": {"type": "integer","minimum": 0, "maximum": 100}, "big": {"type": "integer","minimum": 0, "maximum": 9223372036854775807}}
pub struct Data {
    pub id: i32,   // ← conservative-downgraded to i32
    pub big: i64,  // ← Remains i64 for large values
}

How Has This Been Tested?

  1. Unit Tests:

    • Added integer-type.schema test input and rust langauge quickTestRendererOptions:
      • Range detection logic (conservative mode)
      • Forced type behaviors (force-i32/force-i64)
    • Integration tests for CLI flag parsing
  2. Validation Tests:

    # Tested with sample datasets:
    script/quicktype -s schema test/inputs/schema/integer-type.schema -o out.rs 
    script/quicktype -s schema test/inputs/schema/integer-type.schema -o out.rs  --integer-type force-i32
    script/quicktype -s schema test/inputs/schema/integer-type.schema -o out.rs  --integer-type force-i64
    • Verified output compiles with cargo check
  3. Environment:

    • Rust 1.87 + TypeScript 5.0
    • Windows/Linux/macOS cross-validation

1. Added IntegerType enum
2. Introduced integerType configuration option
 - Supports forced i32/i64 usage
 - Enables automatic selection based on numerical range
@schani

schani commented Jul 9, 2026

Copy link
Copy Markdown
Member

This PR has merge conflicts with master, and "allow edits from maintainers" is not enabled on the fork, so we can't update the branch for you. Could you merge master (or rebase) and resolve the conflicts? Enabling maintainer edits would also let us help with future conflicts. Thanks!

@schani

schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Thanks for this feature, @jonashao — it's exactly what #2790 asked for, and we want it in the upcoming major release.

Since this PR doesn't allow maintainer edits, we've carried it forward in #2964, with your commit as the base of that branch so your authorship is preserved. On top of it we fixed the conservative mode to require both bounds before choosing i32 (a one-sided minimum no longer downgrades the type), integrated with the integer-range machinery that landed in the meantime, and reworked the test schema so it passes for every language — which also resolved the C++ skip you'd noted (the ±2^63 bounds were rounding up as doubles and overflowing int64_t constants).

If you'd prefer to update this PR yourself instead, we're happy to review — otherwise we'll proceed with #2964. Thanks again!

@schani
schani merged commit 57dd956 into glideapps:master Jul 20, 2026
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

Successfully merging this pull request may close these issues.

2 participants