presolve-compiler 0.1.0-alpha.3

The Presolve compiler toolchain for TypeScript web applications.
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use crate::{
    decode_tooling_query_snapshot_v1, ToolingQueryDiagnosticV1, ToolingQueryReferenceV1,
    ToolingQuerySemanticRecordV1, ToolingQuerySnapshotV1,
};
use serde::{Deserialize, Serialize};

const REQUEST_SCHEMA_V1: &str = "presolve.language-service-wasm-request";
const RESPONSE_SCHEMA_V1: &str = "presolve.language-service-wasm-response";

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct PositionRequestV1 {
    schema: String,
    version: u32,
    operation: String,
    source_unit_id: String,
    offset: u64,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SemanticIdRequestV1 {
    schema: String,
    version: u32,
    operation: String,
    query_semantic_id: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SourceUnitRequestV1 {
    schema: String,
    version: u32,
    operation: String,
    source_unit_id: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UnsupportedRequestV1 {
    schema: String,
    version: u32,
    operation: String,
}

enum QueryRequestV1 {
    Position(PositionRequestV1),
    Definition(SemanticIdRequestV1),
    References(SemanticIdRequestV1),
    DocumentSymbols(SourceUnitRequestV1),
    Diagnostics(SourceUnitRequestV1),
    Unsupported(UnsupportedRequestV1),
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ErrorResponseV1<'a> {
    schema: &'static str,
    version: u32,
    operation: &'a str,
    status: &'static str,
    code: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct UnsupportedResponseV1<'a> {
    schema: &'static str,
    version: u32,
    operation: &'a str,
    status: &'static str,
    capability: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct RecordsResultV1 {
    records: Vec<ToolingQuerySemanticRecordV1>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct RecordResultV1 {
    record: ToolingQuerySemanticRecordV1,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ReferencesResultV1 {
    references: Vec<ToolingQueryReferenceV1>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct DiagnosticsResultV1 {
    diagnostics: Vec<ToolingQueryDiagnosticV1>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct OkResponseV1<T> {
    schema: &'static str,
    version: u32,
    operation: String,
    status: &'static str,
    result: T,
}

/// Projects one caller-supplied query snapshot through the L12-C WASM protocol.
///
/// This stays crate-private so no native API becomes a competing public delivery
/// authority. The WASM adapter is the only planned external caller.
#[expect(
    clippy::too_many_lines,
    reason = "the five fixed L12 projections are reviewed as one protocol dispatch"
)]
pub(crate) fn query_snapshot_v1(product_bytes: &[u8], request_bytes: &[u8]) -> Vec<u8> {
    let Ok(product) = decode_tooling_query_snapshot_v1(product_bytes) else {
        return error_response("", "invalid_product");
    };
    let request = match decode_request_v1(request_bytes) {
        Ok(request) => request,
        Err(operation) => return error_response(&operation, "invalid_request"),
    };

    match request {
        QueryRequestV1::Position(request) => {
            let Some(unit) = product
                .source_units
                .iter()
                .find(|unit| unit.source_unit_id == request.source_unit_id)
            else {
                return error_response(&request.operation, "unknown_source_unit");
            };
            if request.offset > unit.source_length {
                return error_response(&request.operation, "offset_out_of_range");
            }
            ok_response(
                request.operation,
                RecordsResultV1 {
                    records: product
                        .semantic_records
                        .into_iter()
                        .filter(|record| {
                            record.range.source_unit_id == unit.source_unit_id
                                && record.range.start <= request.offset
                                && request.offset < record.range.end
                        })
                        .collect(),
                },
            )
        }
        QueryRequestV1::Definition(request) => {
            let Some(record) = product
                .semantic_records
                .into_iter()
                .find(|record| record.query_semantic_id == request.query_semantic_id)
            else {
                return error_response(&request.operation, "unknown_query_semantic_id");
            };
            ok_response(request.operation, RecordResultV1 { record })
        }
        QueryRequestV1::References(request) => {
            if !product
                .semantic_records
                .iter()
                .any(|record| record.query_semantic_id == request.query_semantic_id)
            {
                return error_response(&request.operation, "unknown_query_semantic_id");
            }
            ok_response(
                request.operation,
                ReferencesResultV1 {
                    references: product
                        .references
                        .into_iter()
                        .filter(|reference| {
                            reference.target_query_semantic_id == request.query_semantic_id
                        })
                        .collect(),
                },
            )
        }
        QueryRequestV1::DocumentSymbols(request) => {
            if !has_source_unit(&product, &request.source_unit_id) {
                return error_response(&request.operation, "unknown_source_unit");
            }
            ok_response(
                request.operation,
                RecordsResultV1 {
                    records: product
                        .semantic_records
                        .into_iter()
                        .filter(|record| record.range.source_unit_id == request.source_unit_id)
                        .collect(),
                },
            )
        }
        QueryRequestV1::Diagnostics(request) => {
            if !has_source_unit(&product, &request.source_unit_id) {
                return error_response(&request.operation, "unknown_source_unit");
            }
            ok_response(
                request.operation,
                DiagnosticsResultV1 {
                    diagnostics: product
                        .diagnostics
                        .into_iter()
                        .filter(|diagnostic| {
                            diagnostic
                                .primary_range
                                .as_ref()
                                .is_some_and(|range| range.source_unit_id == request.source_unit_id)
                        })
                        .collect(),
                },
            )
        }
        QueryRequestV1::Unsupported(request) => unsupported_response(&request.operation),
    }
}

fn has_source_unit(product: &ToolingQuerySnapshotV1, source_unit_id: &str) -> bool {
    product
        .source_units
        .iter()
        .any(|unit| unit.source_unit_id == source_unit_id)
}

fn decode_request_v1(bytes: &[u8]) -> Result<QueryRequestV1, String> {
    let value: serde_json::Value = serde_json::from_slice(bytes).map_err(|_| String::new())?;
    let operation = value
        .get("operation")
        .and_then(serde_json::Value::as_str)
        .map_or_else(String::new, ToOwned::to_owned);
    let request = match operation.as_str() {
        "position" => serde_json::from_value::<PositionRequestV1>(value.clone())
            .ok()
            .filter(valid_position_request)
            .map(QueryRequestV1::Position),
        "definition" => serde_json::from_value::<SemanticIdRequestV1>(value.clone())
            .ok()
            .filter(|request| valid_semantic_id_request(request, "definition"))
            .map(QueryRequestV1::Definition),
        "references" => serde_json::from_value::<SemanticIdRequestV1>(value.clone())
            .ok()
            .filter(|request| valid_semantic_id_request(request, "references"))
            .map(QueryRequestV1::References),
        "documentSymbols" => serde_json::from_value::<SourceUnitRequestV1>(value.clone())
            .ok()
            .filter(|request| valid_source_unit_request(request, "documentSymbols"))
            .map(QueryRequestV1::DocumentSymbols),
        "diagnostics" => serde_json::from_value::<SourceUnitRequestV1>(value.clone())
            .ok()
            .filter(|request| valid_source_unit_request(request, "diagnostics"))
            .map(QueryRequestV1::Diagnostics),
        "hover" | "rename" | "completion" | "signatureHelp" | "semanticTokens"
        | "sourceMapping" | "edits" | "codeActions" => {
            serde_json::from_value::<UnsupportedRequestV1>(value.clone())
                .ok()
                .filter(valid_unsupported_request)
                .map(QueryRequestV1::Unsupported)
        }
        _ => None,
    }
    .ok_or_else(|| operation.clone())?;

    (request_json(&request).as_bytes() == bytes)
        .then_some(request)
        .ok_or(operation)
}

fn valid_position_request(request: &PositionRequestV1) -> bool {
    request.schema == REQUEST_SCHEMA_V1 && request.version == 1 && request.operation == "position"
}

fn valid_semantic_id_request(request: &SemanticIdRequestV1, operation: &str) -> bool {
    request.schema == REQUEST_SCHEMA_V1
        && request.version == 1
        && request.operation == operation
        && !request.query_semantic_id.is_empty()
}

fn valid_source_unit_request(request: &SourceUnitRequestV1, operation: &str) -> bool {
    request.schema == REQUEST_SCHEMA_V1
        && request.version == 1
        && request.operation == operation
        && !request.source_unit_id.is_empty()
}

fn valid_unsupported_request(request: &UnsupportedRequestV1) -> bool {
    request.schema == REQUEST_SCHEMA_V1
        && request.version == 1
        && matches!(
            request.operation.as_str(),
            "hover"
                | "rename"
                | "completion"
                | "signatureHelp"
                | "semanticTokens"
                | "sourceMapping"
                | "edits"
                | "codeActions"
        )
}

fn request_json(request: &QueryRequestV1) -> String {
    match request {
        QueryRequestV1::Position(request) => json(request),
        QueryRequestV1::Definition(request) | QueryRequestV1::References(request) => json(request),
        QueryRequestV1::DocumentSymbols(request) | QueryRequestV1::Diagnostics(request) => {
            json(request)
        }
        QueryRequestV1::Unsupported(request) => json(request),
    }
}

fn ok_response<T: Serialize>(operation: String, result: T) -> Vec<u8> {
    json(&OkResponseV1 {
        schema: RESPONSE_SCHEMA_V1,
        version: 1,
        operation,
        status: "ok",
        result,
    })
    .into_bytes()
}

fn error_response(operation: &str, code: &str) -> Vec<u8> {
    json(&ErrorResponseV1 {
        schema: RESPONSE_SCHEMA_V1,
        version: 1,
        operation,
        status: "error",
        code,
    })
    .into_bytes()
}

fn unsupported_response(operation: &str) -> Vec<u8> {
    json(&UnsupportedResponseV1 {
        schema: RESPONSE_SCHEMA_V1,
        version: 1,
        operation,
        status: "unsupported",
        capability: operation,
    })
    .into_bytes()
}

fn json<T: Serialize>(value: &T) -> String {
    serde_json::to_string(value).expect("language-service response serializes") + "\n"
}

#[cfg(test)]
mod tests {
    use super::*;

    const PRODUCT: &[u8] = include_bytes!("../fixtures/tooling/query-snapshot-v1.json");

    fn snapshot() -> ToolingQuerySnapshotV1 {
        decode_tooling_query_snapshot_v1(PRODUCT).expect("frozen query product decodes")
    }

    fn request(operation: &str, fields: &str) -> Vec<u8> {
        format!(
            "{{\"schema\":\"{REQUEST_SCHEMA_V1}\",\"version\":1,\"operation\":\"{operation}\"{fields}}}\n"
        )
        .into_bytes()
    }

    fn response(product: &[u8], request: &[u8]) -> serde_json::Value {
        let bytes = query_snapshot_v1(product, request);
        assert!(bytes.ends_with(b"\n"));
        serde_json::from_slice(&bytes).expect("response is json")
    }

    #[test]
    fn l12c2_projects_only_records_present_in_a_strict_product() {
        let snapshot = snapshot();
        let source_unit_id = &snapshot.source_units[0].source_unit_id;
        let result = response(
            PRODUCT,
            &request(
                "position",
                &format!(",\"sourceUnitId\":\"{source_unit_id}\",\"offset\":114"),
            ),
        );
        let records = result["result"]["records"]
            .as_array()
            .expect("position returns records");
        let expected = snapshot
            .semantic_records
            .iter()
            .filter(|record| record.range.start <= 114 && 114 < record.range.end)
            .map(|record| serde_json::to_value(record).expect("record serializes"))
            .collect::<Vec<_>>();
        assert_eq!(records, &expected);
        assert_eq!(result["status"], "ok");
        assert_eq!(result["operation"], "position");
    }

    #[test]
    fn l12c2_projects_definition_references_symbols_and_diagnostics_without_inference() {
        let snapshot = snapshot();
        let source_unit_id = &snapshot.source_units[0].source_unit_id;
        let target = snapshot.references[0].target_query_semantic_id.clone();
        let definition = response(
            PRODUCT,
            &request("definition", &format!(",\"querySemanticId\":\"{target}\"")),
        );
        assert_eq!(definition["result"]["record"]["querySemanticId"], target);

        let references = response(
            PRODUCT,
            &request("references", &format!(",\"querySemanticId\":\"{target}\"")),
        );
        assert_eq!(
            references["result"]["references"].as_array().map(Vec::len),
            Some(1)
        );

        let symbols = response(
            PRODUCT,
            &request(
                "documentSymbols",
                &format!(",\"sourceUnitId\":\"{source_unit_id}\""),
            ),
        );
        assert_eq!(
            symbols["result"]["records"].as_array().map(Vec::len),
            Some(7)
        );

        let diagnostics = response(
            PRODUCT,
            &request(
                "diagnostics",
                &format!(",\"sourceUnitId\":\"{source_unit_id}\""),
            ),
        );
        assert_eq!(
            diagnostics["result"]["diagnostics"]
                .as_array()
                .map(Vec::len),
            Some(0)
        );
    }

    #[test]
    fn l12c2_rejects_noncanonical_or_invalid_products_before_requests() {
        let noncanonical_request = b"{\"schema\":\"wrong\"}\n";
        let invalid_product_response = response(b"{}\n", noncanonical_request);
        assert_eq!(invalid_product_response["operation"], "");
        assert_eq!(invalid_product_response["code"], "invalid_product");

        let snapshot = snapshot();
        let source_unit_id = &snapshot.source_units[0].source_unit_id;
        let invalid_identity = String::from_utf8(PRODUCT.to_vec())
            .expect("fixture is utf8")
            .replacen(
                "b95be6ce6ca9ad7c0b5a2cd25963e7fb40da1f8d76f4e171ef32ddaca9f2d4c2",
                "c95be6ce6ca9ad7c0b5a2cd25963e7fb40da1f8d76f4e171ef32ddaca9f2d4c2",
                1,
            );
        let invalid_identity_response = response(
            invalid_identity.as_bytes(),
            &request(
                "position",
                &format!(",\"sourceUnitId\":\"{source_unit_id}\",\"offset\":0"),
            ),
        );
        assert_eq!(invalid_identity_response["code"], "invalid_product");
    }

    #[test]
    fn l12c2_errors_and_unsupported_capabilities_are_stable() {
        let snapshot = snapshot();
        let source_unit_id = &snapshot.source_units[0].source_unit_id;
        let unknown_unit = response(
            PRODUCT,
            &request(
                "position",
                ",\"sourceUnitId\":\"source:missing\",\"offset\":0",
            ),
        );
        assert_eq!(unknown_unit["code"], "unknown_source_unit");

        let out_of_range = response(
            PRODUCT,
            &request(
                "position",
                &format!(",\"sourceUnitId\":\"{source_unit_id}\",\"offset\":140"),
            ),
        );
        assert_eq!(out_of_range["code"], "offset_out_of_range");

        let unknown_id = response(
            PRODUCT,
            &request(
                "definition",
                ",\"querySemanticId\":\"query-semantic:missing\"",
            ),
        );
        assert_eq!(unknown_id["code"], "unknown_query_semantic_id");

        for operation in [
            "hover",
            "rename",
            "completion",
            "signatureHelp",
            "semanticTokens",
            "sourceMapping",
            "edits",
            "codeActions",
        ] {
            let unsupported = response(PRODUCT, &request(operation, ""));
            assert_eq!(unsupported["status"], "unsupported");
            assert_eq!(unsupported["capability"], operation);
        }
    }

    #[test]
    fn l12c2_rejects_noncanonical_and_unknown_requests() {
        let snapshot = snapshot();
        let source_unit_id = &snapshot.source_units[0].source_unit_id;
        let mut noncanonical = request(
            "position",
            &format!(",\"sourceUnitId\":\"{source_unit_id}\",\"offset\":0"),
        );
        noncanonical.insert(0, b' ');
        let noncanonical_response = response(PRODUCT, &noncanonical);
        assert_eq!(noncanonical_response["code"], "invalid_request");
        assert_eq!(noncanonical_response["operation"], "position");

        let unknown = request("notAQuery", "");
        let unknown_response = response(PRODUCT, &unknown);
        assert_eq!(unknown_response["code"], "invalid_request");
        assert_eq!(unknown_response["operation"], "notAQuery");
    }
}