Skip to content

Commit 25cad09

Browse files
stephentoubCopilot
andcommitted
codegen: emit Rust type aliases for scalar RPC result schemas
The Rust codegen only emitted a named type for result schemas that were an enum, array, map, or object. A method whose result is an inline primitive (e.g. {"type":"integer"}) produced a reference to a type that was never defined, so the generated crate failed to compile with E0425. No RPC method had a primitive result until session.cancelAllBackgroundAgents was added, so this latent gap only surfaces on a schema update. Add rustScalarType()/emitRustScalarAlias() so scalar results emit a plain type alias alongside the existing shapes. This is a no-op against the current pinned schema and prevents the next dependency bump from hitting the same build break. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: e615d062-bcb7-431e-aa9c-d3e47405723a
1 parent 6563591 commit 25cad09

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

scripts/codegen/rust.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,45 @@ function emitRustMapAlias(
575575
);
576576
}
577577

578+
/**
579+
* Map a primitive JSON Schema type to its Rust equivalent, or `undefined` when
580+
* the schema is not a plain scalar. Mirrors the primitive branches of
581+
* {@link resolveRustType}.
582+
*/
583+
function rustScalarType(schema: JSONSchema7): string | undefined {
584+
if (schema.enum || schema.const !== undefined) return undefined;
585+
switch (schema.type) {
586+
case "string":
587+
return "String";
588+
case "number":
589+
return "f64";
590+
case "integer":
591+
return isIntegerSchemaBoundedToInt32(schema) ? "i32" : "i64";
592+
case "boolean":
593+
return "bool";
594+
default:
595+
return undefined;
596+
}
597+
}
598+
599+
/**
600+
* Emit a type alias for a named schema that resolves to a primitive scalar
601+
* (e.g. an RPC result declared as `{ "type": "integer" }`). Without this the
602+
* generated RPC surface would reference a `*Result` type that was never
603+
* defined.
604+
*/
605+
function emitRustScalarAlias(
606+
typeName: string,
607+
schema: JSONSchema7,
608+
ctx: RustCodegenCtx,
609+
description?: string,
610+
): void {
611+
if (ctx.generatedNames.has(typeName)) return;
612+
const scalarType = rustScalarType(schema);
613+
if (!scalarType) return;
614+
emitRustTypeAlias(typeName, schema, scalarType, ctx, description);
615+
}
616+
578617
function rustRpcResultDescription(
579618
method: RpcMethod,
580619
resultSchema: JSONSchema7 | undefined,
@@ -1527,6 +1566,8 @@ function generateApiTypesCode(
15271566
} else {
15281567
tryEmitRustUnion(schema, name, "", ctx);
15291568
}
1569+
} else {
1570+
emitRustScalarAlias(name, schema, ctx, schema.description);
15301571
}
15311572
}
15321573

@@ -1576,6 +1617,8 @@ function generateApiTypesCode(
15761617
emitRustMapAlias(resultName, resolved, ctx, resolved.description);
15771618
} else if (isObjectSchema(resolved)) {
15781619
emitRustStruct(resultName, resolved, ctx, resolved.description);
1620+
} else {
1621+
emitRustScalarAlias(resultName, resolved, ctx, resolved.description);
15791622
}
15801623
}
15811624
}

0 commit comments

Comments
 (0)