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/.qwen/settings.json +11 -0
- package/CHANGELOG.md +86 -0
- package/DEV.md +81 -0
- package/QWEN.md +238 -0
- package/README.md +194 -4
- package/dist/src/emitter.d.ts +2 -0
- package/dist/src/emitter.js +662 -16
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/test/hello.test.js +78 -0
- package/dist/test/hello.test.js.map +1 -1
- package/example/lib/learning/models.tsp +2 -0
- package/example/output-rust/Cargo.lock +639 -1
- package/example/output-rust/Cargo.toml +9 -1
- package/example/output-rust/src/generated/mod.rs +2 -1
- package/example/output-rust/src/generated/server.rs +712 -0
- package/example/output-rust/src/generated/types.rs +20 -42
- package/example/package-lock.json +1 -2
- package/justfile +4 -1
- package/package.json +8 -3
- package/src/emitter.ts +868 -15
- package/src/index.ts +1 -1
- package/src/lib.tsp +4 -2
- package/test/hello.test.ts +100 -0
package/src/index.ts
CHANGED
package/src/lib.tsp
CHANGED
|
@@ -2,5 +2,7 @@ import "../dist/src/emitter.js";
|
|
|
2
2
|
|
|
3
3
|
using TypeSpec.Reflection;
|
|
4
4
|
|
|
5
|
-
extern dec rustDerive(target: Model, derive: valueof string);
|
|
6
|
-
extern dec rustDerives(target: Model, ...derives: valueof string[]);
|
|
5
|
+
extern dec rustDerive(target: Model | Enum, derive: valueof string);
|
|
6
|
+
extern dec rustDerives(target: Model | Enum, ...derives: valueof string[]);
|
|
7
|
+
extern dec rustAttr(target: Model | Enum, attr: valueof string);
|
|
8
|
+
extern dec rustAttrs(target: Model | Enum, ...attrs: valueof string[]);
|
package/test/hello.test.ts
CHANGED
|
@@ -165,4 +165,104 @@ describe("Rust emitter", () => {
|
|
|
165
165
|
true,
|
|
166
166
|
);
|
|
167
167
|
});
|
|
168
|
+
|
|
169
|
+
it("emits custom rustDerive on enum", async () => {
|
|
170
|
+
const results = await emit(`
|
|
171
|
+
import "typespec-rust-emitter";
|
|
172
|
+
|
|
173
|
+
@rustDerive("strum::Display")
|
|
174
|
+
enum Status {
|
|
175
|
+
active,
|
|
176
|
+
inactive,
|
|
177
|
+
}
|
|
178
|
+
`);
|
|
179
|
+
const output = results["types.rs"];
|
|
180
|
+
strictEqual(
|
|
181
|
+
output.includes(
|
|
182
|
+
"#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, strum::Display)]",
|
|
183
|
+
),
|
|
184
|
+
true,
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("emits multiple rustDerive macros on enum via $rustDerives", async () => {
|
|
189
|
+
const results = await emit(`
|
|
190
|
+
import "typespec-rust-emitter";
|
|
191
|
+
|
|
192
|
+
@rustDerives("strum::Display", "strum::EnumString")
|
|
193
|
+
enum Priority {
|
|
194
|
+
low,
|
|
195
|
+
high,
|
|
196
|
+
}
|
|
197
|
+
`);
|
|
198
|
+
const output = results["types.rs"];
|
|
199
|
+
strictEqual(
|
|
200
|
+
output.includes(
|
|
201
|
+
"#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumString)]",
|
|
202
|
+
),
|
|
203
|
+
true,
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("emits custom rustAttr on model", async () => {
|
|
208
|
+
const results = await emit(`
|
|
209
|
+
import "typespec-rust-emitter";
|
|
210
|
+
|
|
211
|
+
@rustAttr("sqlx(type_name = \\"user\\")")
|
|
212
|
+
model User {
|
|
213
|
+
name: string;
|
|
214
|
+
}
|
|
215
|
+
`);
|
|
216
|
+
const output = results["types.rs"];
|
|
217
|
+
strictEqual(output.includes('#[sqlx(type_name = "user")]'), true);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("emits multiple rustAttrs on model via $rustAttrs", async () => {
|
|
221
|
+
const results = await emit(`
|
|
222
|
+
import "typespec-rust-emitter";
|
|
223
|
+
|
|
224
|
+
@rustAttrs("sqlx(type_name = \\"user\\")", "cfg_attr(feature = \\"test\\", derive(Debug))")
|
|
225
|
+
model Person {
|
|
226
|
+
name: string;
|
|
227
|
+
}
|
|
228
|
+
`);
|
|
229
|
+
const output = results["types.rs"];
|
|
230
|
+
strictEqual(output.includes('#[sqlx(type_name = "user")]'), true);
|
|
231
|
+
strictEqual(
|
|
232
|
+
output.includes('#[cfg_attr(feature = "test", derive(Debug))]'),
|
|
233
|
+
true,
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("emits custom rustAttr on enum", async () => {
|
|
238
|
+
const results = await emit(`
|
|
239
|
+
import "typespec-rust-emitter";
|
|
240
|
+
|
|
241
|
+
@rustAttr("sqlx(type_name = \\"study_status\\")")
|
|
242
|
+
enum Status {
|
|
243
|
+
active,
|
|
244
|
+
inactive,
|
|
245
|
+
}
|
|
246
|
+
`);
|
|
247
|
+
const output = results["types.rs"];
|
|
248
|
+
strictEqual(output.includes('#[sqlx(type_name = "study_status")]'), true);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("emits multiple rustAttrs on enum via $rustAttrs", async () => {
|
|
252
|
+
const results = await emit(`
|
|
253
|
+
import "typespec-rust-emitter";
|
|
254
|
+
|
|
255
|
+
@rustAttrs("sqlx(type_name = \\"priority\\")", "cfg_attr(feature = \\"test\\", derive(Debug))")
|
|
256
|
+
enum Priority {
|
|
257
|
+
low,
|
|
258
|
+
high,
|
|
259
|
+
}
|
|
260
|
+
`);
|
|
261
|
+
const output = results["types.rs"];
|
|
262
|
+
strictEqual(output.includes('#[sqlx(type_name = "priority")]'), true);
|
|
263
|
+
strictEqual(
|
|
264
|
+
output.includes('#[cfg_attr(feature = "test", derive(Debug))]'),
|
|
265
|
+
true,
|
|
266
|
+
);
|
|
267
|
+
});
|
|
168
268
|
});
|