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

chore(rust): Strongly type Rust NamingStyles #2602

Merged
merged 50 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6e557fe
standardise json formatting
inferrinizzard Mar 19, 2024
a7cec76
add file association for schema files
inferrinizzard Mar 19, 2024
6140704
Merge branch 'master' into chore/repo/eslint
inferrinizzard Apr 7, 2024
b2a7904
install eslint packages
inferrinizzard Apr 7, 2024
b97c2b2
add eslint config
inferrinizzard Apr 7, 2024
e504a8a
remove tslint
inferrinizzard Apr 7, 2024
92bd563
eslint autofix
inferrinizzard Apr 7, 2024
392838a
update eslint rules, eslintignore
inferrinizzard Apr 8, 2024
d9baf6e
add lint:fix script
inferrinizzard Apr 8, 2024
8bbabf1
update eslint rules, eslintignore
inferrinizzard Apr 8, 2024
f1d19aa
add lint:fix script
inferrinizzard Apr 8, 2024
994567e
add import rules
inferrinizzard Apr 8, 2024
d259874
add import rules
inferrinizzard Apr 8, 2024
291f5c9
update import rules
inferrinizzard Apr 12, 2024
03d8001
reduce excess style rules
inferrinizzard Apr 12, 2024
99dd240
downgrade remaining to warnings
inferrinizzard Apr 13, 2024
7f22254
fix enum values
inferrinizzard Apr 8, 2024
885b35a
add all missing accessibility modifiers
inferrinizzard Apr 11, 2024
f791721
fix nullish errors
inferrinizzard Apr 12, 2024
db9a9cf
update import rules
inferrinizzard Apr 12, 2024
3088be7
fix all require imports
inferrinizzard Apr 12, 2024
c492962
fix all imports
inferrinizzard Apr 12, 2024
a47609a
reduce excess style rules
inferrinizzard Apr 12, 2024
b536136
fix any types
inferrinizzard Apr 13, 2024
f0e7615
fix misc errors
inferrinizzard Apr 13, 2024
c0232f3
downgrade remaining to warnings
inferrinizzard Apr 13, 2024
59e877f
return types
inferrinizzard Apr 13, 2024
e5edac4
fix types errors
inferrinizzard Apr 13, 2024
0267ad0
Merge branch 'chore/repo/eslint' into chore/repo/eslint-fix
inferrinizzard Apr 13, 2024
130fe31
fix json import for test tsconfig
inferrinizzard Apr 13, 2024
37812f3
Merge branch 'master' into chore/repo/eslint
inferrinizzard Apr 14, 2024
e89f453
Merge branch 'chore/repo/eslint' into chore/repo/eslint-fix
inferrinizzard Apr 14, 2024
06002f9
auto lint fix
inferrinizzard Apr 14, 2024
93ab86e
fix lint errors in extension
inferrinizzard Apr 14, 2024
6a81618
Merge branch 'master' into chore/repo/eslint
inferrinizzard Apr 14, 2024
6ca4eaa
Merge branch 'chore/repo/eslint' into chore/repo/eslint-fix
inferrinizzard Apr 14, 2024
1ec5933
fix lint errors in Elixir
inferrinizzard Apr 14, 2024
4620ba5
make ref.pushElement public
inferrinizzard Apr 14, 2024
4fe1649
fix misc
inferrinizzard Apr 14, 2024
eed53c6
fix accidental public in CSharp raw text get
inferrinizzard Apr 14, 2024
a4989bf
Merge branch 'master' into chore/repo/eslint-fix
inferrinizzard Apr 30, 2024
8e6fc4f
fix new lint errors
inferrinizzard Apr 30, 2024
58cfc88
Merge branch 'chore/repo/eslint-fix' into base
inferrinizzard May 5, 2024
f16e6ad
Merge branch 'refactor/imports/languages'
inferrinizzard May 5, 2024
6f05453
strongly type Rust naming styles
inferrinizzard May 10, 2024
70c9397
fix typo
inferrinizzard May 10, 2024
b925782
unify emitDeriveHeader for Rust
inferrinizzard May 10, 2024
130265c
Merge branch 'master' into chore/rust/type-naming-styles
inferrinizzard May 18, 2024
9d92a04
:recycle:
inferrinizzard May 18, 2024
aa2cb4f
Merge branch 'master' into chore/rust/type-naming-styles
inferrinizzard May 18, 2024
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
2 changes: 1 addition & 1 deletion packages/quicktype-core/src/language/CJSON/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { type RenderContext } from "../../Renderer";
import { EnumOption, type Option, StringOption, getOptionValues } from "../../RendererOptions";
import { type NamingStyle } from "../../support/Strings";
import { TargetLanguage } from "../../TargetLanguage";
import { type FixMeOptionsType, type FixMeOptionsAnyType } from "../../types";
import { type FixMeOptionsAnyType, type FixMeOptionsType } from "../../types";

import { CJSONRenderer } from "./CJSONRenderer";

Expand Down
15 changes: 8 additions & 7 deletions packages/quicktype-core/src/language/Rust/RustRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import { keywords } from "./constants";
import { type rustOptions } from "./language";
import {
Density,
type NamingStyleKey,
Visibility,
camelNamingFunction,
getPreferedNamingStyle,
getPreferredNamingStyle,
listMatchingNamingStyles,
nameToNamingStyle,
nameWithNamingStyle,
namingStyles,
rustStringEscape,
snakeNamingFunction
Expand Down Expand Up @@ -121,12 +122,12 @@ export class RustRenderer extends ConvenienceRenderer {
private emitRenameAttribute(
propName: Name,
jsonName: string,
defaultNamingStyle: string,
preferedNamingStyle: string
defaultNamingStyle: NamingStyleKey,
preferedNamingStyle: NamingStyleKey
): void {
const escapedName = rustStringEscape(jsonName);
const name = namingStyles[defaultNamingStyle].fromParts(this.sourcelikeToString(propName).split(" "));
const styledName = nameToNamingStyle(name, preferedNamingStyle);
const styledName = nameWithNamingStyle(name, preferedNamingStyle);
const namesDiffer = escapedName !== styledName;
if (namesDiffer) {
this.emitLine('#[serde(rename = "', escapedName, '")]');
Expand Down Expand Up @@ -168,7 +169,7 @@ export class RustRenderer extends ConvenienceRenderer {

// Set the default naming style on the struct
const defaultStyle = "snake_case";
const preferedNamingStyle = getPreferedNamingStyle(Object.values(propertiesNamingStyles).flat(), defaultStyle);
const preferedNamingStyle = getPreferredNamingStyle(Object.values(propertiesNamingStyles).flat(), defaultStyle);
if (preferedNamingStyle !== defaultStyle) {
this.emitLine(`#[serde(rename_all = "${preferedNamingStyle}")]`);
}
Expand Down Expand Up @@ -240,7 +241,7 @@ export class RustRenderer extends ConvenienceRenderer {

// Set the default naming style on the enum
const defaultStyle = "PascalCase";
const preferedNamingStyle = getPreferedNamingStyle(Object.values(enumCasesNamingStyles).flat(), defaultStyle);
const preferedNamingStyle = getPreferredNamingStyle(Object.values(enumCasesNamingStyles).flat(), defaultStyle);
if (preferedNamingStyle !== defaultStyle) {
this.emitLine(`#[serde(rename_all = "${preferedNamingStyle}")]`);
}
Expand Down
26 changes: 15 additions & 11 deletions packages/quicktype-core/src/language/Rust/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface NamingStyle {
toParts: NameToParts;
}

export const namingStyles: Record<string, NamingStyle> = {
export const namingStyles = {
snake_case: {
regex: /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/,
toParts: (name: string): string[] => name.split("_"),
Expand Down Expand Up @@ -80,7 +80,11 @@ export const namingStyles: Record<string, NamingStyle> = {
toParts: (name: string): string[] => [name],
fromParts: (parts: string[]): string => parts.map(p => p.toUpperCase()).join("")
}
};
} as const;

namingStyles satisfies Record<string, NamingStyle>;

export type NamingStyleKey = keyof typeof namingStyles;

const isAsciiLetterOrUnderscoreOrDigit = (codePoint: number): boolean => {
if (!isAscii(codePoint)) {
Expand Down Expand Up @@ -132,27 +136,27 @@ const standardUnicodeRustEscape = (codePoint: number): string => {

export const rustStringEscape = utf32ConcatMap(escapeNonPrintableMapper(isPrintable, standardUnicodeRustEscape));

export function getPreferedNamingStyle(namingStyleOccurences: string[], defaultStyle: string): string {
export function getPreferredNamingStyle(namingStyleOccurences: string[], defaultStyle: NamingStyleKey): NamingStyleKey {
const occurrences = Object.fromEntries(Object.keys(namingStyles).map(key => [key, 0]));
namingStyleOccurences.forEach(style => ++occurrences[style]);
const max = Math.max(...Object.values(occurrences));
const preferedStyles = Object.entries(occurrences)
.filter(([_style, num]) => num === max)
.map(([style, _num]) => style);
const preferedStyles = Object.entries(occurrences).flatMap(([style, num]) =>
num === max ? [style] : []
) as NamingStyleKey[];
if (preferedStyles.includes(defaultStyle)) {
return defaultStyle;
}

return preferedStyles[0];
}

export function listMatchingNamingStyles(name: string): string[] {
return Object.entries(namingStyles)
.filter(([_, { regex }]) => regex.test(name))
.map(([namingStyle, _]) => namingStyle);
export function listMatchingNamingStyles(name: string): NamingStyleKey[] {
return Object.entries(namingStyles).flatMap(([namingStyleKey, { regex }]) =>
regex.test(name) ? [namingStyleKey] : []
) as NamingStyleKey[];
}

export function nameToNamingStyle(name: string, style: string): string {
export function nameWithNamingStyle(name: string, style: NamingStyleKey): string {
if (namingStyles[style].regex.test(name)) {
return name;
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"outDir": "dist",
"baseUrl": "src"
"baseUrl": "src",
"resolveJsonModule": true
},
"include": ["src"]
}
Loading