Skip to content

Commit 74734d5

Browse files
feat: add support for all rust number types (#2209)
1 parent d79b345 commit 74734d5

File tree

2 files changed

+225
-5
lines changed

2 files changed

+225
-5
lines changed

src/generators/rust/RustConstrainer.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,41 @@ export const RustDefaultTypeMapping: RustTypeMapping = {
289289
}
290290
},
291291
Integer({ constrainedModel }): string {
292-
const type = 'i32';
293292
switch (constrainedModel.options.format) {
294-
case 'integer':
293+
case 'int8':
294+
case 'i8':
295+
return 'i8';
296+
case 'int16':
297+
case 'i16':
298+
return 'i16';
295299
case 'int32':
296-
return type;
297-
case 'long':
300+
case 'integer':
301+
case 'i32':
302+
return 'i32';
298303
case 'int64':
304+
case 'long':
305+
case 'i64':
299306
return 'i64';
307+
case 'int128':
308+
case 'i128':
309+
return 'i128';
310+
case 'uint8':
311+
case 'u8':
312+
return 'u8';
313+
case 'uint16':
314+
case 'u16':
315+
return 'u16';
316+
case 'uint32':
317+
case 'u32':
318+
return 'u32';
319+
case 'uint64':
320+
case 'u64':
321+
return 'u64';
322+
case 'uint128':
323+
case 'u128':
324+
return 'u128';
300325
default:
301-
return type;
326+
return 'i32';
302327
}
303328
},
304329
String({ constrainedModel }): string {

test/generators/rust/RustConstrainer.spec.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,201 @@ describe('RustConstrainer', () => {
149149
});
150150
expect(type).toEqual('i64');
151151
});
152+
test('should render u8 when format is u8', () => {
153+
const model = new ConstrainedIntegerModel(
154+
'test',
155+
{},
156+
{ format: 'u8' },
157+
''
158+
);
159+
const type = RustDefaultTypeMapping.Integer({
160+
constrainedModel: model,
161+
...defaultOptions
162+
});
163+
expect(type).toEqual('u8');
164+
});
165+
166+
test('should render u16 when format is u16', () => {
167+
const model = new ConstrainedIntegerModel(
168+
'test',
169+
{},
170+
{ format: 'u16' },
171+
''
172+
);
173+
const type = RustDefaultTypeMapping.Integer({
174+
constrainedModel: model,
175+
...defaultOptions
176+
});
177+
expect(type).toEqual('u16');
178+
});
179+
180+
test('should render u32 when format is u32', () => {
181+
const model = new ConstrainedIntegerModel(
182+
'test',
183+
{},
184+
{ format: 'u32' },
185+
''
186+
);
187+
const type = RustDefaultTypeMapping.Integer({
188+
constrainedModel: model,
189+
...defaultOptions
190+
});
191+
expect(type).toEqual('u32');
192+
});
193+
194+
test('should render u64 when format is u64', () => {
195+
const model = new ConstrainedIntegerModel(
196+
'test',
197+
{},
198+
{ format: 'u64' },
199+
''
200+
);
201+
const type = RustDefaultTypeMapping.Integer({
202+
constrainedModel: model,
203+
...defaultOptions
204+
});
205+
expect(type).toEqual('u64');
206+
});
207+
208+
test('should render u128 when format is u128', () => {
209+
const model = new ConstrainedIntegerModel(
210+
'test',
211+
{},
212+
{ format: 'u128' },
213+
''
214+
);
215+
const type = RustDefaultTypeMapping.Integer({
216+
constrainedModel: model,
217+
...defaultOptions
218+
});
219+
expect(type).toEqual('u128');
220+
});
221+
222+
test('should render uint32 format as u32', () => {
223+
const model = new ConstrainedIntegerModel(
224+
'test',
225+
{},
226+
{ format: 'uint32' },
227+
''
228+
);
229+
const type = RustDefaultTypeMapping.Integer({
230+
constrainedModel: model,
231+
...defaultOptions
232+
});
233+
expect(type).toEqual('u32');
234+
});
235+
236+
test('should render uint64 format as u64', () => {
237+
const model = new ConstrainedIntegerModel(
238+
'test',
239+
{},
240+
{ format: 'uint64' },
241+
''
242+
);
243+
const type = RustDefaultTypeMapping.Integer({
244+
constrainedModel: model,
245+
...defaultOptions
246+
});
247+
expect(type).toEqual('u64');
248+
});
249+
250+
test('should render i8 when format is int8', () => {
251+
const model = new ConstrainedIntegerModel(
252+
'test',
253+
{},
254+
{ format: 'int8' },
255+
''
256+
);
257+
const type = RustDefaultTypeMapping.Integer({
258+
constrainedModel: model,
259+
...defaultOptions
260+
});
261+
expect(type).toEqual('i8');
262+
});
263+
264+
test('should render i16 when format is int16', () => {
265+
const model = new ConstrainedIntegerModel(
266+
'test',
267+
{},
268+
{ format: 'int16' },
269+
''
270+
);
271+
const type = RustDefaultTypeMapping.Integer({
272+
constrainedModel: model,
273+
...defaultOptions
274+
});
275+
expect(type).toEqual('i16');
276+
});
277+
278+
test('should render i128 when format is int128', () => {
279+
const model = new ConstrainedIntegerModel(
280+
'test',
281+
{},
282+
{ format: 'int128' },
283+
''
284+
);
285+
const type = RustDefaultTypeMapping.Integer({
286+
constrainedModel: model,
287+
...defaultOptions
288+
});
289+
expect(type).toEqual('i128');
290+
});
291+
292+
test('should render u8 when format is uint8', () => {
293+
const model = new ConstrainedIntegerModel(
294+
'test',
295+
{},
296+
{ format: 'uint8' },
297+
''
298+
);
299+
const type = RustDefaultTypeMapping.Integer({
300+
constrainedModel: model,
301+
...defaultOptions
302+
});
303+
expect(type).toEqual('u8');
304+
});
305+
306+
test('should render u16 when format is uint16', () => {
307+
const model = new ConstrainedIntegerModel(
308+
'test',
309+
{},
310+
{ format: 'uint16' },
311+
''
312+
);
313+
const type = RustDefaultTypeMapping.Integer({
314+
constrainedModel: model,
315+
...defaultOptions
316+
});
317+
expect(type).toEqual('u16');
318+
});
319+
320+
test('should render u128 when format is uint128', () => {
321+
const model = new ConstrainedIntegerModel(
322+
'test',
323+
{},
324+
{ format: 'uint128' },
325+
''
326+
);
327+
const type = RustDefaultTypeMapping.Integer({
328+
constrainedModel: model,
329+
...defaultOptions
330+
});
331+
expect(type).toEqual('u128');
332+
});
333+
334+
test('should render default i32 when format is unknown', () => {
335+
const model = new ConstrainedIntegerModel(
336+
'test',
337+
{},
338+
{ format: 'unknown' },
339+
''
340+
);
341+
const type = RustDefaultTypeMapping.Integer({
342+
constrainedModel: model,
343+
...defaultOptions
344+
});
345+
expect(type).toEqual('i32');
346+
});
152347
});
153348
describe('String', () => {
154349
test('should render type', () => {

0 commit comments

Comments
 (0)