Skip to content

Commit 891ed01

Browse files
chore(rust): clean up unused imports (#9858)
* chore(rust): clean up unused imports * seed update * bump version
1 parent b00bad9 commit 891ed01

File tree

298 files changed

+13
-846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

298 files changed

+13
-846
lines changed

generators/rust/dynamic-snippets/src/EndpointSnippetGenerator.ts

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class EndpointSnippetGenerator {
5151
snippet: FernIr.dynamic.EndpointSnippetRequest;
5252
}): string[] {
5353
// Get use statements
54-
const useStatements = this.getUseStatements({ endpoint, snippet });
54+
const useStatements = this.getUseStatements();
5555

5656
// Create the main function body
5757
const mainBody = rust.CodeBlock.fromStatements([
@@ -99,52 +99,9 @@ export class EndpointSnippetGenerator {
9999
return components;
100100
}
101101

102-
private getUseStatements({
103-
endpoint,
104-
snippet
105-
}: {
106-
endpoint: FernIr.dynamic.Endpoint;
107-
snippet: FernIr.dynamic.EndpointSnippetRequest;
108-
}): rust.UseStatement[] {
109-
const stdImports = new Set<string>();
110-
const chronoImports = new Set<string>();
111-
const uuidImports = new Set<string>();
112-
113-
// Collect types used in snippet values that require std/chrono/uuid imports
114-
this.collectSnippetTypeImports(snippet, new Set<string>(), stdImports, chronoImports, uuidImports);
115-
102+
private getUseStatements(): rust.UseStatement[] {
116103
const useStatements: rust.UseStatement[] = [];
117104

118-
// Add standard library imports if needed
119-
if (stdImports.size > 0) {
120-
useStatements.push(
121-
new rust.UseStatement({
122-
path: "std::collections",
123-
items: Array.from(stdImports)
124-
})
125-
);
126-
}
127-
128-
// Add chrono imports if needed
129-
if (chronoImports.size > 0) {
130-
useStatements.push(
131-
new rust.UseStatement({
132-
path: "chrono",
133-
items: Array.from(chronoImports)
134-
})
135-
);
136-
}
137-
138-
// Add UUID imports if needed
139-
if (uuidImports.size > 0) {
140-
useStatements.push(
141-
new rust.UseStatement({
142-
path: "uuid",
143-
items: Array.from(uuidImports)
144-
})
145-
);
146-
}
147-
148105
// Use prelude import for all crate types
149106
useStatements.push(
150107
new rust.UseStatement({
@@ -156,34 +113,6 @@ export class EndpointSnippetGenerator {
156113
return useStatements;
157114
}
158115

159-
// New method to collect types from snippet values
160-
private collectSnippetTypeImports(
161-
snippet: FernIr.dynamic.EndpointSnippetRequest,
162-
imports: Set<string>,
163-
stdImports: Set<string>,
164-
chronoImports: Set<string>,
165-
uuidImports: Set<string>
166-
): void {
167-
// Collect types from request body if present
168-
if (snippet.requestBody != null) {
169-
this.collectTypesFromValue(snippet.requestBody, imports, stdImports, chronoImports, uuidImports);
170-
}
171-
172-
// Collect types from query parameters
173-
if (snippet.queryParameters != null) {
174-
Object.values(snippet.queryParameters).forEach((value) => {
175-
this.collectTypesFromValue(value, imports, stdImports, chronoImports, uuidImports);
176-
});
177-
}
178-
179-
// Collect types from headers
180-
if (snippet.headers != null) {
181-
Object.values(snippet.headers).forEach((value) => {
182-
this.collectTypesFromValue(value, imports, stdImports, chronoImports, uuidImports);
183-
});
184-
}
185-
}
186-
187116
// Helper to collect type imports from a value by analyzing its structure
188117
private collectTypesFromValue(
189118
value: unknown,
Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RelativeFilePath } from "@fern-api/fs-utils";
22
import { RustFile } from "@fern-api/rust-base";
33
import { Attribute, PUBLIC, rust } from "@fern-api/rust-codegen";
4-
import { AliasTypeDeclaration, TypeDeclaration, TypeReference } from "@fern-fern/ir-sdk/api";
4+
import { AliasTypeDeclaration, TypeDeclaration } from "@fern-fern/ir-sdk/api";
55
import { generateRustTypeForTypeReference } from "../converters";
66
import { ModelGeneratorContext } from "../ModelGeneratorContext";
7-
import { isChronoType, isCollectionType, isUuidType, typeSupportsHashAndEq } from "../utils/primitiveTypeUtils";
7+
import { typeSupportsHashAndEq } from "../utils/primitiveTypeUtils";
88

99
export class AliasGenerator {
1010
private readonly typeDeclaration: TypeDeclaration;
@@ -52,45 +52,6 @@ export class AliasGenerator {
5252
return writer.toString();
5353
}
5454

55-
private writeUseStatements(writer: rust.Writer): void {
56-
writer.writeLine("use serde::{Deserialize, Serialize};");
57-
58-
// Add additional use statements based on the inner type
59-
this.writeAdditionalUseStatements(writer);
60-
}
61-
62-
private writeAdditionalUseStatements(writer: rust.Writer): void {
63-
const innerType = this.aliasTypeDeclaration.aliasOf;
64-
65-
// Add imports for custom named types FIRST
66-
const customTypes = this.getCustomTypesUsedInAlias();
67-
customTypes.forEach((typeName) => {
68-
const modulePath = this.context.getModulePathForType(typeName.snakeCase.unsafeName);
69-
const moduleNameEscaped = this.context.escapeRustKeyword(modulePath);
70-
writer.writeLine(`use crate::${moduleNameEscaped}::${typeName.pascalCase.unsafeName};`);
71-
});
72-
73-
// Add chrono if aliasing a datetime
74-
if (isChronoType(innerType)) {
75-
writer.writeLine("use chrono::{DateTime, Utc};");
76-
}
77-
78-
// Add uuid if aliasing a UUID
79-
if (isUuidType(innerType)) {
80-
writer.writeLine("use uuid::Uuid;");
81-
}
82-
83-
// Add collections if aliasing a map or set
84-
if (isCollectionType(innerType)) {
85-
writer.writeLine("use std::collections::HashMap;");
86-
}
87-
88-
// TODO: @iamnamananand996 build to use serde_json::Value ---> Value directly
89-
// if (hasJsonValueFields(properties)) {
90-
// writer.writeLine("use serde_json::Value;");
91-
// }
92-
}
93-
9455
private generateNewtypeForTypeDeclaration(): rust.NewtypeStruct {
9556
return rust.newtypeStruct({
9657
name: this.context.getUniqueTypeNameForDeclaration(this.typeDeclaration),
@@ -124,50 +85,4 @@ export class AliasGenerator {
12485
// Check if the aliased type can support Hash and Eq derives
12586
return typeSupportsHashAndEq(this.aliasTypeDeclaration.aliasOf, this.context);
12687
}
127-
128-
private getCustomTypesUsedInAlias(): {
129-
snakeCase: { unsafeName: string };
130-
pascalCase: { unsafeName: string };
131-
}[] {
132-
const customTypeNames: {
133-
snakeCase: { unsafeName: string };
134-
pascalCase: { unsafeName: string };
135-
}[] = [];
136-
const visited = new Set<string>();
137-
138-
const extractNamedTypesRecursively = (typeRef: TypeReference) => {
139-
if (typeRef.type === "named") {
140-
const typeName = typeRef.name.originalName;
141-
if (!visited.has(typeName)) {
142-
visited.add(typeName);
143-
customTypeNames.push({
144-
snakeCase: { unsafeName: typeRef.name.snakeCase.unsafeName },
145-
pascalCase: { unsafeName: typeRef.name.pascalCase.unsafeName }
146-
});
147-
}
148-
} else if (typeRef.type === "container") {
149-
typeRef.container._visit({
150-
list: (listType: TypeReference) => extractNamedTypesRecursively(listType),
151-
set: (setType: TypeReference) => extractNamedTypesRecursively(setType),
152-
optional: (optionalType: TypeReference) => extractNamedTypesRecursively(optionalType),
153-
nullable: (nullableType: TypeReference) => extractNamedTypesRecursively(nullableType),
154-
map: (mapType) => {
155-
extractNamedTypesRecursively(mapType.keyType);
156-
extractNamedTypesRecursively(mapType.valueType);
157-
},
158-
literal: () => {
159-
// No named types in literals
160-
},
161-
_other: () => {
162-
// Unknown container type
163-
}
164-
});
165-
}
166-
};
167-
168-
// Analyze the aliased type
169-
extractNamedTypesRecursively(this.aliasTypeDeclaration.aliasOf);
170-
171-
return customTypeNames;
172-
}
17388
}

generators/rust/model/src/enum/EnumGenerator.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ export class EnumGenerator {
5454
return writer.toString();
5555
}
5656

57-
private writeUseStatements(writer: rust.Writer): void {
58-
writer.writeLine("use serde::{Deserialize, Serialize};");
59-
writer.writeLine("use std::fmt;");
60-
}
61-
6257
private generateEnumForTypeDeclaration(): rust.Enum {
6358
return rust.enum_({
6459
name: this.context.getUniqueTypeNameForDeclaration(this.typeDeclaration),

generators/rust/model/src/object/StructGenerator.ts

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import { RelativeFilePath } from "@fern-api/fs-utils";
22
import { RustFile } from "@fern-api/rust-base";
33
import { Attribute, PUBLIC, rust } from "@fern-api/rust-codegen";
4-
54
import { ObjectProperty, ObjectTypeDeclaration, TypeDeclaration } from "@fern-fern/ir-sdk/api";
6-
75
import { ModelGeneratorContext } from "../ModelGeneratorContext";
86
import { namedTypeSupportsHashAndEq, namedTypeSupportsPartialEq } from "../utils/primitiveTypeUtils";
97
import { isFieldRecursive } from "../utils/recursiveTypeUtils";
108
import {
119
canDeriveHashAndEq,
1210
canDerivePartialEq,
1311
generateFieldAttributes,
14-
generateFieldType,
15-
getCustomTypesUsedInFields,
16-
hasBigIntFields,
17-
hasDateFields,
18-
hasDateTimeOnlyFields,
19-
hasFloatingPointSets,
20-
hasHashMapFields,
21-
hasHashSetFields,
22-
hasUuidFields
12+
generateFieldType
2313
} from "../utils/structUtils";
2414

2515
export class StructGenerator {
@@ -68,81 +58,6 @@ export class StructGenerator {
6858
return writer.toString();
6959
}
7060

71-
private writeUseStatements(writer: rust.Writer): void {
72-
// Add imports for custom named types referenced in fields FIRST
73-
const customTypes = getCustomTypesUsedInFields(
74-
this.objectTypeDeclaration.properties,
75-
this.typeDeclaration.name.name.pascalCase.unsafeName
76-
);
77-
customTypes.forEach((typeName) => {
78-
const modulePath = this.context.getModulePathForType(typeName.snakeCase.unsafeName);
79-
const moduleNameEscaped = this.context.escapeRustKeyword(modulePath);
80-
writer.writeLine(`use crate::${moduleNameEscaped}::${typeName.pascalCase.unsafeName};`);
81-
});
82-
83-
// Add imports for parent types
84-
if (this.objectTypeDeclaration.extends.length > 0) {
85-
this.objectTypeDeclaration.extends.forEach((parentType) => {
86-
// Use getUniqueTypeNameForReference to get the correct type name with fernFilepath prefix
87-
const parentTypeName = this.context.getUniqueTypeNameForReference(parentType);
88-
const modulePath = this.context.getModulePathForType(parentType.name.snakeCase.unsafeName);
89-
const moduleNameEscaped = this.context.escapeRustKeyword(modulePath);
90-
writer.writeLine(`use crate::${moduleNameEscaped}::${parentTypeName};`);
91-
});
92-
}
93-
94-
// Add chrono imports based on specific types needed
95-
const hasDateOnly = hasDateFields(this.objectTypeDeclaration.properties);
96-
const hasDateTimeOnly = hasDateTimeOnlyFields(this.objectTypeDeclaration.properties);
97-
98-
// TODO: @iamnamananand996 - use AST mechanism for all imports
99-
if (hasDateOnly && hasDateTimeOnly) {
100-
// Both date and datetime types present
101-
writer.writeLine("use chrono::{DateTime, NaiveDate, Utc};");
102-
} else if (hasDateOnly) {
103-
// Only date type present, import NaiveDate only
104-
writer.writeLine("use chrono::NaiveDate;");
105-
} else if (hasDateTimeOnly) {
106-
// Only datetime type present, import DateTime and Utc only
107-
writer.writeLine("use chrono::{DateTime, Utc};");
108-
}
109-
110-
// Add std::collections imports based on specific collection types used
111-
const needsHashMap = hasHashMapFields(this.objectTypeDeclaration.properties);
112-
const needsHashSet = hasHashSetFields(this.objectTypeDeclaration.properties);
113-
114-
if (needsHashMap && needsHashSet) {
115-
writer.writeLine("use std::collections::{HashMap, HashSet};");
116-
} else if (needsHashMap) {
117-
writer.writeLine("use std::collections::HashMap;");
118-
} else if (needsHashSet) {
119-
writer.writeLine("use std::collections::HashSet;");
120-
}
121-
122-
// Add ordered_float if we have floating-point sets
123-
if (hasFloatingPointSets(this.objectTypeDeclaration.properties)) {
124-
writer.writeLine("use ordered_float::OrderedFloat;");
125-
}
126-
127-
// Add uuid if we have UUID fields
128-
if (hasUuidFields(this.objectTypeDeclaration.properties)) {
129-
writer.writeLine("use uuid::Uuid;");
130-
}
131-
132-
// Add num_bigint if we have BigInt fields
133-
if (hasBigIntFields(this.objectTypeDeclaration.properties)) {
134-
writer.writeLine("use num_bigint::BigInt;");
135-
}
136-
137-
// TODO: @iamnamananand996 build to use serde_json::Value ---> Value directly
138-
// if (hasJsonValueFields(properties)) {
139-
// writer.writeLine("use serde_json::Value;");
140-
// }
141-
142-
// Add serde imports LAST
143-
writer.writeLine("use serde::{Deserialize, Serialize};");
144-
}
145-
14661
private generateStructForTypeDeclaration(): rust.Struct {
14762
const fields: rust.Field[] = [];
14863

0 commit comments

Comments
 (0)