maplibre-expr 0.3.0

Pure-Rust parser and evaluator for MapLibre GL style expressions
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Conformance harness: runs the vendored `maplibre-style-spec` expression
//! fixtures, one libtest case per fixture directory.
//!
//! Each `test.json` carries an `expression`, a list of `inputs`, and the
//! `expected` compile result plus per-input `outputs`. For every fixture we:
//!
//!   1. parse the expression (checking success vs. compile-error), then
//!   2. evaluate it against each input and compare to the expected output,
//!      matching `{ "error": ... }` outputs against evaluation errors.
//!
//! Compilation is modeled as parse + [`typecheck`] (the latter fed the expected
//! type derived from the fixture's `propertySpec`). Fixtures listed in
//! `tests/known_failures.txt` are reported as *ignored* rather than failing —
//! that file is the running to-do list of operators and behaviours not yet
//! implemented. Nothing is skipped silently: the count of ignored fixtures is
//! printed by the runner and the list is under version control.
//!
//! ## Scope note
//!
//! This harness verifies `compiled.result` (success vs. error), the per-input
//! `outputs`, and error-message/location-`key` parity against the fixtures'
//! `expected.compiled.errors` / `outputs[i].error`. It does **not** assert the
//! other static-analysis fields (`type`, `isFeatureConstant`, `isZoomConstant`).
//! Run with `PARITY=1` for an error-parity coverage report instead.

use std::collections::BTreeMap;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

use libtest_mimic::{Arguments, Failed, Trial};
use maplibre_expr::convert::convert_function;
use maplibre_expr::{evaluate, parse, typecheck, EvaluationContext, Feature, Type, Value};
use serde_json::Value as Json;

/// Map a fixture's `propertySpec` to the expected expression [`Type`], so the
/// type-checker sees the same expectation MapLibre derives from the spec.
fn property_spec_type(spec: &Json) -> Option<Type> {
    let scalar = |t: &str| match t {
        "color" => Some(Type::Color),
        "number" => Some(Type::Number),
        "string" | "enum" => Some(Type::String),
        "boolean" => Some(Type::Boolean),
        "formatted" => Some(Type::Formatted),
        "resolvedImage" => Some(Type::ResolvedImage),
        "padding" => Some(Type::Padding),
        "numberArray" => Some(Type::NumberArray),
        "colorArray" => Some(Type::ColorArray),
        "projectionDefinition" => Some(Type::ProjectionDefinition),
        "variableAnchorOffsetCollection" => Some(Type::VariableAnchorOffsetCollection),
        _ => None,
    };
    match spec.get("type").and_then(Json::as_str)? {
        "array" => {
            let item = spec
                .get("value")
                .and_then(Json::as_str)
                .and_then(scalar)
                .unwrap_or(Type::Value);
            let n = spec
                .get("length")
                .and_then(Json::as_u64)
                .map(|v| v as usize);
            Some(Type::array(item, n))
        }
        other => scalar(other),
    }
}

// Legacy function objects (`{type, property, stops, ...}`) are converted to
// the equivalent modern expression before parsing. That conversion lives in
// the library's public `convert` module (`convert_function`, imported above),
// which this harness exercises directly against the upstream fixtures.

fn main() {
    let args = Arguments::from_args();

    let root = fixtures_root();
    let known = load_known_failures();
    let mut fixtures = Vec::new();
    collect(&root, &root, &mut fixtures);
    fixtures.sort();

    if std::env::var_os("PARITY").is_some() {
        run_parity(&fixtures);
        return;
    }

    let trials = fixtures
        .into_iter()
        .map(|(name, path)| {
            let ignored = known.contains(&name);
            let mut trial = Trial::test(name, move || run_fixture(&path));
            if ignored {
                trial = trial.with_ignored_flag(true);
            }
            trial
        })
        .collect();

    libtest_mimic::run(&args, trials).exit();
}

/// Assessment mode (`PARITY=1 cargo test --test spec`): instead of pass/fail,
/// measure how closely our error *text* and location *key* match the fixtures'
/// `expected.compiled.errors` (Tier B/C), and the per-input eval-error text.
fn run_parity(fixtures: &[(String, PathBuf)]) {
    // Compile-error parity.
    let mut ce_total = 0usize; // fixtures expecting a compile error
    let mut ce_raised = 0usize; // ... where we also raised one
    let mut ce_msg_exact = 0usize; // ... with byte-identical message
    let mut ce_key_present = 0usize; // ... whose expected key is non-empty
    let mut ce_key_exact = 0usize; // ... where our key matches
    let mut msg_mismatches: Vec<(String, String, String)> = Vec::new();
    let mut key_mismatches: Vec<(String, String, String)> = Vec::new();

    // Eval-error parity.
    let mut ee_total = 0usize;
    let mut ee_raised = 0usize;
    let mut ee_msg_exact = 0usize;
    let mut ee_mismatches: Vec<(String, String, String)> = Vec::new();

    for (name, path) in fixtures {
        let Ok(raw) = fs::read_to_string(path) else {
            continue;
        };
        let Ok(doc) = serde_json::from_str::<Json>(&raw) else {
            continue;
        };
        let Some(expression) = doc.get("expression") else {
            continue;
        };
        let expected = doc.get("expected").cloned().unwrap_or(Json::Null);
        let compiled_result = expected
            .get("compiled")
            .and_then(|c| c.get("result"))
            .and_then(Json::as_str)
            .unwrap_or("success");

        // Legacy stop-function objects are converted before parsing.
        let converted;
        let expression = if expression.is_object() {
            match doc.get("propertySpec") {
                Some(spec) => {
                    converted = convert_function(expression, spec);
                    &converted
                }
                None => expression,
            }
        } else {
            expression
        };

        let expected_type = doc.get("propertySpec").and_then(property_spec_type);
        let coerce_top_string = doc
            .get("propertySpec")
            .and_then(|s| s.get("type"))
            .and_then(Json::as_str)
            == Some("string");
        let compiled = parse(expression)
            .and_then(|e| typecheck(&e, expected_type.as_ref(), coerce_top_string));

        if compiled_result == "error" {
            ce_total += 1;
            let want = expected
                .get("compiled")
                .and_then(|c| c.get("errors"))
                .and_then(Json::as_array)
                .and_then(|a| a.first());
            let want_msg = want
                .and_then(|e| e.get("error"))
                .and_then(Json::as_str)
                .unwrap_or("");
            let want_key = want
                .and_then(|e| e.get("key"))
                .and_then(Json::as_str)
                .unwrap_or("");
            if let Err(e) = &compiled {
                ce_raised += 1;
                let got_msg = e.to_string();
                if got_msg == want_msg {
                    ce_msg_exact += 1;
                } else {
                    msg_mismatches.push((name.clone(), want_msg.to_string(), got_msg));
                }
                if !want_key.is_empty() {
                    ce_key_present += 1;
                    if e.key == want_key {
                        ce_key_exact += 1;
                    } else {
                        key_mismatches.push((name.clone(), want_key.to_string(), e.key.clone()));
                    }
                }
            }
            continue;
        }

        // Successful compile: measure eval-error text against `{ "error": ... }`.
        let Ok(expr) = compiled else { continue };
        let empty = Vec::new();
        let inputs = doc.get("inputs").and_then(Json::as_array).unwrap_or(&empty);
        let outputs = expected
            .get("outputs")
            .and_then(Json::as_array)
            .cloned()
            .unwrap_or_default();
        let global_state: BTreeMap<String, Value> = doc
            .get("globalState")
            .and_then(Json::as_object)
            .map(|o| {
                o.iter()
                    .map(|(k, v)| (k.clone(), Value::from_json(v)))
                    .collect()
            })
            .unwrap_or_default();
        for (i, input) in inputs.iter().enumerate() {
            let Some(want_msg) = outputs
                .get(i)
                .and_then(|o| o.get("error"))
                .and_then(Json::as_str)
            else {
                continue;
            };
            ee_total += 1;
            let Ok(ctx) = build_context(input) else {
                continue;
            };
            let ctx = ctx.with_global_state(global_state.clone());
            if let Err(e) = evaluate(&expr, &ctx) {
                ee_raised += 1;
                let got = e.to_string();
                if got == want_msg {
                    ee_msg_exact += 1;
                } else {
                    ee_mismatches.push((name.clone(), want_msg.to_string(), got));
                }
            }
        }
    }

    let pct = |n: usize, d: usize| {
        if d == 0 {
            100.0
        } else {
            100.0 * n as f64 / d as f64
        }
    };
    println!("\n=== Error-parity assessment ===\n");
    println!("Compile errors (Tier B = message, Tier C = key):");
    println!("  fixtures expecting a compile error : {ce_total}");
    println!(
        "  error raised by us                 : {ce_raised} ({:.1}%)",
        pct(ce_raised, ce_total)
    );
    println!(
        "  message byte-identical             : {ce_msg_exact} / {ce_total} ({:.1}%)",
        pct(ce_msg_exact, ce_total)
    );
    println!(
        "  location key matches               : {ce_key_exact} / {ce_key_present} non-empty keys ({:.1}%)",
        pct(ce_key_exact, ce_key_present)
    );
    println!("\nEval errors:");
    println!("  outputs expecting an error         : {ee_total}");
    println!(
        "  error raised by us                 : {ee_raised} ({:.1}%)",
        pct(ee_raised, ee_total)
    );
    println!(
        "  message byte-identical             : {ee_msg_exact} / {ee_total} ({:.1}%)",
        pct(ee_msg_exact, ee_total)
    );

    let show = |title: &str, v: &[(String, String, String)], limit: usize| {
        println!("\n--- {title} ({} total) ---", v.len());
        for (name, want, got) in v.iter().take(limit) {
            println!("  [{name}]\n    want: {want}\n    got : {got}");
        }
        if v.len() > limit {
            println!("  ... and {} more", v.len() - limit);
        }
    };
    show("compile message mismatches", &msg_mismatches, 60);
    show("location key mismatches", &key_mismatches, 40);
    show("eval message mismatches", &ee_mismatches, 40);
}

fn fixtures_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("expression")
}

fn load_known_failures() -> HashSet<String> {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("known_failures.txt");
    let Ok(contents) = fs::read_to_string(path) else {
        return HashSet::new();
    };
    contents
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty() && !l.starts_with('#'))
        .map(String::from)
        .collect()
}

/// Recursively find every `test.json`, naming each by its path relative to the
/// fixtures root (e.g. `interpolate/linear`).
fn collect(root: &Path, dir: &Path, out: &mut Vec<(String, PathBuf)>) {
    let Ok(entries) = fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            collect(root, &path, out);
        } else if path.file_name().and_then(|n| n.to_str()) == Some("test.json") {
            let name = path
                .parent()
                .unwrap()
                .strip_prefix(root)
                .unwrap()
                .to_string_lossy()
                .replace('\\', "/");
            out.push((name, path));
        }
    }
}

fn run_fixture(path: &Path) -> Result<(), Failed> {
    let raw = fs::read_to_string(path).map_err(|e| format!("cannot read fixture: {e}"))?;
    let doc: Json = serde_json::from_str(&raw).map_err(|e| format!("invalid fixture json: {e}"))?;

    let expression = doc
        .get("expression")
        .ok_or("fixture missing \"expression\"")?;
    let expected = doc.get("expected").ok_or("fixture missing \"expected\"")?;
    let compiled_result = expected
        .get("compiled")
        .and_then(|c| c.get("result"))
        .and_then(Json::as_str)
        .unwrap_or("success");

    // A legacy function object (`{type, property, stops, ...}`) is converted to
    // the equivalent modern expression before parsing.
    let converted;
    let expression = if expression.is_object() {
        match doc.get("propertySpec") {
            Some(spec) => {
                converted = convert_function(expression, spec);
                &converted
            }
            None => expression,
        }
    } else {
        expression
    };

    // A fixture "compiles" if it both parses and type-checks. The expected
    // type comes from the property spec, when present. Type checking returns the
    // annotated tree (with coercion/assertion nodes) that we then evaluate.
    let expected_type = doc.get("propertySpec").and_then(property_spec_type);
    let coerce_top_string = doc
        .get("propertySpec")
        .and_then(|s| s.get("type"))
        .and_then(Json::as_str)
        == Some("string");
    let compiled = parse(expression)
        .and_then(|expr| typecheck(&expr, expected_type.as_ref(), coerce_top_string));

    if compiled_result == "error" {
        return match compiled {
            // Also enforce message + location-key parity against the first
            // expected error (see the PARITY assessment mode).
            Err(e) => {
                let want = expected
                    .get("compiled")
                    .and_then(|c| c.get("errors"))
                    .and_then(Json::as_array)
                    .and_then(|a| a.first());
                if let Some(wmsg) = want.and_then(|w| w.get("error")).and_then(Json::as_str) {
                    if e.to_string() != wmsg {
                        return Err(format!(
                            "error message mismatch:\n  want: {wmsg}\n  got:  {e}"
                        )
                        .into());
                    }
                }
                if let Some(wkey) = want.and_then(|w| w.get("key")).and_then(Json::as_str) {
                    if e.key != wkey {
                        return Err(
                            format!("error key mismatch: want {wkey:?}, got {:?}", e.key).into(),
                        );
                    }
                }
                Ok(())
            }
            Ok(_) => {
                Err("expected a compile error, but the expression compiled successfully".into())
            }
        };
    }

    let expr = compiled.map_err(|e| format!("expected successful compile, but failed: {e}"))?;

    let empty = Vec::new();
    let inputs = doc.get("inputs").and_then(Json::as_array).unwrap_or(&empty);
    let outputs = expected
        .get("outputs")
        .and_then(Json::as_array)
        .cloned()
        .unwrap_or_default();

    // `globalState` is a fixture-level map shared across all inputs.
    let global_state: BTreeMap<String, Value> = doc
        .get("globalState")
        .and_then(Json::as_object)
        .map(|o| {
            o.iter()
                .map(|(k, v)| (k.clone(), Value::from_json(v)))
                .collect()
        })
        .unwrap_or_default();

    for (i, input) in inputs.iter().enumerate() {
        let ctx = build_context(input)?.with_global_state(global_state.clone());
        let expected_output = outputs
            .get(i)
            .ok_or_else(|| format!("input #{i} has no expected output"))?;

        match evaluate(&expr, &ctx) {
            Ok(value) => {
                if let Some(err_obj) = expected_output.get("error") {
                    return Err(format!(
                        "input #{i}: expected evaluation error ({err_obj}), got value {:?}",
                        value
                    )
                    .into());
                }
                let actual = value_to_json(&value);
                if !json_close(&actual, expected_output) {
                    return Err(
                        format!("input #{i}: expected {expected_output}, got {actual}").into(),
                    );
                }
            }
            Err(e) => match expected_output.get("error").and_then(Json::as_str) {
                None => {
                    return Err(format!(
                        "input #{i}: expected {expected_output}, got evaluation error: {e}"
                    )
                    .into());
                }
                // Enforce evaluation-error message parity.
                Some(want) if e.to_string() != want => {
                    return Err(format!(
                        "input #{i}: error message mismatch:\n  want: {want}\n  got:  {e}"
                    )
                    .into());
                }
                Some(_) => {}
            },
        }
    }

    Ok(())
}

/// Build an [`EvaluationContext`] from a fixture input `[globals, feature]`.
fn build_context(input: &Json) -> Result<EvaluationContext, Failed> {
    let items = input
        .as_array()
        .ok_or("each input must be a [globals, feature] array")?;

    let mut ctx = EvaluationContext::new();
    if let Some(zoom) = items
        .first()
        .and_then(|g| g.get("zoom"))
        .and_then(Json::as_f64)
    {
        ctx.zoom = Some(zoom);
    }
    if let Some(images) = items
        .first()
        .and_then(|g| g.get("availableImages"))
        .and_then(Json::as_array)
    {
        ctx.available_images = images
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
    }
    if let Some(c) = items.first().and_then(|g| g.get("canonicalID")) {
        let n = |k| c.get(k).and_then(Json::as_u64).map(|v| v as u32);
        if let (Some(z), Some(x), Some(y)) = (n("z"), n("x"), n("y")) {
            ctx.canonical = Some((z, x, y));
        }
    }
    let global_num = |k| items.first().and_then(|g| g.get(k)).and_then(Json::as_f64);
    ctx.heatmap_density = global_num("heatmapDensity");
    ctx.elevation = global_num("elevation");
    ctx.line_progress = global_num("lineProgress");

    if let Some(feature_json) = items.get(1) {
        ctx.feature = build_feature(feature_json);
    }
    Ok(ctx)
}

fn build_feature(json: &Json) -> Feature {
    let mut feature = Feature::default();

    if let Some(props) = json.get("properties").and_then(Json::as_object) {
        feature.properties = props
            .iter()
            .map(|(k, v)| (k.clone(), Value::from_json(v)))
            .collect::<BTreeMap<_, _>>();
    }
    if let Some(id) = json.get("id") {
        if !id.is_null() {
            feature.id = Some(Value::from_json(id));
        }
    }
    if let Some(state) = json.get("featureState").and_then(Json::as_object) {
        feature.state = state
            .iter()
            .map(|(k, v)| (k.clone(), Value::from_json(v)))
            .collect::<BTreeMap<_, _>>();
    }
    feature.geometry_type = geometry_type(json);
    if let Some(geom) = json.get("geometry") {
        feature.geometry = extract_geometry(geom);
    }
    feature
}

fn geometry_type(json: &Json) -> Option<String> {
    // Normalize to the vector-tile geometry class (Point/LineString/Polygon).
    if let Some(t) = json
        .get("geometry")
        .and_then(|g| g.get("type"))
        .and_then(Json::as_str)
    {
        return Some(
            match t {
                "Point" | "MultiPoint" => "Point",
                "LineString" | "MultiLineString" => "LineString",
                "Polygon" | "MultiPolygon" => "Polygon",
                other => other,
            }
            .to_string(),
        );
    }
    match json.get("type") {
        Some(Json::String(s)) => Some(s.clone()),
        Some(Json::Number(n)) => match n.as_u64() {
            Some(1) => Some("Point".into()),
            Some(2) => Some("LineString".into()),
            Some(3) => Some("Polygon".into()),
            _ => None,
        },
        _ => None,
    }
}

/// Extract the feature geometry as raw `[lng, lat]` groups (rings / lines).
fn extract_geometry(geom: &Json) -> Vec<Vec<(f64, f64)>> {
    let pt = |c: &Json| -> Option<(f64, f64)> {
        let a = c.as_array()?;
        Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?))
    };
    let line = |c: &Json| -> Vec<(f64, f64)> {
        c.as_array()
            .map(|a| a.iter().filter_map(pt).collect())
            .unwrap_or_default()
    };
    let coords = geom.get("coordinates");
    match geom.get("type").and_then(Json::as_str) {
        Some("Point") => coords
            .and_then(pt)
            .map(|p| vec![vec![p]])
            .unwrap_or_default(),
        Some("MultiPoint") => coords
            .and_then(Json::as_array)
            .map(|a| a.iter().filter_map(pt).map(|p| vec![p]).collect())
            .unwrap_or_default(),
        Some("LineString") => coords.map(|c| vec![line(c)]).unwrap_or_default(),
        Some("MultiLineString") | Some("Polygon") => coords
            .and_then(Json::as_array)
            .map(|a| a.iter().map(line).collect())
            .unwrap_or_default(),
        Some("MultiPolygon") => coords
            .and_then(Json::as_array)
            .map(|polys| {
                polys
                    .iter()
                    .filter_map(Json::as_array)
                    .flatten()
                    .map(line)
                    .collect()
            })
            .unwrap_or_default(),
        _ => Vec::new(),
    }
}

/// Serialize a [`Value`] to JSON using the same representation the spec
/// fixtures use for expected outputs.
fn value_to_json(value: &Value) -> Json {
    match value {
        Value::Null => Json::Null,
        Value::Bool(b) => Json::Bool(*b),
        Value::Number(n) => serde_json::json!(n),
        Value::String(s) => Json::String(s.clone()),
        // Colors are compared as normalized [r, g, b, a] arrays.
        Value::Color(c) => Json::Array(
            c.to_rgba_unit()
                .iter()
                .map(|n| serde_json::json!(n))
                .collect(),
        ),
        Value::Array(a) => Json::Array(a.iter().map(value_to_json).collect()),
        Value::Object(o) => Json::Object(
            o.iter()
                .map(|(k, v)| (k.clone(), value_to_json(v)))
                .collect(),
        ),
        Value::Image { name, available } => {
            serde_json::json!({ "name": name, "available": available })
        }
        Value::NumberArray(v) => serde_json::json!({ "values": v }),
        Value::Padding(v) => serde_json::json!({ "values": v }),
        Value::ColorArray(v) => {
            let colors: Vec<Json> = v
                .iter()
                .map(|c| serde_json::json!({"r": c.r, "g": c.g, "b": c.b, "a": c.a}))
                .collect();
            serde_json::json!({ "values": colors })
        }
        Value::Projection(p) => match p {
            maplibre_expr::Projection::Named(s) => Json::String(s.clone()),
            maplibre_expr::Projection::Transition {
                from,
                to,
                transition,
            } => serde_json::json!({ "from": from, "to": to, "transition": transition }),
        },
        // A collator is never a direct output value.
        Value::Collator { .. } => Json::Null,
        Value::Formatted(sections) => {
            let secs: Vec<Json> = sections
                .iter()
                .map(|s| {
                    serde_json::json!({
                        "text": s.text,
                        "image": s.image.as_ref().map(|(n, a)| serde_json::json!({"name": n, "available": a})),
                        "scale": s.scale,
                        "fontStack": s.font_stack,
                        "textColor": s.text_color.map(|c| serde_json::json!({"r": c.r, "g": c.g, "b": c.b, "a": c.a})),
                        "verticalAlign": s.vertical_align,
                    })
                })
                .collect();
            serde_json::json!({ "sections": secs })
        }
    }
}

/// Structural JSON equality that mirrors the upstream harness: numbers are
/// reduced to 6 significant decimal figures (see [`strip_precision`]) before
/// being compared, matching `deepEqual`/`stripPrecision` in the spec repo's
/// `test/lib/json-diff.ts`.
fn json_close(a: &Json, b: &Json) -> bool {
    match (a, b) {
        (Json::Number(x), Json::Number(y)) => {
            let (x, y) = (
                x.as_f64().unwrap_or(f64::NAN),
                y.as_f64().unwrap_or(f64::NAN),
            );
            if x.is_nan() || y.is_nan() {
                return x.is_nan() && y.is_nan();
            }
            let (sx, sy) = (strip_precision(x, 6), strip_precision(y, 6));
            (sx - sy).abs() <= 1e-9 * sx.abs().max(1.0)
        }
        (Json::Array(x), Json::Array(y)) => {
            x.len() == y.len() && x.iter().zip(y).all(|(a, b)| json_close(a, b))
        }
        (Json::Object(x), Json::Object(y)) => {
            x.len() == y.len()
                && x.iter()
                    .all(|(k, v)| y.get(k).is_some_and(|w| json_close(v, w)))
        }
        _ => a == b,
    }
}

/// Reduce `x` to `sig` significant decimal figures by truncation, matching
/// upstream `stripPrecision`. The double-floor guards against a value that is
/// already stripped drifting under floating-point rounding.
fn strip_precision(x: f64, sig: i32) -> f64 {
    if x == 0.0 {
        return 0.0;
    }
    let multiplier = 10f64.powf((sig as f64 - x.abs().log10().ceil()).max(0.0));
    let first = (x * multiplier).floor() / multiplier;
    (first * multiplier).floor() / multiplier
}