alef 0.68.0

Opinionated polyglot binding generator for Rust libraries
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
//! Unit tests for `presentation.rs`, split out for the file-modularization cap.

use super::*;
use crate::e2e::config::{ArgMapping, CallConfig};
use crate::e2e::fixture::{FixtureDocs, FixtureDocsPresentation, SideEffectClass};
use std::collections::BTreeMap;

fn fixture() -> Fixture {
    Fixture {
        id: "present_items".into(),
        description: "Present returned items".into(),
        input: serde_json::json!({"old_source": "test.txt"}),
        docs: Some(FixtureDocs {
            topic: "configuration".into(),
            stem: None,
            paths: BTreeMap::new(),
            title: None,
            description: None,
            input: None,
            shows: Vec::new(),
            error: None,
            presentation: Some(FixtureDocsPresentation {
                call: None,
                input: Some(serde_json::json!({"source": "guide.txt"})),
                args: Some(vec![ArgMapping {
                    name: "source".into(),
                    field: "source".into(),
                    arg_type: "string".into(),
                    optional: false,
                    owned: false,
                    element_type: None,
                    go_type: None,
                    vec_inner_is_ref: false,
                    trait_name: None,
                }]),
                files: Vec::new(),
                operations: vec![FixtureDocsOperation::Iterate {
                    path: "items".into(),
                    item: "item".into(),
                    fields: vec!["text".into(), "metadata.heading".into()],
                    display: true,
                    optional: true,
                }],
            }),
            client: None,
            side_effects: SideEffectClass::Safe,
            coverage_exceptions: BTreeMap::new(),
        }),
        ..Fixture::default()
    }
}

fn config() -> E2eConfig {
    E2eConfig {
        call: CallConfig {
            function: "process".into(),
            result_var: "result".into(),
            ..CallConfig::default()
        },
        fields_optional: ["items".to_string()].into_iter().collect(),
        ..E2eConfig::default()
    }
}

#[test]
fn docs_call_overrides_reuse_typed_fixture_arguments() {
    let fixture = fixture().docs_call_fixture();
    assert_eq!(fixture.input, serde_json::json!({"source": "guide.txt"}));
    assert_eq!(fixture.args[0].arg_type, "string");
    assert_eq!(fixture.args[0].field, "source");
}

#[test]
fn docs_call_fixture_removes_mock_harness_and_uses_an_illustrative_url() {
    let mut fixture = fixture();
    fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .input = None;
    fixture.input = serde_json::json!({
        "mock_responses": [{"path": "/guide.txt", "status_code": 200}],
        "extract_input": {"kind": "uri", "uri": "$mock_url/guide.txt"}
    });
    fixture.mock_response = Some(crate::e2e::fixture::MockResponse {
        status: 200,
        body: None,
        stream_chunks: None,
        headers: Default::default(),
    });

    let docs_fixture = fixture.docs_call_fixture();

    assert!(docs_fixture.mock_response.is_none());
    assert!(docs_fixture.input.get("mock_responses").is_none());
    assert_eq!(
        docs_fixture
            .input
            .pointer("/extract_input/uri")
            .and_then(serde_json::Value::as_str),
        Some("https://example.com/guide.txt")
    );
    assert!(!docs_fixture.needs_mock_server());
}

#[test]
fn show_display_flag_selects_the_human_readable_rust_formatter() {
    let mut display_fixture = fixture();
    display_fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![FixtureDocsOperation::Show {
        path: "text".into(),
        display: true,
    }];
    let mut debug_fixture = fixture();
    debug_fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![FixtureDocsOperation::Show {
        path: "text".into(),
        display: false,
    }];
    let config = config();

    let render = |operations| {
        crate::e2e::template_env::render(
            "rust/snippet_body.rs.jinja",
            minijinja::context! { imports => Vec::<String>::new(), body => vec!["let result = process();"],
            is_async => false, presentation => operations },
        )
    };
    let displayed = render(resolve(&display_fixture, &config, "rust", &[], &[]));
    let debugged = render(resolve(&debug_fixture, &config, "rust", &[], &[]));

    assert!(displayed.contains("println!(\"{}\", result.text);"), "{displayed}");
    assert!(debugged.contains("println!(\"{:?}\", result.text);"), "{debugged}");
}

#[test]
fn presentation_templates_emit_idiomatic_python_rust_and_typescript() {
    let fixture = fixture();
    let config = config();
    let python = resolve(&fixture, &config, "python", &[], &[]);
    let rust = resolve(&fixture, &config, "rust", &[], &[]);
    let mut typescript_fixture = fixture.clone();
    typescript_fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![FixtureDocsOperation::Iterate {
        path: "results[0].chunks".into(),
        item: "chunk".into(),
        fields: vec!["content".into()],
        display: true,
        optional: true,
    }];
    let typescript = resolve(&typescript_fixture, &config, "node", &[], &[]);

    let python_output = crate::e2e::template_env::render(
        "python/snippet_body.py.jinja",
        minijinja::context! { imports => Vec::<String>::new(), body => vec!["result = process()"],
        is_async => false, presentation => python },
    );
    let rust_output = crate::e2e::template_env::render(
        "rust/snippet_body.rs.jinja",
        minijinja::context! { imports => Vec::<String>::new(), body => vec!["let result = process();"],
        is_async => false, presentation => rust },
    );
    let typescript_output = crate::e2e::template_env::render(
        "typescript/snippet_body.jinja",
        minijinja::context! { imports => vec!["process"], module => "@example/library",
        setup_lines => Vec::<String>::new(), client_setup => "", call_expr => "process()",
        result_var => "result", is_async => false, expects_error => false,
        presentation => typescript },
    );

    assert!(
        python_output.contains("for item in result.items or []:"),
        "{python_output}"
    );
    assert!(
        python_output.contains("print(item.metadata.heading)"),
        "{python_output}"
    );
    assert!(
        rust_output.contains("for item in result.items.iter().flatten()"),
        "{rust_output}"
    );
    assert!(
        rust_output.contains("println!(\"{}\", item.metadata.heading);"),
        "{rust_output}"
    );
    assert!(
        typescript_output.contains("const [first] = result.results ?? [];"),
        "{typescript_output}"
    );
    assert!(
        typescript_output.contains("for (const chunk of first?.chunks ?? [])"),
        "{typescript_output}"
    );
    assert!(
        typescript_output.contains("console.log(chunk.content);"),
        "{typescript_output}"
    );
}

/// A fixture's own `optional: false` on an `Iterate` operation must not
/// override field-optionality the resolver already knows about (from the
/// e2e config's `fields_optional`). `config_element_types.json` hit this:
/// `results[0].elements` is a genuinely optional field (registered in
/// `fields_optional`), but the fixture's `Iterate` operation didn't set
/// `"optional": true`, so the generated node/wasm snippet rendered
/// `for (const element of first?.elements)` with no `?? []` guard --
/// `first?.elements` is `Element[] | undefined`, and iterating it directly
/// is a `tsc` TS18048 in strict mode.
#[test]
fn resolve_iterate_treats_path_optional_when_fixture_flag_is_stale() {
    let mut stale_fixture = fixture();
    stale_fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![FixtureDocsOperation::Iterate {
        path: "results[0].elements".into(),
        item: "element".into(),
        fields: vec!["element_type".into()],
        display: true,
        optional: false,
    }];
    let mut stale_config = config();
    stale_config.fields_optional = ["results[0].elements".to_string()].into_iter().collect();

    let operations = resolve(&stale_fixture, &stale_config, "node", &[], &[]);
    let iterate = operations.first().expect("one iterate operation");
    assert!(
        iterate.optional,
        "resolver-known optionality for 'results[0].elements' must win over the fixture's stale `optional: false`"
    );

    let typescript_output = crate::e2e::template_env::render(
        "typescript/snippet_body.jinja",
        minijinja::context! { imports => vec!["process"], module => "@example/library",
        setup_lines => Vec::<String>::new(), client_setup => "", call_expr => "process()",
        result_var => "result", is_async => false, expects_error => false,
        presentation => operations },
    );
    assert!(
        typescript_output.contains("for (const element of first?.elements ?? [])"),
        "{typescript_output}"
    );
}

/// A docs snippet that shows a field reached through an `Option<T>` in a non-leaf
/// position must unwrap, even when the consumer's `alef.toml` never lists that field
/// under `fields_optional` -- the IR alone (`FieldDef.optional`) must be enough. This
/// is the snippet-surface half of the same bug the e2e assertion resolver had: passing
/// real `type_defs` changes the rendered accessor, and `&[]` (no IR) reproduces the old
/// (broken) behavior -- proving the merge in `resolve` actually takes effect rather
/// than every new-parameter call site silently passing an empty set. ~keep
#[test]
fn resolve_show_unwraps_ir_only_optional_field_in_non_leaf_position() {
    use crate::core::ir::{FieldDef, TypeDef};

    let mut show_fixture = fixture();
    show_fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![FixtureDocsOperation::Show {
        path: "data.kind".into(),
        display: false,
    }];
    // No `fields_optional` entry for `data` anywhere in this config -- optionality
    // must come entirely from the IR passed to `resolve`.
    let config = config();
    assert!(!config.fields_optional.contains("data"));

    let process_result = TypeDef {
        name: "ProcessResult".to_string(),
        fields: vec![FieldDef {
            name: "data".to_string(),
            optional: true,
            ..FieldDef::default()
        }],
        ..TypeDef::default()
    };

    let without_ir = resolve(&show_fixture, &config, "rust", &[], &[]);
    let with_ir = resolve(&show_fixture, &config, "rust", &[process_result], &[]);

    assert_eq!(
        without_ir[0].expression, "result.data.kind",
        "with no IR in scope, resolve falls back to the pre-fix (non-compiling) accessor"
    );
    assert_eq!(
        with_ir[0].expression, "result.data.as_ref().unwrap().kind",
        "with IR in scope, resolve must unwrap the Option before the nested field access"
    );
}

/// `display: true` on a `Show` path whose IR-resolved type is a struct/enum this crate
/// defines must be downgraded to the debug formatter -- `extract` never records `Display`
/// impls (`STD_TRAITS` discards them), so `println!("{}", result.data)` against a `Data`
/// struct with no hand-written `Display` is a snippet that does not compile. A sibling
/// `Show` on a plain `String` field must keep `display: true` unchanged -- the whole point
/// of the flag.
#[test]
fn resolve_downgrades_display_true_against_an_ir_struct_field_but_keeps_it_for_a_scalar() {
    use crate::core::ir::{FieldDef, FunctionDef, TypeDef, TypeRef};

    let mut fixture = fixture();
    fixture
        .docs
        .as_mut()
        .and_then(|docs| docs.presentation.as_mut())
        .expect("presentation")
        .operations = vec![
        FixtureDocsOperation::Show {
            path: "data".into(),
            display: true,
        },
        FixtureDocsOperation::Show {
            path: "text".into(),
            display: true,
        },
    ];
    let config = config();

    let process_result = TypeDef {
        name: "ProcessResult".to_string(),
        fields: vec![
            FieldDef {
                name: "data".to_string(),
                ty: TypeRef::Named("Data".to_string()),
                ..FieldDef::default()
            },
            FieldDef {
                name: "text".to_string(),
                ty: TypeRef::String,
                ..FieldDef::default()
            },
        ],
        ..TypeDef::default()
    };
    let data = TypeDef {
        name: "Data".to_string(),
        ..TypeDef::default()
    };
    let process_fn = FunctionDef {
        name: "process".to_string(),
        return_type: TypeRef::Named("ProcessResult".to_string()),
        ..FunctionDef::default()
    };

    let operations = resolve(
        &fixture,
        &config,
        "rust",
        &[process_result, data],
        std::slice::from_ref(&process_fn),
    );
    let by_path = |path: &str| operations.iter().find(|op| op.expression.ends_with(path)).unwrap();

    assert!(
        !by_path("data").display,
        "a struct-typed field must be downgraded to the debug formatter"
    );
    assert!(
        by_path("text").display,
        "a scalar field must keep its authored display: true"
    );

    let rust_output = crate::e2e::template_env::render(
        "rust/snippet_body.rs.jinja",
        minijinja::context! { imports => Vec::<String>::new(), body => vec!["let result = process();"],
        is_async => false, presentation => operations },
    );
    assert!(
        rust_output.contains("println!(\"{:?}\", result.data);"),
        "{rust_output}"
    );
    assert!(rust_output.contains("println!(\"{}\", result.text);"), "{rust_output}");
}

/// The shape every fixture-driven (non-hand-authored) docs fixture takes: `docs` is
/// present so the fixture DOES get a snippet, but nobody hand-annotated `shows` or
/// `presentation` -- the only field knowledge lives in `assertions`. Before this fell
/// back to reading `assertions`, `resolve` returned an empty operations list here and
/// every generated snippet in every language bottomed out at a bare
/// `print(result)`/`println!("{:?}", result)`, never showing how to consume the return
/// value. Two assertions on the same field (`equals` and `not_empty`, both on
/// `"content"`) must collapse to one `show`, not print the field twice.
#[test]
fn resolve_derives_show_operations_from_assertion_fields_when_docs_names_none() {
    let fixture: Fixture = serde_json::from_value(serde_json::json!({
        "id": "smoke_simple_paragraph",
        "description": "Simple paragraph converts correctly",
        "input": {"html": "<p>Hello World</p>"},
        "assertions": [
            {"type": "equals", "field": "content", "value": "Hello World\n"},
            {"type": "not_empty", "field": "content"}
        ],
        "docs": {"topic": "smoke", "stem": "smoke_simple_paragraph"}
    }))
    .expect("fixture must parse");
    let config = E2eConfig {
        call: CallConfig {
            function: "convert".into(),
            result_var: "result".into(),
            ..CallConfig::default()
        },
        ..E2eConfig::default()
    };

    let python = resolve(&fixture, &config, "python", &[], &[]);
    assert_eq!(python.len(), 1, "the duplicate 'content' field must not be shown twice");
    assert_eq!(python[0].kind, "show");
    assert_eq!(python[0].expression, "result.content");

    let rust = resolve(&fixture, &config, "rust", &[], &[]);
    assert_eq!(rust[0].expression, "result.content");
}

/// An `error`-typed assertion names no `field` and must not be mistaken for one -- it
/// documents a failure mode, not a field to print on the success path.
#[test]
fn resolve_ignores_assertions_with_no_field_when_deriving_show_operations() {
    let fixture: Fixture = serde_json::from_value(serde_json::json!({
        "id": "auth_error",
        "description": "Authentication failure",
        "input": {"token": "bad"},
        "assertions": [{"type": "error"}],
        "docs": {"topic": "errors", "stem": "auth_error"}
    }))
    .expect("fixture must parse");
    let config = config();

    assert!(resolve(&fixture, &config, "python", &[], &[]).is_empty());
}

/// A void call has no result to access; even a fixture whose assertions happen to name a
/// field (e.g. a side-effect check) must not gain a fabricated `print(result.<field>)`.
#[test]
fn resolve_derives_no_show_operations_for_a_void_returning_call() {
    let fixture: Fixture = serde_json::from_value(serde_json::json!({
        "id": "configure_logging",
        "description": "Configure logging",
        "input": {"level": "debug"},
        "assertions": [{"type": "equals", "field": "level", "value": "debug"}],
        "docs": {"topic": "configuration", "stem": "configure_logging"}
    }))
    .expect("fixture must parse");
    let mut config = config();
    config.call.returns_void = true;

    assert!(resolve(&fixture, &config, "python", &[], &[]).is_empty());
}