typespec-rust-emitter 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/emitter.ts CHANGED
@@ -21,6 +21,8 @@ import {
21
21
  StringLiteral,
22
22
  DecoratorContext,
23
23
  getNamespaceFullName,
24
+ Operation,
25
+ getTags,
24
26
  } from "@typespec/compiler";
25
27
 
26
28
  export interface RustEmitterOptions {
@@ -31,34 +33,45 @@ interface RustDeriveInfo {
31
33
  derives: string[];
32
34
  }
33
35
 
36
+ interface RustAttrInfo {
37
+ attrs: string[];
38
+ }
39
+
34
40
  const rustDeriveKey = Symbol("rustDerive");
41
+ const rustAttrKey = Symbol("rustAttr");
35
42
 
36
43
  export function $rustDerive(
37
44
  context: DecoratorContext,
38
45
  target: Type,
39
46
  derive: string,
40
47
  ) {
41
- if (target.kind !== "Model") {
48
+ if (target.kind !== "Model" && target.kind !== "Enum") {
42
49
  context.program.reportDiagnostic({
43
50
  code: "rust-derive-invalid-target",
44
- message: `@rustDerive can only be applied to models`,
51
+ message: `@rustDerive can only be applied to models and enums`,
45
52
  severity: "error",
46
53
  target: context.decoratorTarget,
47
54
  });
48
55
  return;
49
56
  }
50
57
 
51
- const model = target as Model;
52
- const ns = model.namespace ? getNamespaceFullName(model.namespace) : "";
58
+ const ns =
59
+ target.kind === "Model"
60
+ ? target.namespace
61
+ ? getNamespaceFullName(target.namespace)
62
+ : ""
63
+ : target.namespace
64
+ ? getNamespaceFullName(target.namespace)
65
+ : "";
53
66
 
54
67
  if (!ns.startsWith("TypeSpec")) {
55
- const info = (model as any)[rustDeriveKey] as RustDeriveInfo | undefined;
68
+ const info = (target as any)[rustDeriveKey] as RustDeriveInfo | undefined;
56
69
  if (info) {
57
70
  if (!info.derives.includes(derive)) {
58
71
  info.derives.push(derive);
59
72
  }
60
73
  } else {
61
- (model as any)[rustDeriveKey] = { derives: [derive] };
74
+ (target as any)[rustDeriveKey] = { derives: [derive] };
62
75
  }
63
76
  }
64
77
  }
@@ -73,6 +86,762 @@ export function $rustDerives(
73
86
  }
74
87
  }
75
88
 
89
+ export function $rustAttr(
90
+ context: DecoratorContext,
91
+ target: Type,
92
+ attr: string,
93
+ ) {
94
+ if (target.kind !== "Model" && target.kind !== "Enum") {
95
+ context.program.reportDiagnostic({
96
+ code: "rust-attr-invalid-target",
97
+ message: `@rustAttr can only be applied to models and enums`,
98
+ severity: "error",
99
+ target: context.decoratorTarget,
100
+ });
101
+ return;
102
+ }
103
+
104
+ const ns =
105
+ target.kind === "Model"
106
+ ? target.namespace
107
+ ? getNamespaceFullName(target.namespace)
108
+ : ""
109
+ : target.namespace
110
+ ? getNamespaceFullName(target.namespace)
111
+ : "";
112
+
113
+ if (!ns.startsWith("TypeSpec")) {
114
+ const info = (target as any)[rustAttrKey] as RustAttrInfo | undefined;
115
+ if (info) {
116
+ if (!info.attrs.includes(attr)) {
117
+ info.attrs.push(attr);
118
+ }
119
+ } else {
120
+ (target as any)[rustAttrKey] = { attrs: [attr] };
121
+ }
122
+ }
123
+ }
124
+
125
+ export function $rustAttrs(
126
+ context: DecoratorContext,
127
+ target: Type,
128
+ ...attrs: string[]
129
+ ) {
130
+ for (const attr of attrs) {
131
+ $rustAttr(context, target, attr);
132
+ }
133
+ }
134
+
135
+ type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
136
+
137
+ interface OperationInfo {
138
+ name: string;
139
+ method: HttpMethod;
140
+ path: string;
141
+ tags: string[];
142
+ parameters: ParameterInfo[];
143
+ body: ModelProperty | undefined;
144
+ responses: ResponseInfo[];
145
+ doc: string | undefined;
146
+ }
147
+
148
+ interface ParameterInfo {
149
+ name: string;
150
+ rustName: string;
151
+ rustType: string;
152
+ location: "path" | "query" | "header" | "cookie";
153
+ required: boolean;
154
+ optional?: boolean;
155
+ }
156
+
157
+ interface ResponseInfo {
158
+ statusCode: number;
159
+ bodyType: string | undefined;
160
+ bodyDescription: string | undefined;
161
+ }
162
+
163
+ function getDecoratorName(decorator: any): string {
164
+ if (!decorator) return "";
165
+ if (typeof decorator !== "object") return "";
166
+
167
+ if (decorator.node?.target?.sv) {
168
+ return decorator.node.target.sv;
169
+ }
170
+ if (
171
+ decorator.node?.target?.kind === "Identifier" &&
172
+ decorator.node?.target?.sv
173
+ ) {
174
+ return decorator.node.target.sv;
175
+ }
176
+
177
+ return "";
178
+ }
179
+
180
+ function getDecoratorArgValue(decorator: any, index: number = 0): string {
181
+ if (!decorator) return "";
182
+ const args = decorator.args || decorator.arguments;
183
+ if (!args) return "";
184
+ const arg = args[index];
185
+ if (arg?.jsValue !== undefined) return String(arg.jsValue);
186
+ if (arg?.value !== undefined) return String(arg.value);
187
+ return "";
188
+ }
189
+
190
+ function getHttpMethod(
191
+ _program: Program,
192
+ operation: Operation,
193
+ ): HttpMethod | undefined {
194
+ const decorators = (operation as any).decorators;
195
+ if (!decorators) return undefined;
196
+
197
+ for (const key of Object.keys(decorators)) {
198
+ const decorator = decorators[key];
199
+ const name = getDecoratorName(decorator);
200
+ if (name === "get") return "GET";
201
+ if (name === "post") return "POST";
202
+ if (name === "put") return "PUT";
203
+ if (name === "patch") return "PATCH";
204
+ if (name === "delete") return "DELETE";
205
+ if (name === "head") return "HEAD";
206
+ }
207
+ return undefined;
208
+ }
209
+
210
+ function getRoute(_program: Program, target: Namespace | Operation): string {
211
+ const decorators = (target as any).decorators;
212
+ if (!decorators) return "";
213
+
214
+ for (const key of Object.keys(decorators)) {
215
+ const decorator = decorators[key];
216
+ const name = getDecoratorName(decorator);
217
+ if (name === "route") {
218
+ return getDecoratorArgValue(decorator, 0);
219
+ }
220
+ }
221
+ return "";
222
+ }
223
+
224
+ function hasAuthDecorator(operation: Operation): boolean {
225
+ const decorators = (operation as any).decorators;
226
+ if (!decorators) return false;
227
+
228
+ for (const key of Object.keys(decorators)) {
229
+ const decorator = decorators[key];
230
+ const name = getDecoratorName(decorator);
231
+ if (name === "useAuth") {
232
+ return true;
233
+ }
234
+ }
235
+ return false;
236
+ }
237
+
238
+ function getOperationParameters(
239
+ program: Program,
240
+ operation: Operation,
241
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
242
+ ): ParameterInfo[] {
243
+ const params: ParameterInfo[] = [];
244
+ const model = operation.parameters;
245
+
246
+ for (const [propName, prop] of model.properties) {
247
+ const decorators = (prop as any).decorators;
248
+ let location: "path" | "query" | "header" | "cookie" = "query";
249
+ let rustName = toRustIdent(propName);
250
+
251
+ if (decorators) {
252
+ if (decorators["$path"]) {
253
+ location = "path";
254
+ } else if (decorators["$query"]) {
255
+ location = "query";
256
+ } else if (decorators["$header"]) {
257
+ location = "header";
258
+ const headerDec = decorators["$header"];
259
+ if (headerDec?.value) {
260
+ rustName = headerDec.value;
261
+ }
262
+ } else if (decorators["$cookie"]) {
263
+ location = "cookie";
264
+ }
265
+ }
266
+
267
+ const { type: rustType } = getRustTypeForProperty(
268
+ prop.type,
269
+ program,
270
+ anonymousEnums,
271
+ );
272
+
273
+ params.push({
274
+ name: propName,
275
+ rustName,
276
+ rustType,
277
+ location,
278
+ required: !prop.optional,
279
+ optional: prop.optional,
280
+ });
281
+ }
282
+
283
+ return params;
284
+ }
285
+
286
+ function getOperationBody(operation: Operation): ModelProperty | undefined {
287
+ for (const [_propName, prop] of operation.parameters.properties) {
288
+ const decorators = (prop as any).decorators;
289
+ if (decorators?.["$body"] || decorators?.["$bodyRoot"]) {
290
+ return prop;
291
+ }
292
+ }
293
+ return undefined;
294
+ }
295
+
296
+ function getOperationResponses(
297
+ program: Program,
298
+ operation: Operation,
299
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
300
+ ): ResponseInfo[] {
301
+ const responses: ResponseInfo[] = [];
302
+ const returnType = operation.returnType;
303
+
304
+ if (returnType.kind === "Union") {
305
+ const union = returnType as Union;
306
+ for (const variant of union.variants.values()) {
307
+ const statusCode = getStatusCode(variant);
308
+ const bodyInfo = getBodyFromResponse(variant, program, anonymousEnums);
309
+ responses.push({
310
+ statusCode,
311
+ bodyType: bodyInfo.type,
312
+ bodyDescription: bodyInfo.description,
313
+ });
314
+ }
315
+ } else if (returnType.kind === "Model") {
316
+ const model = returnType as Model;
317
+ for (const [propName, prop] of model.properties) {
318
+ if (propName === "body") {
319
+ const { type: rustType } = getRustTypeForProperty(
320
+ prop.type,
321
+ program,
322
+ anonymousEnums,
323
+ );
324
+ responses.push({
325
+ statusCode: 200,
326
+ bodyType: rustType,
327
+ bodyDescription: getDoc(program, prop),
328
+ });
329
+ }
330
+ }
331
+ }
332
+
333
+ return responses;
334
+ }
335
+
336
+ function getStatusCode(variant: { type: Type }): number {
337
+ if (variant.type.kind === "Model") {
338
+ const model = variant.type as Model;
339
+ for (const [propName, prop] of model.properties) {
340
+ if (propName === "statusCode") {
341
+ const typeAny = prop.type as any;
342
+ if (typeAny.value !== undefined) {
343
+ return typeAny.value as number;
344
+ }
345
+ }
346
+ }
347
+ }
348
+ return 200;
349
+ }
350
+
351
+ function getBodyFromResponse(
352
+ variant: { type: Type },
353
+ program: Program,
354
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
355
+ ): { type: string | undefined; description: string | undefined } {
356
+ if (variant.type.kind === "Model") {
357
+ const model = variant.type as Model;
358
+ for (const [propName, prop] of model.properties) {
359
+ if (propName === "body") {
360
+ const { type: rustType } = getRustTypeForProperty(
361
+ prop.type,
362
+ program,
363
+ anonymousEnums,
364
+ );
365
+ return { type: rustType, description: getDoc(program, prop) };
366
+ }
367
+ }
368
+ }
369
+ return { type: undefined, description: undefined };
370
+ }
371
+
372
+ function getAllOperations(
373
+ program: Program,
374
+ ): { namespace: Namespace; operations: Operation[] }[] {
375
+ const seenNamespaces = new Set<string>();
376
+ const namespaceOps = new Map<string, { ns: Namespace; ops: Operation[] }>();
377
+
378
+ navigateProgram(program, {
379
+ namespace(ns: Namespace) {
380
+ const route = getRoute(program, ns);
381
+ if (!route) return;
382
+ if (!seenNamespaces.has(ns.name)) {
383
+ seenNamespaces.add(ns.name);
384
+ namespaceOps.set(ns.name, { ns, ops: [] });
385
+ }
386
+ },
387
+ operation(op: Operation) {
388
+ const method = getHttpMethod(program, op);
389
+ if (!method) return;
390
+ const ns = op.namespace;
391
+ if (!ns) return;
392
+ if (!seenNamespaces.has(ns.name)) {
393
+ seenNamespaces.add(ns.name);
394
+ namespaceOps.set(ns.name, { ns, ops: [] });
395
+ }
396
+ const entry = namespaceOps.get(ns.name);
397
+ if (entry) {
398
+ entry.ops.push(op);
399
+ }
400
+ },
401
+ });
402
+
403
+ const result: { namespace: Namespace; operations: Operation[] }[] = [];
404
+ for (const [, entry] of namespaceOps) {
405
+ if (entry.ops.length > 0) {
406
+ result.push({ namespace: entry.ns, operations: entry.ops });
407
+ }
408
+ }
409
+ return result;
410
+ }
411
+
412
+ function buildFullPath(namespaceRoute: string, operationRoute: string): string {
413
+ let fullPath = namespaceRoute + operationRoute;
414
+ fullPath = fullPath.replace(/\/+/g, "/");
415
+ if (fullPath.length > 1 && fullPath.endsWith("/")) {
416
+ fullPath = fullPath.slice(0, -1);
417
+ }
418
+ return fullPath;
419
+ }
420
+
421
+ function emitOperationInfo(
422
+ program: Program,
423
+ op: Operation,
424
+ nsRoute: string,
425
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
426
+ ): OperationInfo | undefined {
427
+ const method = getHttpMethod(program, op);
428
+ if (!method) return undefined;
429
+
430
+ const opRoute = getRoute(program, op);
431
+ const fullPath = buildFullPath(nsRoute, opRoute);
432
+ const tags = getTags(program, op) ?? [];
433
+ const params = getOperationParameters(program, op, anonymousEnums);
434
+ const body = getOperationBody(op);
435
+ const responses = getOperationResponses(program, op, anonymousEnums);
436
+ const doc = getDoc(program, op);
437
+
438
+ const opName = op.name.replace(/[^a-zA-Z0-9_]/g, "_");
439
+
440
+ return {
441
+ name: opName,
442
+ method,
443
+ path: fullPath,
444
+ tags,
445
+ parameters: params,
446
+ body: body,
447
+ responses,
448
+ doc,
449
+ };
450
+ }
451
+
452
+ function getStatusVariantName(statusCode: number): string {
453
+ const statusNames: Record<number, string> = {
454
+ 200: "Ok",
455
+ 201: "Created",
456
+ 202: "Accepted",
457
+ 204: "NoContent",
458
+ 400: "BadRequest",
459
+ 401: "Unauthorized",
460
+ 403: "Forbidden",
461
+ 404: "NotFound",
462
+ 409: "Conflict",
463
+ 500: "InternalServerError",
464
+ };
465
+ return statusNames[statusCode] || `Status${statusCode}`;
466
+ }
467
+
468
+ function getHttpStatusCode(statusCode: number): string {
469
+ const statusCodes: Record<number, string> = {
470
+ 200: "StatusCode::OK",
471
+ 201: "StatusCode::CREATED",
472
+ 202: "StatusCode::ACCEPTED",
473
+ 204: "StatusCode::NO_CONTENT",
474
+ 400: "StatusCode::BAD_REQUEST",
475
+ 401: "StatusCode::UNAUTHORIZED",
476
+ 403: "StatusCode::FORBIDDEN",
477
+ 404: "StatusCode::NOT_FOUND",
478
+ 409: "StatusCode::CONFLICT",
479
+ 500: "StatusCode::INTERNAL_SERVER_ERROR",
480
+ };
481
+ return statusCodes[statusCode] || `StatusCode::from_u16(${statusCode})`;
482
+ }
483
+
484
+ function generateServerTrait(
485
+ program: Program,
486
+ namespaceGroups: { namespace: Namespace; operations: Operation[] }[],
487
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
488
+ ): string {
489
+ const parts: string[] = [];
490
+
491
+ parts.push(`use super::types::*;
492
+ use async_trait::async_trait;
493
+ use axum::{http::StatusCode, Json};
494
+ use eyre::Result;
495
+
496
+ #[async_trait]
497
+ pub trait Server: Send + Sync {
498
+ type Claims: Send + Sync + 'static;
499
+
500
+ `);
501
+
502
+ for (const group of namespaceGroups) {
503
+ const nsName = toPascalCase(
504
+ group.namespace.name.replace(/[^a-zA-Z0-9_]/g, "_"),
505
+ );
506
+
507
+ for (const op of group.operations) {
508
+ const opInfo = emitOperationInfo(program, op, "", anonymousEnums);
509
+ if (!opInfo) continue;
510
+
511
+ if (opInfo.doc) {
512
+ parts.push(` ${formatDoc(opInfo.doc)}`);
513
+ }
514
+
515
+ const requestName = `${nsName}${toPascalCase(opInfo.name)}Request`;
516
+ const responseName = `${nsName}${toPascalCase(opInfo.name)}Response`;
517
+ const fnName = toRustIdent(`${nsName}_${opInfo.name}`);
518
+ const isProtected = hasAuthDecorator(op);
519
+
520
+ if (isProtected) {
521
+ parts.push(
522
+ ` async fn ${fnName}(&self, claims: Self::Claims, request: ${requestName}) -> Result<${responseName}>;`,
523
+ );
524
+ } else {
525
+ parts.push(
526
+ ` async fn ${fnName}(&self, request: ${requestName}) -> Result<${responseName}>;`,
527
+ );
528
+ }
529
+ }
530
+ }
531
+
532
+ parts.push("}");
533
+
534
+ return parts.join("\n");
535
+ }
536
+
537
+ function generateRequestStructs(
538
+ program: Program,
539
+ namespaceGroups: { namespace: Namespace; operations: Operation[] }[],
540
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
541
+ ): string {
542
+ const parts: string[] = [];
543
+
544
+ for (const group of namespaceGroups) {
545
+ const nsName = toPascalCase(
546
+ group.namespace.name.replace(/[^a-zA-Z0-9_]/g, "_"),
547
+ );
548
+
549
+ for (const op of group.operations) {
550
+ const opInfo = emitOperationInfo(program, op, "", anonymousEnums);
551
+ if (!opInfo) continue;
552
+
553
+ const params = opInfo.parameters;
554
+ const requestName = `${nsName}${toPascalCase(opInfo.name)}Request`;
555
+
556
+ const fields: string[] = [];
557
+
558
+ for (const param of params) {
559
+ const rustType = param.optional
560
+ ? `Option<${param.rustType}>`
561
+ : param.rustType;
562
+ fields.push(` #[serde(rename = "${param.name}", flatten)]`);
563
+ fields.push(` pub ${param.rustName}: ${rustType},`);
564
+ }
565
+
566
+ if (opInfo.body) {
567
+ const bodyType = getRustTypeForProperty(
568
+ opInfo.body.type,
569
+ program,
570
+ anonymousEnums,
571
+ );
572
+ fields.push(` #[serde(rename = "body")]`);
573
+ fields.push(` pub body: ${bodyType.type},`);
574
+ }
575
+
576
+ parts.push(`#[derive(Debug, Clone, serde::Deserialize)]
577
+ pub struct ${requestName} {
578
+ ${fields.join("\n")}
579
+ }
580
+ `);
581
+ }
582
+ }
583
+
584
+ return parts.join("\n");
585
+ }
586
+
587
+ function generateResponseEnums(
588
+ program: Program,
589
+ namespaceGroups: { namespace: Namespace; operations: Operation[] }[],
590
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
591
+ ): string {
592
+ const parts: string[] = [];
593
+
594
+ for (const group of namespaceGroups) {
595
+ const nsName = toPascalCase(
596
+ group.namespace.name.replace(/[^a-zA-Z0-9_]/g, "_"),
597
+ );
598
+
599
+ for (const op of group.operations) {
600
+ const opInfo = emitOperationInfo(program, op, "", anonymousEnums);
601
+ if (!opInfo) continue;
602
+
603
+ if (opInfo.responses.length === 0) continue;
604
+
605
+ const responseName = `${nsName}${toPascalCase(opInfo.name)}Response`;
606
+
607
+ const variants: string[] = [];
608
+ for (const resp of opInfo.responses) {
609
+ const variantName = getStatusVariantName(resp.statusCode);
610
+ if (!resp.bodyType) {
611
+ variants.push(` ${variantName},`);
612
+ } else {
613
+ variants.push(` ${variantName}(Json<${resp.bodyType}>),`);
614
+ }
615
+ }
616
+ parts.push(`pub enum ${responseName} {
617
+ ${variants.join("\n")}
618
+ }
619
+ `);
620
+
621
+ parts.push(`impl IntoResponse for ${responseName} {
622
+ fn into_response(self) -> axum::response::Response {
623
+ match self {
624
+ `);
625
+ for (const resp of opInfo.responses) {
626
+ const variantName = getStatusVariantName(resp.statusCode);
627
+ const statusCodeStr = getHttpStatusCode(resp.statusCode);
628
+ if (!resp.bodyType) {
629
+ parts.push(
630
+ ` ${responseName}::${variantName} => ${statusCodeStr}.into_response(),`,
631
+ );
632
+ } else {
633
+ parts.push(
634
+ ` ${responseName}::${variantName}(body) => (${statusCodeStr}, body).into_response(),`,
635
+ );
636
+ }
637
+ }
638
+ parts.push(` }
639
+ }
640
+ }
641
+ `);
642
+ }
643
+ }
644
+
645
+ return parts.join("\n");
646
+ }
647
+
648
+ function generateRouter(
649
+ program: Program,
650
+ namespaceGroups: { namespace: Namespace; operations: Operation[] }[],
651
+ anonymousEnums: Map<string, AnonymousStringLiteralUnion>,
652
+ ): string {
653
+ const handlers: string[] = [];
654
+ const publicRoutes: string[] = [];
655
+ const protectedRoutes: string[] = [];
656
+ const usedMethods = new Set<string>();
657
+
658
+ for (const group of namespaceGroups) {
659
+ const nsRoute = getRoute(program, group.namespace);
660
+ if (!nsRoute) continue;
661
+
662
+ const nsName = toPascalCase(
663
+ group.namespace.name.replace(/[^a-zA-Z0-9_]/g, "_"),
664
+ );
665
+
666
+ for (const op of group.operations) {
667
+ const opInfo = emitOperationInfo(program, op, nsRoute, anonymousEnums);
668
+ if (!opInfo) continue;
669
+
670
+ const method = opInfo.method.toLowerCase();
671
+ usedMethods.add(method);
672
+
673
+ const handlerFnName = toRustIdent(`${nsName}_${opInfo.name}`);
674
+ const traitFnName = handlerFnName;
675
+ const requestName = `${nsName}${toPascalCase(opInfo.name)}Request`;
676
+ const isProtected = hasAuthDecorator(op);
677
+
678
+ const pathParams = opInfo.parameters.filter((p) => p.location === "path");
679
+ const queryParams = opInfo.parameters.filter(
680
+ (p) => p.location === "query",
681
+ );
682
+ const hasPathParams = pathParams.length > 0;
683
+ const hasQueryParams = queryParams.length > 0;
684
+ const hasBody = !!opInfo.body;
685
+
686
+ // Build extractor lines and request construction expression
687
+ const extractorLines: string[] = [];
688
+ let requestExpr = "";
689
+
690
+ if (hasPathParams) {
691
+ const pathTypes = pathParams.map((p) => p.rustType).join(", ");
692
+ const pathFields = pathParams.map((p) => p.rustName).join(", ");
693
+ if (pathParams.length === 1) {
694
+ extractorLines.push(
695
+ ` axum::extract::Path(${pathFields}): axum::extract::Path<${pathTypes}>,`,
696
+ );
697
+ } else {
698
+ extractorLines.push(
699
+ ` axum::extract::Path((${pathFields})): axum::extract::Path<(${pathTypes})>,`,
700
+ );
701
+ }
702
+ }
703
+
704
+ if (hasQueryParams) {
705
+ extractorLines.push(
706
+ ` axum::extract::Query(query): axum::extract::Query<${requestName}>,`,
707
+ );
708
+ } else if (hasBody) {
709
+ extractorLines.push(
710
+ ` axum::Json(body): axum::Json<${requestName}Body>,`,
711
+ );
712
+ }
713
+
714
+ if (isProtected) {
715
+ extractorLines.push(
716
+ ` axum::Extension(claims): axum::Extension<S::Claims>,`,
717
+ );
718
+ }
719
+
720
+ // Build request struct expression
721
+ if (hasOnlyPathParams(hasPathParams, hasQueryParams, hasBody)) {
722
+ const pathAssignments = pathParams
723
+ .map((p) => `${p.rustName},`)
724
+ .join(" ");
725
+ requestExpr = `${requestName} { ${pathAssignments} }`;
726
+ } else if (hasQueryParams) {
727
+ if (hasPathParams) {
728
+ const pathAssignments = pathParams
729
+ .map((p) => `${p.rustName},`)
730
+ .join(" ");
731
+ requestExpr = `${requestName} { ${pathAssignments} ..query }`;
732
+ } else {
733
+ requestExpr = "query";
734
+ }
735
+ } else if (hasBody) {
736
+ if (hasPathParams) {
737
+ const pathAssignments = pathParams
738
+ .map((p) => `${p.rustName},`)
739
+ .join(" ");
740
+ requestExpr = `${requestName} { ${pathAssignments} body }`;
741
+ } else {
742
+ requestExpr = `${requestName} { body }`;
743
+ }
744
+ } else {
745
+ requestExpr = `${requestName} {}`;
746
+ }
747
+
748
+ // Server method call
749
+ const serverCall = isProtected
750
+ ? `service.${traitFnName}(claims, ${requestExpr}).await`
751
+ : `service.${traitFnName}(${requestExpr}).await`;
752
+
753
+ // All handlers use <S> generics, Claims is now an associated type
754
+ let handlerCode = `pub async fn ${handlerFnName}_handler<S>(
755
+ axum::extract::State(service): axum::extract::State<S>,
756
+ ${extractorLines.join("\n")}
757
+ ) -> impl axum::response::IntoResponse
758
+ where
759
+ S: Server + Clone + Send + Sync + 'static,
760
+ S::Claims: Send + Sync + Clone + 'static,
761
+ {
762
+ let result = ${serverCall};
763
+ match result {
764
+ Ok(response) => response.into_response(),
765
+ Err(e) => (
766
+ axum::http::StatusCode::INTERNAL_SERVER_ERROR,
767
+ format!("Internal error: {e}"),
768
+ )
769
+ .into_response(),
770
+ }
771
+ }`;
772
+
773
+ handlers.push(handlerCode);
774
+
775
+ let routePath = `"${opInfo.path}"`;
776
+ let routeStmt = "";
777
+ if (isProtected) {
778
+ routeStmt = `.route(${routePath}, ${method}(${handlerFnName}_handler::<S>))`;
779
+ protectedRoutes.push(routeStmt);
780
+ } else {
781
+ routeStmt = `.route(${routePath}, ${method}(${handlerFnName}_handler::<S>))`;
782
+ publicRoutes.push(routeStmt);
783
+ }
784
+ }
785
+ }
786
+
787
+ const methodImports = Array.from(usedMethods).sort().join(", ");
788
+
789
+ const routerBody = buildRouterBody(publicRoutes, protectedRoutes);
790
+
791
+ const parts: string[] = [];
792
+ parts.push(`use axum::response::IntoResponse;
793
+ use axum::routing::{${methodImports}};
794
+ use axum::Router;
795
+
796
+ `);
797
+ parts.push(handlers.join("\n\n"));
798
+ parts.push(`
799
+ pub fn create_router<S, M>(service: S, middleware: M) -> Router
800
+ where
801
+ S: Server + Clone + Send + Sync + 'static,
802
+ S::Claims: Send + Sync + Clone + 'static,
803
+ M: FnOnce(Router<S>) -> Router<S> + Clone + Send + Sync + 'static,
804
+ {
805
+ ${routerBody}
806
+ }`);
807
+
808
+ return parts.join("\n");
809
+ }
810
+
811
+ function hasOnlyPathParams(
812
+ hasPath: boolean,
813
+ hasQuery: boolean,
814
+ hasBody: boolean,
815
+ ): boolean {
816
+ return hasPath && !hasQuery && !hasBody;
817
+ }
818
+
819
+ function buildRouterBody(
820
+ publicRoutes: string[],
821
+ protectedRoutes: string[],
822
+ ): string {
823
+ const lines: string[] = [];
824
+ lines.push(" let mut router = Router::new();");
825
+ if (publicRoutes.length > 0) {
826
+ lines.push(" let public = Router::new()");
827
+ for (const r of publicRoutes) {
828
+ lines.push(` ${r.trim()}`);
829
+ }
830
+ lines.push(" ;");
831
+ lines.push(" router = router.merge(public);");
832
+ }
833
+ if (protectedRoutes.length > 0) {
834
+ lines.push(" let protected = Router::new()");
835
+ for (const r of protectedRoutes) {
836
+ lines.push(` ${r.trim()}`);
837
+ }
838
+ lines.push(" ;");
839
+ lines.push(" router = router.merge(middleware(protected));");
840
+ }
841
+ lines.push(" router.with_state(service)");
842
+ return lines.join("\n");
843
+ }
844
+
76
845
  const scalarToRust: Record<string, string> = {
77
846
  string: "String",
78
847
  int8: "i8",
@@ -403,10 +1172,17 @@ function emitModel(
403
1172
  parts.push('#[error("{code}: {message}")]');
404
1173
  }
405
1174
 
406
- parts.push(`pub struct ${name}`);
1175
+ const customAttrs = (model as any)[rustAttrKey] as
1176
+ | RustAttrInfo
1177
+ | undefined;
1178
+ if (customAttrs) {
1179
+ for (const attr of customAttrs.attrs) {
1180
+ parts.push(`#[${attr}]`);
1181
+ }
1182
+ }
407
1183
 
408
1184
  if (allProps.size > 0) {
409
- parts.push(" {");
1185
+ const fields: string[] = [];
410
1186
  for (const [propName, prop] of allProps) {
411
1187
  const doc = getDoc(program, prop);
412
1188
  const { type: rustType } = getRustTypeForProperty(
@@ -420,20 +1196,23 @@ function emitModel(
420
1196
  propName !== fieldName ? `#[serde(rename = "${propName}")]` : "";
421
1197
 
422
1198
  if (doc) {
423
- parts.push(`\n ${formatDoc(doc)}`);
1199
+ fields.push(` ${formatDoc(doc)}`);
424
1200
  }
425
1201
  if (serdeRename) {
426
- parts.push(` ${serdeRename}`);
1202
+ fields.push(` ${serdeRename}`);
427
1203
  }
428
1204
  if (optional) {
429
- parts.push(` #[serde(skip_serializing_if = "Option::is_none")]`);
1205
+ fields.push(` #[serde(skip_serializing_if = "Option::is_none")]`);
430
1206
  }
431
- parts.push(
1207
+ fields.push(
432
1208
  ` pub ${fieldName}: ${optional ? `Option<${rustType}>` : rustType},`,
433
1209
  );
434
1210
  }
435
- parts.push("}");
1211
+ parts.push(`pub struct ${name} {
1212
+ ${fields.join("\n")}
1213
+ }`);
436
1214
  } else {
1215
+ parts.push(`pub struct ${name}`);
437
1216
  parts.push("(());");
438
1217
  }
439
1218
 
@@ -468,10 +1247,43 @@ function emitEnum(enumType: Enum): string {
468
1247
  (m) => m.value === undefined || typeof m.value === "string",
469
1248
  );
470
1249
 
1250
+ const baseDerives = [
1251
+ "Debug",
1252
+ "Clone",
1253
+ "Copy",
1254
+ "PartialEq",
1255
+ "Eq",
1256
+ "Hash",
1257
+ "serde::Serialize",
1258
+ "serde::Deserialize",
1259
+ ];
1260
+
1261
+ const customDerives = (enumType as any)[rustDeriveKey] as
1262
+ | RustDeriveInfo
1263
+ | undefined;
1264
+ const allDerives = [...baseDerives];
1265
+ if (customDerives) {
1266
+ allDerives.push(...customDerives.derives);
1267
+ }
1268
+
1269
+ const customAttrs = (enumType as any)[rustAttrKey] as
1270
+ | RustAttrInfo
1271
+ | undefined;
1272
+ const attrLines: string[] = [];
1273
+ if (customAttrs) {
1274
+ for (const attr of customAttrs.attrs) {
1275
+ attrLines.push(`#[${attr}]`);
1276
+ }
1277
+ }
1278
+
471
1279
  if (isString) {
472
1280
  parts.push(
473
- `#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]\npub enum ${name} {`,
1281
+ `#[derive(${allDerives.join(", ")})]`,
474
1282
  );
1283
+ if (attrLines.length > 0) {
1284
+ parts.push(...attrLines);
1285
+ }
1286
+ parts.push(`pub enum ${name} {`);
475
1287
  for (const member of members) {
476
1288
  const variantName = toRustVariantName(member.name);
477
1289
  const serdeValue = member.value ?? member.name;
@@ -480,8 +1292,12 @@ function emitEnum(enumType: Enum): string {
480
1292
  }
481
1293
  } else {
482
1294
  parts.push(
483
- `#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]\npub enum ${name} {`,
1295
+ `#[derive(${allDerives.join(", ")})]`,
484
1296
  );
1297
+ if (attrLines.length > 0) {
1298
+ parts.push(...attrLines);
1299
+ }
1300
+ parts.push(`pub enum ${name} {`);
485
1301
  for (const member of members) {
486
1302
  const variantName = toRustVariantName(member.name);
487
1303
  const enumValue = member.value ?? 0;
@@ -676,10 +1492,47 @@ export async function $onEmit(
676
1492
  content.push("");
677
1493
  }
678
1494
 
1495
+ const namespaceGroups = getAllOperations(context.program);
1496
+
679
1497
  const outputContent = content.join("\n");
680
1498
 
681
1499
  await emitFile(context.program, {
682
1500
  path: resolvePath(context.emitterOutputDir, "types.rs"),
683
1501
  content: outputContent,
684
1502
  });
1503
+
1504
+ if (namespaceGroups.length > 0) {
1505
+ const serverTrait = generateServerTrait(
1506
+ context.program,
1507
+ namespaceGroups,
1508
+ anonymousEnums,
1509
+ );
1510
+ const requestStructs = generateRequestStructs(
1511
+ context.program,
1512
+ namespaceGroups,
1513
+ anonymousEnums,
1514
+ );
1515
+ const responseEnums = generateResponseEnums(
1516
+ context.program,
1517
+ namespaceGroups,
1518
+ anonymousEnums,
1519
+ );
1520
+ const router = generateRouter(
1521
+ context.program,
1522
+ namespaceGroups,
1523
+ anonymousEnums,
1524
+ );
1525
+
1526
+ const serverContent = [
1527
+ serverTrait,
1528
+ requestStructs,
1529
+ responseEnums,
1530
+ router,
1531
+ ].join("\n");
1532
+
1533
+ await emitFile(context.program, {
1534
+ path: resolvePath(context.emitterOutputDir, "server.rs"),
1535
+ content: serverContent,
1536
+ });
1537
+ }
685
1538
  }