cratestack-parser 0.7.15

Rust-native schema-first framework for typed HTTP APIs, generated clients, and backend services.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#![cfg(test)]

use super::parse_schema;

#[test]
fn accepts_isolation_attribute_on_procedure() {
    let schema = parse_schema(
        r#"
type TransferInput {
  from Int
  to Int
}

mutation procedure transfer(args: TransferInput): TransferInput
  @isolation("serializable")
"#,
    )
    .expect("procedure with @isolation should parse");

    let attrs = &schema.procedures[0].attributes;
    assert!(
        attrs
            .iter()
            .any(|a| a.raw == "@isolation(\"serializable\")"),
        "expected @isolation in attributes: {attrs:?}",
    );
}

#[test]
fn accepts_isolation_repeatable_read() {
    parse_schema(
        r#"
type Ping {
  nonce String
}

procedure read_only(args: Ping): Ping
  @isolation("repeatable_read")
"#,
    )
    .expect("repeatable_read isolation should parse");
}

#[test]
fn rejects_invalid_isolation_level() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @isolation("snapshot")
"#,
    )
    .expect_err("unknown isolation level should fail");

    assert!(
        error
            .to_string()
            .contains("unknown transaction isolation level"),
        "error: {error}",
    );
}

#[test]
fn rejects_isolation_missing_argument() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @isolation
"#,
    )
    .expect_err("@isolation without args should fail");

    assert!(
        error
            .to_string()
            .contains("@isolation requires a quoted level argument"),
        "error: {error}",
    );
}

#[test]
fn accepts_api_version_and_deprecated_on_procedure() {
    let schema = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure healthcheck(args: Ping): Ping
  @api_version("v1")
  @deprecated("use healthcheck_v2")
"#,
    )
    .expect("procedure with @api_version + @deprecated should parse");

    let attrs = &schema.procedures[0].attributes;
    assert!(
        attrs.iter().any(|a| a.raw == "@api_version(\"v1\")"),
        "expected @api_version: {attrs:?}",
    );
    assert!(
        attrs
            .iter()
            .any(|a| a.raw == "@deprecated(\"use healthcheck_v2\")"),
        "expected @deprecated",
    );
}

#[test]
fn rejects_empty_api_version() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure healthcheck(args: Ping): Ping
  @api_version("")
"#,
    )
    .expect_err("empty @api_version should fail");

    assert!(
        error.to_string().contains("@api_version must not be empty"),
        "error: {error}",
    );
}

#[test]
fn rejects_api_version_with_invalid_characters() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure healthcheck(args: Ping): Ping
  @api_version("v 1")
"#,
    )
    .expect_err("@api_version with space should fail");

    assert!(
        error.to_string().contains("must contain only alphanumeric"),
        "error: {error}",
    );
}

#[test]
fn parses_no_idempotency_attribute_on_procedure() {
    let schema = parse_schema(
        r#"
type Ping {
  nonce String
}

mutation procedure healthcheck(args: Ping): Ping
  @no_idempotency
"#,
    )
    .expect("procedure with @no_idempotency should parse");

    let attrs = &schema.procedures[0].attributes;
    assert!(
        attrs.iter().any(|a| a.raw == "@no_idempotency"),
        "procedure attributes should include @no_idempotency: {:?}",
        attrs,
    );
}

/// cratestack#154: `@no_rate_limit` is valid syntax when the schema
/// declares `extension rate_limit { }`.
#[test]
fn accepts_no_rate_limit_attribute_when_extension_is_declared() {
    let schema = parse_schema(
        r#"
extension rate_limit {
}

type Ping {
  nonce String
}

mutation procedure createPayment(args: Ping): Ping
  @no_rate_limit
"#,
    )
    .expect("@no_rate_limit should parse when `extension rate_limit { }` is declared");

    let attrs = &schema.procedures[0].attributes;
    assert!(
        attrs.iter().any(|a| a.raw == "@no_rate_limit"),
        "procedure attributes should include @no_rate_limit: {:?}",
        attrs,
    );
}

/// cratestack#154: using `@no_rate_limit` without declaring `extension
/// rate_limit { }` anywhere in the schema is a validation error, not a
/// silent no-op — mirrors the Cargo-feature-gate error #161 raises at macro
/// expansion time, but this one fires at parse/validate time since the
/// parser already knows whether the extension is declared.
#[test]
fn rejects_no_rate_limit_attribute_without_extension_declared() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

mutation procedure createPayment(args: Ping): Ping
  @no_rate_limit
"#,
    )
    .expect_err("@no_rate_limit without `extension rate_limit { }` should be a validation error");

    let message = error.to_string();
    assert!(
        message.contains("@no_rate_limit"),
        "error should name the offending attribute, got: {message}",
    );
    assert!(
        message.contains("extension rate_limit"),
        "error should point at the missing `extension rate_limit {{ }}` declaration, got: {message}",
    );
}

/// cratestack#154: `@no_rate_limit` takes no arguments.
#[test]
fn rejects_no_rate_limit_attribute_with_arguments() {
    let error = parse_schema(
        r#"
extension rate_limit {
}

type Ping {
  nonce String
}

mutation procedure createPayment(args: Ping): Ping
  @no_rate_limit("forever")
"#,
    )
    .expect_err("@no_rate_limit should not accept any arguments");

    assert!(
        error.to_string().contains("does not take any arguments"),
        "error: {error}",
    );
}

/// cratestack#407: `@status(202)` is accepted and threaded into
/// `procedure.attributes` verbatim, mirroring `@api_version`/`@deprecated`.
#[test]
fn accepts_status_attribute_on_procedure() {
    let schema = parse_schema(
        r#"
type Ping {
  nonce String
}

mutation procedure submit(args: Ping): Ping
  @status(202)
"#,
    )
    .expect("procedure with @status(202) should parse");

    let attrs = &schema.procedures[0].attributes;
    assert!(
        attrs.iter().any(|a| a.raw == "@status(202)"),
        "expected @status(202) in attributes: {attrs:?}",
    );
}

/// cratestack#407: the boundary values of the allowed `200..=299` range are
/// both accepted.
#[test]
fn accepts_status_boundary_values() {
    parse_schema(
        r#"
type Ping {
  nonce String
}

procedure lower(args: Ping): Ping
  @status(200)
"#,
    )
    .expect("@status(200) should parse");

    parse_schema(
        r#"
type Ping {
  nonce String
}

procedure upper(args: Ping): Ping
  @status(299)
"#,
    )
    .expect("@status(299) should parse");
}

/// cratestack#407: anything outside `200..=299` is a schema-compile-time
/// error, not a runtime surprise — `CoolError` already owns the
/// 3xx/4xx/5xx space.
#[test]
fn rejects_status_outside_2xx_range() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @status(404)
"#,
    )
    .expect_err("@status(404) should be rejected");

    assert!(
        error.to_string().contains("outside the allowed 2xx range"),
        "error: {error}",
    );
}

/// cratestack#407: `@status(300)` (just above the allowed range) is
/// rejected too — this pins the exact boundary as a regression guard.
#[test]
fn rejects_status_just_above_range() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @status(300)
"#,
    )
    .expect_err("@status(300) should be rejected");

    assert!(
        error.to_string().contains("outside the allowed 2xx range"),
        "error: {error}",
    );
}

/// cratestack#407: `@status` requires a numeric argument.
#[test]
fn rejects_status_non_numeric_argument() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @status("202")
"#,
    )
    .expect_err("@status with a quoted argument should be rejected");

    assert!(
        error.to_string().contains("integer HTTP status code"),
        "error: {error}",
    );
}

/// cratestack#407: `@status` without an argument is rejected.
#[test]
fn rejects_status_missing_argument() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @status
"#,
    )
    .expect_err("@status without args should fail");

    assert!(
        error
            .to_string()
            .contains("@status requires a numeric status code argument"),
        "error: {error}",
    );
}

/// cratestack#407: at most one `@status` per procedure.
#[test]
fn rejects_duplicate_status_attribute() {
    let error = parse_schema(
        r#"
type Ping {
  nonce String
}

procedure broken(args: Ping): Ping
  @status(202)
  @status(201)
"#,
    )
    .expect_err("more than one @status on the same procedure should be rejected");

    assert!(
        error.to_string().contains("more than one @status"),
        "error: {error}",
    );
}

/// cratestack#407 follow-up: `@status` is a REST-only attribute.
/// `generate_procedure_axum_handler` emits a single `#dispatch_ident`
/// shared by both the REST route and the `transport rpc` unary dispatch
/// arm, so an unrejected `@status` here would silently become
/// wire-visible on the RPC envelope too (`convert_handler_error_response`
/// passes any `is_success()` status through unchanged). Reject the
/// combination at schema-compile time instead.
#[test]
fn rejects_status_attribute_under_transport_rpc() {
    let error = parse_schema(
        r#"
transport rpc

type Ping {
  nonce String
}

mutation procedure submit(args: Ping): Ping
  @status(202)
"#,
    )
    .expect_err("@status under `transport rpc` should be rejected");

    assert!(
        error.to_string().contains("REST-only attribute"),
        "error: {error}",
    );
}

/// cratestack#407 follow-up: `transport grpc` is unaffected by the
/// `transport rpc` rejection above — gRPC's own status model never
/// reads the HTTP status `@status` controls, so the combination is
/// inert there, not silently wrong, and stays accepted.
#[test]
fn accepts_status_attribute_under_transport_grpc() {
    parse_schema(
        r#"
transport grpc

type Ping {
  nonce String
}

mutation procedure submit(args: Ping): Ping
  @status(202)
"#,
    )
    .expect("@status under `transport grpc` should still parse");
}

/// cratestack#154: at most one `@no_rate_limit` per procedure.
#[test]
fn rejects_duplicate_no_rate_limit_attribute() {
    let error = parse_schema(
        r#"
extension rate_limit {
}

type Ping {
  nonce String
}

mutation procedure createPayment(args: Ping): Ping
  @no_rate_limit
  @no_rate_limit
"#,
    )
    .expect_err("more than one @no_rate_limit on the same procedure should be rejected");

    assert!(
        error.to_string().contains("more than one @no_rate_limit"),
        "error: {error}",
    );
}