10000 feat(Rust): Add integer type inference option by jonashao · Pull Request #2 · gamkay/quicktype · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/quicktype-core/src/language/Rust/RustRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
removeNullFromUnion,
} from "../../Type/TypeUtils";

import { minMaxValueForType } from "../../attributes/Constraints";
import { keywords } from "./constants";
import type { rustOptions } from "./language";
import { IntegerType } from "./language";
import {
Density,
type NamingStyleKey,
Expand Down Expand Up @@ -96,6 +98,33 @@ export class RustRenderer extends ConvenienceRenderer {
return "/// ";
}

private getIntegerType(integerType: Type): string {
switch (this._options.integerType) {
case IntegerType.ForceI32:
return "i32";
case IntegerType.ForceI64:
return "i64";
case IntegerType.Conservative:
default: {
const minMax = minMaxValueForType(integerType);
if (minMax !== undefined) {
const [min, max] = minMax;
// Check if values fit in i32 range: [-2147483648, 2147483647]
const i32Min = -2147483648;
const i32Max = 2147483647;

if (
(min === undefined || min >= i32Min) &&
(max === undefined || max <= i32Max)
) {
return "i32";
}
}
return "i64";
}
}
}

private nullableRustType(t: Type, withIssues: boolean): Sourcelike {
return ["Option<", this.breakCycle(t, withIssues), ">"];
}
Expand All @@ -121,7 +150,7 @@ export class RustRenderer extends ConvenienceRenderer {
"Option<serde_json::Value>",
),
(_boolType) => "bool",
(_integerType) => "i64",
(integerType) => this.getIntegerType(integerType),
(_doubleType) => "f64",
(_stringType) => "String",
(arrayType) => [
Expand Down
16 changes: 16 additions & 0 deletions packages/quicktype-core/src/language/Rust/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import type { LanguageName, RendererOptions } from "../../types";
import { RustRenderer } from "./RustRenderer";
import { Density, Visibility } from "./utils";

export enum IntegerType {
Conservative = "conservative",
ForceI32 = "force-i32",
ForceI64 = "force-i64",
}

export const rustOptions = {
density: new EnumOption(
"density",
Expand All @@ -30,6 +36,16 @@ export const rustOptions = {
} as const,
"private",
),
integerType: new EnumOption(
"integer-type",
"Integer type inference",
{
conservative: IntegerType.Conservative,
"force-i32": IntegerType.ForceI32,
"force-i64": IntegerType.ForceI64,
} as const,
"conservative",
),
deriveDebug: new BooleanOption("derive-debug", "Derive Debug impl", false),
deriveClone: new BooleanOption("derive-clone", "Derive Clone impl", false),
derivePartialEq: new BooleanOption(
Expand Down
8 changes: 8 additions & 0 deletions test/inputs/schema/integer-type.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"small_positive": 50,
"large_positive": 2500000000,
"small_negative": -50,
"large_negative": -2500000000,
"i32_range": 1000000000,
"beyond_i32": 9000000000000000000
}
43 changes: 43 additions & 0 deletions test/inputs/schema/integer-type.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "object",
"properties": {
"small_positive": {
"type": "integer",
"minimum": 0,
"maximum": 100
},
"large_positive": {
"type": "integer",
"minimum": 0,
"maximum": 5000000000
},
"small_negative": {
"type": "integer",
"minimum": -100,
"maximum": 0
},
"large_negative": {
"type": "integer",
"minimum": -5000000000,
"maximum": 0
},
"i32_range": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647
},
"beyond_i32": {
"type": "integer",
"minimum": -9223372036854775808,
"maximum": 9223372036854775807
}
},
"required": [
"small_positive",
"large_positive",
"small_negative",
"large_negative",
"i32_range",
"beyond_i32"
]
}
5 changes: 5 additions & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ export const RustLanguage: Language = {
{ visibility: "crate" },
{ visibility: "private" },
{ visibility: "public" },
{ "integer-type": "conservative" },
{ "integer-type": "force-i32" },
{ "integer-type": "force-i64" },
],
sourceFiles: ["src/language/Rust/index.ts"],
};
Expand Down Expand Up @@ -584,6 +587,8 @@ export const CPlusPlusLanguage: Language = {
skipSchema: [
// uses too much memory
"keyword-unions.schema",
// seems like a problem with integer range check
"integer-type.schema",
],
rendererOptions: {},
quickTestRendererOptions: [
Expand Down
Loading
0