jshift 0.6.0

Schema-guided typed JSON on raw bytes: path get/mutate, open views, project — without a Value tree.
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
//! Field projection: keep-list / JMESPath subset → smaller JSON.
//!
//! # Architecture
//!
//! | Layer | Module | Role |
//! | --- | --- | --- |
//! | AST | [`select`] | [`SelectExpr`] (identity, object, array, multi-select, pipe, flatten, …) |
//! | Paths | [`jmespath`] | keep-list paths + JMESPath **subset** parser (byte-oriented, not a DOM port) |
//! | Plan | [`plan`] | [`ProjectPlan`], styles, missing policy |
//! | Focus | [`focus`] | span/arena model: pure projections never copy trees |
//! | Emit | [`emit`] | one-pass writer; index-accelerated hops; PreserveSource tree replay |
//!
//! ## Byte-oriented JMESPath (not jmespath.rs)
//!
//! Evaluation walks **raw JSON bytes** (`&[u8]` spans). Pure field/index chains and
//! pipes of pure paths resolve to document spans with **zero intermediate tree
//! copies**. Only synthesized values (multi-select, many functions, flatten of
//! reconstructed arrays) materialize into a reclaimable buffer. This is the same
//! design as jshift’s path engine — not a port of
//! [jmespath.rs](https://github.com/jmespath/jmespath.rs) (which builds a
//! `Variable` DOM).
//!
//! ## Index-wired project
//!
//! [`project_indexed`] passes [`crate::IndexedDocument`] side-tables into emit:
//! array element hops and object key lookups use O(1) tables when the open `[`/`{`
//! offset matches an indexed container.
//!
//! ## Streaming cards / JSONL (no giant output array)
//!
//! [`project_each`] / [`project_object_fields_each`] project **one element at a time**
//! into a reusable card buffer and invoke a callback — peak RAM tracks one card, not
//! the full `[card,card,…]` result. List shapes include `[*]`, filters (`[?…]`), and
//! slices. [`project_jsonl_write`] / [`project_object_fields_jsonl_write`] write NDJSON
//! (one JSON object per line) to any [`std::io::Write`].
//!
//! ## Parallel auto-pick
//!
//! [`project_indexed_auto`] (and [`project_parallel_auto`]) enable Rayon list emit only
//! when [`plan_prefers_parallel`] says the per-element work is likely CPU-bound
//! (filters, functions, nested list work). Thin pure-field multi-select cards stay
//! sequential — they are usually memory-bound and parallel can lose.
//!
//! JMESPath compliance: see `tests/jmespath_compliance.rs` (tier A + full suite
//! with **zero residuals** on vendored official fixtures, excluding benchmarks).
//! Null semantics: missing paths / failed projections emit JSON `null` under
//! default [`MissingPolicy::Skip`]; hard failures use [`crate::Error::Jmespath`].

mod emit;
mod focus;
mod jmespath;
mod plan;
mod select;
mod sink;
mod transform;

pub use jmespath::{parse_jmespath_expr, parse_project_path, select_from_project_path};
pub use plan::{MissingPolicy, ProjectPlan, ProjectStyle};
pub use select::{
    ArraySelect, CmpOp, HashField, ObjectSelect, ProjectPathSegment, SelectExpr,
};
pub use sink::{CountingSink, WriteSink};
pub use transform::{Transform, TransformPipeline};

use std::io::Write;

use crate::error::Error;
use crate::index::IndexedDocument;
use crate::path::{parse_path, PathSegment};
use crate::scan::{find_value, find_value_offsets, skip_value, skip_whitespace};

use sink::{CountingSink as CountSink, EmitOut, WriteSink as WSink};

/// Project `json` according to `plan`, returning a new buffer.
///
/// ```
/// use jshift::{project, ProjectPlan};
///
/// let json = br#"{"id":7,"title":"Hat","images":[1,2,3]}"#;
/// let plan = ProjectPlan::from_paths(&["id", "title"]).unwrap();
/// assert_eq!(project(json, &plan).unwrap(), br#"{"id":7,"title":"Hat"}"#);
/// ```
pub fn project(json: &[u8], plan: &ProjectPlan) -> Result<Vec<u8>, Error> {
    let mut out = Vec::new();
    project_into(json, plan, &mut out)?;
    Ok(out)
}

/// Project into an existing buffer (appends).
pub fn project_into(json: &[u8], plan: &ProjectPlan, out: &mut Vec<u8>) -> Result<(), Error> {
    project_to_sink(json, plan, out)
}

/// Project using a pre-built [`IndexedDocument`].
///
/// Array and object side-tables (and Stage-1 structurals when present) accelerate
/// hops during emit: element access is O(1) for indexed arrays, key lookup is O(1)
/// for indexed objects. Share one index build across many `project_indexed` calls
/// on the same snapshot.
///
/// Prefer [`IndexedDocument::index_for_plan`] (or [`project_auto_indexed`]) so array
/// paths from the plan are side-tabled before projecting mid/last indices.
///
/// Evaluation still uses the span/arena model: pure projections never copy trees;
/// only synthesized values (multi-select, function results) touch the arena.
pub fn project_indexed(doc: &IndexedDocument<'_>, plan: &ProjectPlan) -> Result<Vec<u8>, Error> {
    let mut out = Vec::new();
    project_to_sink_indexed(doc, plan, &mut out)?;
    Ok(out)
}

/// Ensure `doc` has side-tables for the plan's array paths, then project.
///
/// Mutates `doc` (idempotent indexing). Ideal when reusing one snapshot for many plans
/// or many `project_indexed` calls after a single prepare.
pub fn project_indexed_prepare(
    doc: &mut IndexedDocument<'_>,
    plan: &ProjectPlan,
) -> Result<Vec<u8>, Error> {
    doc.index_for_plan(plan)?;
    project_indexed(doc, plan)
}

/// Build structural + plan array indexes, then project (one-shot convenience).
///
/// Equivalent to:
/// ```ignore
/// let mut doc = IndexedDocument::empty(json);
/// doc.index_for_plan(plan)?;
/// project_indexed(&doc, plan)
/// ```
///
/// For many projections on the same buffer, build once with
/// [`IndexedDocument::index_for_plan`] and call [`project_indexed`] repeatedly.
pub fn project_auto_indexed(json: &[u8], plan: &ProjectPlan) -> Result<Vec<u8>, Error> {
    let mut doc = IndexedDocument::empty(json);
    doc.index_for_plan(plan)?;
    project_indexed(&doc, plan)
}

/// Generic “thin cards” helper: project each object in an array down to named fields.
///
/// Works for **any** document shape — only the path to the array and field names matter
/// (e.g. `"products"`, `"data.rows"`, `"items"`, or `""` for a root array). Not tied to
/// any particular catalog or domain.
///
/// ```
/// use jshift::project_object_fields;
///
/// let json = br#"{"items":[{"id":1,"t":"a","x":9},{"id":2,"t":"b","x":8}]}"#;
/// let out = project_object_fields(json, "items", &["id", "t"]).unwrap();
/// assert_eq!(out, br#"[{"id":1,"t":"a"},{"id":2,"t":"b"}]"#);
/// ```
pub fn project_object_fields(
    json: &[u8],
    array_path: &str,
    fields: &[&str],
) -> Result<Vec<u8>, Error> {
    let plan = plan_object_fields(array_path, fields)?;
    project(json, &plan)
}

/// Build a [`ProjectPlan`] for [`project_object_fields`] (array of objects → multi-select hash).
pub fn plan_object_fields(array_path: &str, fields: &[&str]) -> Result<ProjectPlan, Error> {
    if fields.is_empty() {
        return Err(Error::InvalidPath {
            msg: "project_object_fields requires at least one field name",
        });
    }
    let multi = fields
        .iter()
        .map(|f| format!("{f}: {f}"))
        .collect::<Vec<_>>()
        .join(", ");
    let expr = if array_path.is_empty() || array_path == "@" {
        format!("[*].{{{multi}}}")
    } else {
        format!("{array_path}[*].{{{multi}}}")
    };
    ProjectPlan::from_jmespath(&expr)
}

/// Parallel list projection using Rayon (requires feature `parallel`).
///
/// Builds plan indexes, then projects. Large `[*]` / keep-list array walks use
/// side-table chunking across threads when beneficial. Still `#![forbid(unsafe_code)]`.
#[cfg(feature = "parallel")]
pub fn project_parallel(json: &[u8], plan: &ProjectPlan) -> Result<Vec<u8>, Error> {
    let mut doc = IndexedDocument::empty(json);
    doc.index_for_plan(plan)?;
    project_indexed_parallel(&doc, plan)
}

/// Like [`project_indexed`], but enables parallel array `[*]` emission when a side-table
/// is present and the array is large enough (feature `parallel`).
#[cfg(feature = "parallel")]
pub fn project_indexed_parallel(
    doc: &IndexedDocument<'_>,
    plan: &ProjectPlan,
) -> Result<Vec<u8>, Error> {
    let mut out = Vec::new();
    let start = skip_whitespace(doc.as_bytes(), 0);
    if start >= doc.as_bytes().len() {
        return Err(Error::InvalidJsonSyntax {
            pos: start,
            msg: "Unexpected EOF",
        });
    }
    let end = match plan.select() {
        SelectExpr::Identity | SelectExpr::Current => skip_value(doc.as_bytes(), start)?,
        _ => {
            let mut end = doc.as_bytes().len();
            while end > start && matches!(doc.as_bytes()[end - 1], b' ' | b'\t' | b'\n' | b'\r') {
                end -= 1;
            }
            end
        }
    };
    let mut ctx = emit::EmitCtx::new(plan, Some(doc));
    #[cfg(feature = "parallel")]
    {
        ctx.allow_parallel = true;
    }
    emit::emit_value(doc.as_bytes(), start, end, plan.select(), &mut ctx, &mut out)?;
    Ok(out)
}

/// Parallel variant of [`project_object_fields`] (feature `parallel`).
#[cfg(feature = "parallel")]
pub fn project_object_fields_parallel(
    json: &[u8],
    array_path: &str,
    fields: &[&str],
) -> Result<Vec<u8>, Error> {
    let plan = plan_object_fields(array_path, fields)?;
    project_parallel(json, &plan)
}

// ── Streaming cards / JSONL (P3) ─────────────────────────────────────────────

/// Project each element of a **list projection** plan as a separate card.
///
/// `plan` must be a list projection (optionally under a field path):
/// - `[*]` — every element (`items[*].{id: id, title: title}`, root `[*].{a: a}`)
/// - filter — e.g. `items[?ok].{id: id}`
/// - slice — e.g. `items[0:10].{id: id}`
///
/// Each card is evaluated into a reusable buffer; **`f(index, card_bytes)`** is
/// called once per kept element. Null results are omitted (JMESPath list-projection
/// semantics). Multi-index projections (e.g. `items[0, 2]`) are not supported here —
/// use [`project`] / [`project_jmespath`].
///
/// Peak memory is **one card** plus the input document — not a giant
/// `[card,card,…]` output array. For NDJSON files see [`project_jsonl_write`].
///
/// ```
/// use jshift::{project_each, ProjectPlan};
///
/// let json = br#"{"items":[{"id":1,"t":"a","x":9},{"id":2,"t":"b"}]}"#;
/// let plan = ProjectPlan::from_jmespath("items[*].{id: id, t: t}").unwrap();
/// let mut cards = Vec::new();
/// project_each(json, &plan, |_, card| {
///     cards.push(card.to_vec());
///     Ok(())
/// }).unwrap();
/// assert_eq!(cards[0], br#"{"id":1,"t":"a"}"#);
/// assert_eq!(cards[1], br#"{"id":2,"t":"b"}"#);
/// ```
pub fn project_each<F>(json: &[u8], plan: &ProjectPlan, f: F) -> Result<(), Error>
where
    F: FnMut(usize, &[u8]) -> Result<(), Error>,
{
    project_each_inner(json, plan, None, f)
}

/// Like [`project_each`], using side-tables from `doc` when the array path is indexed.
pub fn project_each_indexed<F>(
    doc: &IndexedDocument<'_>,
    plan: &ProjectPlan,
    f: F,
) -> Result<(), Error>
where
    F: FnMut(usize, &[u8]) -> Result<(), Error>,
{
    project_each_inner(doc.as_bytes(), plan, Some(doc), f)
}

/// Thin-card variant: project named fields on each object in `array_path`, one callback
/// per element (no giant result array).
///
/// Domain-agnostic — any array path (`""` for root, `"items"`, `"data.rows"`, …).
///
/// ```
/// use jshift::project_object_fields_each;
///
/// let json = br#"[{"id":1,"t":"a","noise":true},{"id":2,"t":"b"}]"#;
/// let mut n = 0usize;
/// project_object_fields_each(json, "", &["id", "t"], |_, card| {
///     n += 1;
///     assert!(card.starts_with(b"{"));
///     Ok(())
/// }).unwrap();
/// assert_eq!(n, 2);
/// ```
pub fn project_object_fields_each<F>(
    json: &[u8],
    array_path: &str,
    fields: &[&str],
    f: F,
) -> Result<(), Error>
where
    F: FnMut(usize, &[u8]) -> Result<(), Error>,
{
    let plan = plan_object_fields(array_path, fields)?;
    project_each(json, &plan, f)
}

/// Indexed thin-card each-callback (prefer when reusing an [`IndexedDocument`]).
pub fn project_object_fields_each_indexed<F>(
    doc: &IndexedDocument<'_>,
    array_path: &str,
    fields: &[&str],
    f: F,
) -> Result<(), Error>
where
    F: FnMut(usize, &[u8]) -> Result<(), Error>,
{
    let plan = plan_object_fields(array_path, fields)?;
    project_each_indexed(doc, &plan, f)
}

/// Write each list-projection card as one NDJSON line (JSON object + newline) to `w`.
///
/// Never materializes the full projected array — only one card buffer is held.
/// Returns total bytes written (including newlines).
///
/// ```
/// use jshift::{project_jsonl_write, ProjectPlan};
///
/// let json = br#"{"rows":[{"a":1,"b":2},{"a":3,"b":4}]}"#;
/// let plan = ProjectPlan::from_jmespath("rows[*].{a: a}").unwrap();
/// let mut out = Vec::new();
/// let n = project_jsonl_write(json, &plan, &mut out).unwrap();
/// assert_eq!(&out[..], b"{\"a\":1}\n{\"a\":3}\n");
/// assert_eq!(n, out.len());
/// ```
pub fn project_jsonl_write<W: Write>(
    json: &[u8],
    plan: &ProjectPlan,
    mut w: W,
) -> Result<usize, Error> {
    let mut written = 0usize;
    project_each(json, plan, |_, card| {
        write_card_line(&mut w, card, &mut written)
    })?;
    Ok(written)
}

/// NDJSON thin cards: each object in `array_path` → one line with selected fields.
pub fn project_object_fields_jsonl_write<W: Write>(
    json: &[u8],
    array_path: &str,
    fields: &[&str],
    w: W,
) -> Result<usize, Error> {
    let plan = plan_object_fields(array_path, fields)?;
    project_jsonl_write(json, &plan, w)
}

/// Indexed NDJSON list projection (reuse side-tables).
pub fn project_jsonl_write_indexed<W: Write>(
    doc: &IndexedDocument<'_>,
    plan: &ProjectPlan,
    mut w: W,
) -> Result<usize, Error> {
    let mut written = 0usize;
    project_each_indexed(doc, plan, |_, card| {
        write_card_line(&mut w, card, &mut written)
    })?;
    Ok(written)
}

fn write_card_line<W: Write>(w: &mut W, card: &[u8], written: &mut usize) -> Result<(), Error> {
    w.write_all(card).map_err(|_| Error::InvalidJsonSyntax {
        pos: 0,
        msg: "Write error while emitting JSONL card",
    })?;
    w.write_all(b"\n").map_err(|_| Error::InvalidJsonSyntax {
        pos: 0,
        msg: "Write error while emitting JSONL newline",
    })?;
    *written = written.saturating_add(card.len()).saturating_add(1);
    Ok(())
}

fn project_each_inner<F>(
    json: &[u8],
    plan: &ProjectPlan,
    index: Option<&IndexedDocument<'_>>,
    f: F,
) -> Result<(), Error>
where
    F: FnMut(usize, &[u8]) -> Result<(), Error>,
{
    let (array_path, list_sel) = peel_list_proj(plan.select()).ok_or(Error::Jmespath {
        msg: "project_each requires a list projection plan (e.g. items[*].{…}, items[?…], items[0:n], or [*].…)",
    })?;
    let each = list_each_expr(list_sel).ok_or(Error::Jmespath {
        msg: "project_each does not support multi-index list projections",
    })?;

    let (open, end) = resolve_array_open_end(json, &array_path, index)?;
    let mut ctx = emit::EmitCtx::new(plan, index);
    emit::for_each_projected_card(json, open, end, list_sel, each, &mut ctx, f)
}

fn resolve_array_open_end(
    json: &[u8],
    array_path: &str,
    index: Option<&IndexedDocument<'_>>,
) -> Result<(usize, usize), Error> {
    let (start, end) = if array_path.is_empty() {
        let start = skip_whitespace(json, 0);
        let end = skip_value(json, start)?;
        (start, end)
    } else {
        let segs = parse_path(array_path);
        if let Some(doc) = index {
            match doc.find_offsets(&segs) {
                Ok(span) => span,
                Err(_) => find_value_offsets(json, &segs)?,
            }
        } else {
            find_value_offsets(json, &segs)?
        }
    };
    let open = skip_whitespace(json, start);
    if open >= json.len() || json[open] != b'[' {
        return Err(Error::TypeMismatch {
            expected: "array",
            found: match json.get(open).copied() {
                Some(b'{') => "object",
                Some(b'"') => "string",
                Some(b't') | Some(b'f') => "bool",
                Some(b'n') => "null",
                Some(b'-') | Some(b'0'..=b'9') => "number",
                _ => "unknown",
            },
        });
    }
    Ok((open, end))
}

/// Peel `path[*|?|slice].card` into `(array_path, list ArraySelect)`.
///
/// Supports nested field chains (`meta.rows[*].…`). Returns `None` for flatten `[]`,
/// multi-select lists, pure multi-index roots, or non-list roots.
fn peel_list_proj(expr: &SelectExpr) -> Option<(String, &ArraySelect)> {
    match expr {
        SelectExpr::Paren(inner) => peel_list_proj(inner),
        SelectExpr::Array(sel) if is_streamable_list(sel) => Some((String::new(), sel)),
        SelectExpr::Sub(left, right) => match right.as_ref() {
            SelectExpr::Array(sel) if is_streamable_list(sel) => {
                let path = field_chain_path(left)?;
                Some((path, sel))
            }
            SelectExpr::Sub(_, _) | SelectExpr::Paren(_) | SelectExpr::Array(_) => {
                let (suffix, sel) = peel_list_proj(right)?;
                let prefix = field_chain_path(left)?;
                let full = join_dot_path(&prefix, &suffix);
                Some((full, sel))
            }
            _ => None,
        },
        _ => None,
    }
}

fn is_streamable_list(sel: &ArraySelect) -> bool {
    matches!(
        sel,
        ArraySelect::Each(_) | ArraySelect::Filter { .. } | ArraySelect::Slice { .. }
    )
}

fn list_each_expr(sel: &ArraySelect) -> Option<&SelectExpr> {
    match sel {
        ArraySelect::Each(each)
        | ArraySelect::Filter { each, .. }
        | ArraySelect::Slice { each, .. } => Some(each.as_ref()),
        ArraySelect::Indices(_) => None,
    }
}

fn field_chain_path(expr: &SelectExpr) -> Option<String> {
    match expr {
        SelectExpr::Identity | SelectExpr::Current => Some(String::new()),
        SelectExpr::Field(k) | SelectExpr::FieldQuoted(k) => Some(k.clone()),
        SelectExpr::Paren(inner) => field_chain_path(inner),
        SelectExpr::Sub(left, right) => {
            let a = field_chain_path(left)?;
            let b = field_chain_path(right)?;
            Some(join_dot_path(&a, &b))
        }
        _ => None,
    }
}

fn join_dot_path(prefix: &str, suffix: &str) -> String {
    if prefix.is_empty() {
        suffix.to_string()
    } else if suffix.is_empty() {
        prefix.to_string()
    } else {
        format!("{prefix}.{suffix}")
    }
}

// ── Parallel auto-pick ───────────────────────────────────────────────────────

/// Heuristic: prefer Rayon list emit when per-element work is likely **CPU-bound**.
///
/// Returns **true** when the plan walks filters, function calls, comparisons that
/// feed list work, flatten, or multi-select fields that are not pure key lookups.
/// Returns **false** for thin pure-field multi-select cards (`items[*].{id:id,t:t}`)
/// and simple keep-lists — those are usually **memory-bound** and sequential
/// indexed emit often wins (see README heavy-vs-thin parallel tables).
///
/// Used by [`project_indexed_auto`]. Force parallel with `project_indexed_parallel`
/// (Cargo feature `parallel`) or sequential with [`project_indexed`].
pub fn plan_prefers_parallel(plan: &ProjectPlan) -> bool {
    expr_prefers_parallel(plan.select())
}

fn expr_prefers_parallel(expr: &SelectExpr) -> bool {
    match expr {
        SelectExpr::Call { .. } => true,
        SelectExpr::Array(ArraySelect::Filter { .. }) => true,
        SelectExpr::Array(ArraySelect::Each(c)) => expr_prefers_parallel(c),
        SelectExpr::Array(ArraySelect::Slice { each, .. }) => expr_prefers_parallel(each),
        SelectExpr::Array(ArraySelect::Indices(map)) => map.values().any(expr_prefers_parallel),
        SelectExpr::MultiSelectHash(fields) => fields.iter().any(|f| match &f.expr {
            SelectExpr::Field(_) | SelectExpr::FieldQuoted(_) | SelectExpr::Identity
            | SelectExpr::Current | SelectExpr::Literal(_) => false,
            other => expr_prefers_parallel(other),
        }),
        SelectExpr::MultiSelectList(items) => items.iter().any(expr_prefers_parallel),
        SelectExpr::Sub(l, r) | SelectExpr::Pipe(l, r) | SelectExpr::And(l, r)
        | SelectExpr::Or(l, r) => expr_prefers_parallel(l) || expr_prefers_parallel(r),
        SelectExpr::Cmp { left, right, .. } => {
            expr_prefers_parallel(left) || expr_prefers_parallel(right)
        }
        SelectExpr::Flatten(i)
        | SelectExpr::Not(i)
        | SelectExpr::Paren(i)
        | SelectExpr::Expref(i)
        | SelectExpr::ObjectProjection(i) => expr_prefers_parallel(i),
        SelectExpr::Object(obj) => obj.fields.values().any(expr_prefers_parallel),
        SelectExpr::Identity
        | SelectExpr::Current
        | SelectExpr::Field(_)
        | SelectExpr::FieldQuoted(_)
        | SelectExpr::Literal(_) => false,
    }
}

/// Project with side-tables, enabling parallel `[*]` only when [`plan_prefers_parallel`].
///
/// Without feature `parallel`, always sequential (same as [`project_indexed`]).
/// Thin pure-field cards stay sequential even with `parallel` enabled.
pub fn project_indexed_auto(
    doc: &IndexedDocument<'_>,
    plan: &ProjectPlan,
) -> Result<Vec<u8>, Error> {
    #[cfg(feature = "parallel")]
    if plan_prefers_parallel(plan) {
        return project_indexed_parallel(doc, plan);
    }
    project_indexed(doc, plan)
}

/// Build plan indexes, then [`project_indexed_auto`] (seq vs parallel by heuristic).
///
/// Contrast [`project_auto_indexed`], which always projects **sequentially** after
/// indexing. This entry point is the “just do the right thing for bulk list project”
/// convenience when feature `parallel` may or may not be enabled.
pub fn project_parallel_auto(json: &[u8], plan: &ProjectPlan) -> Result<Vec<u8>, Error> {
    let mut doc = IndexedDocument::empty(json);
    doc.index_for_plan(plan)?;
    project_indexed_auto(&doc, plan)
}

/// Stream projection into any [`Write`] without buffering the full output.
///
/// Pipe/function intermediates are **spans** into the document or a reclaimable
/// arena — not full intermediate `Vec` trees. The final document is written
/// incrementally to `w`.
///
/// Returns the number of bytes written.
pub fn project_write<W: Write>(json: &[u8], plan: &ProjectPlan, w: W) -> Result<usize, Error> {
    let mut sink = WSink::new(w);
    project_to_sink(json, plan, &mut sink)?;
    Ok(sink.written)
}

/// Exact projected byte length without retaining the output (counting sink).
///
/// Prefer this over allocating a full `Vec` when only capacity / size ratio matters.
/// Uses the same span/arena evaluator as [`project`] (no full intermediate trees).
pub fn projected_len(json: &[u8], plan: &ProjectPlan) -> Result<usize, Error> {
    let mut c = CountSink::default();
    project_to_sink(json, plan, &mut c)?;
    Ok(c.len)
}

fn project_to_sink(json: &[u8], plan: &ProjectPlan, out: &mut impl EmitOut) -> Result<(), Error> {
    project_to_sink_inner(json, plan, None, out)
}

fn project_to_sink_indexed(
    doc: &IndexedDocument<'_>,
    plan: &ProjectPlan,
    out: &mut impl EmitOut,
) -> Result<(), Error> {
    project_to_sink_inner(doc.as_bytes(), plan, Some(doc), out)
}

fn project_to_sink_inner(
    json: &[u8],
    plan: &ProjectPlan,
    index: Option<&IndexedDocument<'_>>,
    out: &mut impl EmitOut,
) -> Result<(), Error> {
    let start = skip_whitespace(json, 0);
    if start >= json.len() {
        return Err(Error::InvalidJsonSyntax {
            pos: start,
            msg: "Unexpected EOF",
        });
    }
    // Sparse-project critical path: do **not** `skip_value` the entire root document
    // just to compute `end`. On a 300 MiB `{"products":[...]}` catalog that alone
    // was ~0.5–0.8s before any real work. Scanners stop at matching `}`/`]`, so an
    // upper bound of (trimmed) `json.len()` is safe for descent. Only Identity /
    // Current root plans need an exact value end (to avoid trailing junk).
    let end = match plan.select() {
        crate::project::select::SelectExpr::Identity
        | crate::project::select::SelectExpr::Current => skip_value(json, start)?,
        _ => {
            let mut end = json.len();
            while end > start && matches!(json[end - 1], b' ' | b'\t' | b'\n' | b'\r') {
                end -= 1;
            }
            end
        }
    };
    let mut ctx = emit::EmitCtx::new(plan, index);
    emit::emit_value(json, start, end, plan.select(), &mut ctx, out)
}

/// Convenience: [`ProjectPlan::from_paths`] then [`project`].
pub fn project_paths(json: &[u8], paths: &[&str]) -> Result<Vec<u8>, Error> {
    project(json, &ProjectPlan::from_paths(paths)?)
}

/// Convenience: [`ProjectPlan::from_jmespath`] then [`project`].
pub fn project_jmespath(json: &[u8], expr: &str) -> Result<Vec<u8>, Error> {
    project(json, &ProjectPlan::from_jmespath(expr)?)
}

/// Ballpark capacity hint for flat keep-lists (not exact projector output).
pub fn estimate_projected_len(json: &[u8], paths: &[&str]) -> Result<usize, Error> {
    if paths.is_empty() {
        return Ok(2);
    }
    let mut total = 1usize;
    for (i, p) in paths.iter().enumerate() {
        if i > 0 {
            total += 1;
        }
        let segs = parse_path(p);
        let val = find_value(json, &segs)?;
        total += key_wire_len(&segs);
        total += 1;
        total += val.len();
    }
    total += 1;
    Ok(total)
}

/// Sum of on-wire value lengths for `paths`.
pub fn estimate_values_len(json: &[u8], paths: &[&str]) -> Result<usize, Error> {
    let mut total = 0usize;
    for p in paths {
        total += find_value(json, &parse_path(p))?.len();
    }
    Ok(total)
}

fn key_wire_len(segs: &[PathSegment<'_>]) -> usize {
    for seg in segs.iter().rev() {
        if let PathSegment::Key(k) = seg {
            return 2 + k.len();
        }
    }
    2
}

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

    #[test]
    fn project_top_level_keys() {
        let json = br#"{"id":1,"title":"x","blob":true}"#;
        assert_eq!(
            project_paths(json, &["id", "title"]).unwrap(),
            br#"{"id":1,"title":"x"}"#
        );
    }

    #[test]
    fn project_jmespath_multi_hash_rename() {
        // JMESPath returns the projected array (not re-wrapped under "products").
        let json = br#"{"products":[{"id":1,"t":"a"},{"id":2,"t":"b"}]}"#;
        let out = project_jmespath(json, "products[*].{id: id, title: t}").unwrap();
        assert_eq!(
            out,
            br#"[{"id":1,"title":"a"},{"id":2,"title":"b"}]"#
        );
    }

    #[test]
    fn project_jmespath_slice() {
        // Slice projection yields the array value itself.
        let json = br#"{"a":[10,20,30,40]}"#;
        let out = project_jmespath(json, "a[1:3]").unwrap();
        assert_eq!(out, br#"[20,30]"#);
    }

    #[test]
    fn project_jmespath_negative_index() {
        let json = br#"{"a":[10,20,30]}"#;
        assert_eq!(project_jmespath(json, "a[-1]").unwrap(), b"30");
        assert_eq!(project_jmespath(json, "a[-2]").unwrap(), b"20");
    }

    #[test]
    fn project_jmespath_filter() {
        let json = br#"{"items":[{"n":1,"ok":true},{"n":2,"ok":false},{"n":3,"ok":true}]}"#;
        let out = project_jmespath(json, "items[?ok == `true`].n").unwrap();
        assert_eq!(out, br#"[1,3]"#);
    }

    #[test]
    fn project_jmespath_functions() {
        let json = br#"{"a":[3,1,2],"t":"Hello"}"#;
        assert_eq!(project_jmespath(json, "length(a)").unwrap(), b"3");
        assert_eq!(project_jmespath(json, "length(t)").unwrap(), b"5");
        assert_eq!(project_jmespath(json, "sort(a)").unwrap(), br#"[1,2,3]"#);
        assert_eq!(project_jmespath(json, "max(a)").unwrap(), b"3");
        assert_eq!(
            project_jmespath(json, "starts_with(t, 'He')").unwrap(),
            b"true"
        );
        assert_eq!(project_jmespath(json, "type(a)").unwrap(), br#""array""#);
    }

    #[test]
    fn project_jmespath_paren_and_or() {
        let json = br#"{"x":1,"y":0}"#;
        assert_eq!(
            project_jmespath(json, "(x == `1`) && (y == `0`)").unwrap(),
            b"true"
        );
        assert_eq!(project_jmespath(json, "x == `2` || y == `0`").unwrap(), b"true");
        assert_eq!(project_jmespath(json, "!(x == `2`)").unwrap(), b"true");
    }

    #[test]
    fn project_paths_keeps_nesting_wrapper() {
        // Keep-list paths preserve ancestor keys (unlike pure JMESPath result).
        let json = br#"{"products":[{"id":1,"t":"a"},{"id":2,"t":"b"}]}"#;
        let out = project_paths(json, &["products[].id", "products[].t"]).unwrap();
        assert_eq!(
            out,
            br#"{"products":[{"id":1,"t":"a"},{"id":2,"t":"b"}]}"#
        );
    }

    #[test]
    fn project_jmespath_pipe_flatten() {
        let json = br#"{"x":[[1,2],[3],[4,5]]}"#;
        let out = project_jmespath(json, "x | []").unwrap();
        assert_eq!(out, br#"[1,2,3,4,5]"#);
    }

    #[test]
    fn project_jmespath_multi_list() {
        let json = br#"{"id":1,"title":"h"}"#;
        let out = project_jmespath(json, "[id, title]").unwrap();
        assert_eq!(out, br#"[1,"h"]"#);
    }

    #[test]
    fn project_jmespath_literal_field() {
        let json = br#"{"id":1}"#;
        let out = project_jmespath(json, "{id: id, source: 'teefury'}").unwrap();
        assert_eq!(out, br#"{"id":1,"source":"teefury"}"#);
    }

    #[test]
    fn project_array_wildcard_paths() {
        let json = br#"{"products":[{"id":1,"t":"a"},{"id":2,"t":"b"}]}"#;
        let out = project_paths(json, &["products[].id"]).unwrap();
        assert_eq!(out, br#"{"products":[{"id":1},{"id":2}]}"#);
    }

    #[test]
    fn project_pretty() {
        let plan = ProjectPlan::from_paths(&["a", "b"])
            .unwrap()
            .style(ProjectStyle::Pretty { indent: 2 });
        let out = project(br#"{"a":1,"b":2}"#, &plan).unwrap();
        assert_eq!(
            std::str::from_utf8(&out).unwrap(),
            "{\n  \"a\": 1,\n  \"b\": 2\n}"
        );
    }

    #[test]
    fn project_preserve_source() {
        let json = br#"{ "id" : 1 , "drop": 2 }"#;
        let plan = ProjectPlan::from_paths(&["id"])
            .unwrap()
            .style(ProjectStyle::PreserveSource);
        assert_eq!(project(json, &plan).unwrap(), br#"{ "id" : 1 }"#);
    }

    #[test]
    fn project_nested_merge() {
        let json = br#"{"user":{"name":"a","age":9,"city":"z"},"x":0}"#;
        assert_eq!(
            project_paths(json, &["user.name", "user.age"]).unwrap(),
            br#"{"user":{"name":"a","age":9}}"#
        );
    }

    #[test]
    fn project_write_streams_and_counts() {
        let json = br#"{"id":1,"title":"x","blob":[1,2,3]}"#;
        let plan = ProjectPlan::from_paths(&["id", "title"]).unwrap();
        let mut buf = Vec::new();
        let n = project_write(json, &plan, &mut buf).unwrap();
        assert_eq!(n, buf.len());
        assert_eq!(buf, br#"{"id":1,"title":"x"}"#);
        assert_eq!(projected_len(json, &plan).unwrap(), n);
    }

    #[test]
    fn project_indexed_matches_project() {
        let json = br#"{"products":[{"id":1},{"id":2}]}"#;
        let plan = ProjectPlan::from_paths(&["products[].id"]).unwrap();
        let doc = crate::IndexedDocument::build(json, &["products"]).unwrap();
        assert_eq!(
            project_indexed(&doc, &plan).unwrap(),
            project(json, &plan).unwrap()
        );
    }

    #[test]
    fn flatten_projection_vs_list_projection() {
        let json = br#"{"reservations":[{"instances":[{"foo":1},{"foo":2}]}]}"#;
        assert_eq!(
            project_jmespath(json, "reservations[].instances[].foo").unwrap(),
            br#"[1,2]"#
        );
        assert_eq!(
            project_jmespath(json, "reservations[*].instances[*].foo").unwrap(),
            br#"[[1,2]]"#
        );
    }

    #[test]
    fn pure_pipe_matches_direct_path() {
        let json = br#"{"a":{"b":{"c":42}}}"#;
        assert_eq!(
            project_jmespath(json, "a.b | c").unwrap(),
            project_jmespath(json, "a.b.c").unwrap()
        );
    }

    #[test]
    fn plan_array_paths_for_index() {
        let plan = ProjectPlan::from_paths(&["products[0].id", "products[2].title"]).unwrap();
        assert_eq!(plan.array_paths_for_index(), vec!["products".to_string()]);

        let plan = ProjectPlan::from_jmespath("products[*].{id: id, title: title}").unwrap();
        assert_eq!(plan.array_paths_for_index(), vec!["products".to_string()]);
    }

    /// Pure-field multi-select must be a single object walk: correctness for renames,
    /// missing→null, field order, and any key names (not catalog-specific).
    #[test]
    fn multi_select_pure_fields_one_pass_semantics() {
        let json = br#"{"z":9,"a":1,"noise":true,"b":"two","c":{"nested":3}}"#;

        // Declared output order, not document order; rename b→alias.
        assert_eq!(
            project_jmespath(json, "{first: a, alias: b, last: z}").unwrap(),
            br#"{"first":1,"alias":"two","last":9}"#
        );

        // Missing keys → null (JMESPath multi-select).
        assert_eq!(
            project_jmespath(json, "{a: a, missing: nope, b: b}").unwrap(),
            br#"{"a":1,"missing":null,"b":"two"}"#
        );

        // Same source field twice.
        assert_eq!(
            project_jmespath(json, "{x: a, y: a}").unwrap(),
            br#"{"x":1,"y":1}"#
        );

        // Nested expr still works (mixed path; not pure-field one-pass).
        assert_eq!(
            project_jmespath(json, "{a: a, n: c.nested}").unwrap(),
            br#"{"a":1,"n":3}"#
        );

        // Array of heterogeneous objects — one-pass per element.
        let arr = br#"[{"id":1,"t":"a","x":9},{"id":2,"t":"b"},{"id":3,"t":"c","x":1}]"#;
        assert_eq!(
            project_jmespath(arr, "[*].{id: id, t: t, x: x}").unwrap(),
            br#"[{"id":1,"t":"a","x":9},{"id":2,"t":"b","x":null},{"id":3,"t":"c","x":1}]"#
        );
    }

    #[test]
    fn project_object_fields_generic_shapes() {
        // Root array of objects
        let root = br#"[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6}]"#;
        assert_eq!(
            project_object_fields(root, "", &["a", "c"]).unwrap(),
            br#"[{"a":1,"c":3},{"a":4,"c":6}]"#
        );
        // Nested array under arbitrary keys
        let nested = br#"{"meta":{"rows":[{"x":9,"y":8,"z":7}]}}"#;
        assert_eq!(
            project_object_fields(nested, "meta.rows", &["z", "x"]).unwrap(),
            br#"[{"z":7,"x":9}]"#
        );
    }

    #[test]
    fn project_each_and_jsonl_match_array_project() {
        let json = br#"{"items":[{"id":1,"t":"a","x":9},{"id":2,"t":"b"},{"id":3,"t":"c","x":1}]}"#;
        let plan = ProjectPlan::from_jmespath("items[*].{id: id, t: t}").unwrap();
        let as_array = project(json, &plan).unwrap();

        let mut cards = Vec::new();
        project_each(json, &plan, |i, card| {
            cards.push((i, card.to_vec()));
            Ok(())
        })
        .unwrap();
        assert_eq!(cards.len(), 3);
        assert_eq!(cards[0].1, br#"{"id":1,"t":"a"}"#);
        assert_eq!(cards[2].1, br#"{"id":3,"t":"c"}"#);

        // Reconstruct array from cards and compare to project().
        let mut rebuilt = Vec::from(b"[".as_slice());
        for (i, (_, c)) in cards.iter().enumerate() {
            if i > 0 {
                rebuilt.push(b',');
            }
            rebuilt.extend_from_slice(c);
        }
        rebuilt.push(b']');
        assert_eq!(rebuilt, as_array);

        let mut jsonl = Vec::new();
        let n = project_jsonl_write(json, &plan, &mut jsonl).unwrap();
        assert_eq!(n, jsonl.len());
        assert_eq!(
            &jsonl[..],
            b"{\"id\":1,\"t\":\"a\"}\n{\"id\":2,\"t\":\"b\"}\n{\"id\":3,\"t\":\"c\"}\n"
        );

        // Nested path + object_fields_each
        let nested = br#"{"meta":{"rows":[{"z":7,"x":9,"y":1}]}}"#;
        let mut got = None;
        project_object_fields_each(nested, "meta.rows", &["z", "x"], |_, card| {
            got = Some(card.to_vec());
            Ok(())
        })
        .unwrap();
        assert_eq!(got.unwrap(), br#"{"z":7,"x":9}"#);

        // Root array
        let root = br#"[{"a":1,"b":2},{"a":3,"b":4}]"#;
        let mut jsonl = Vec::new();
        project_object_fields_jsonl_write(root, "", &["a"], &mut jsonl).unwrap();
        assert_eq!(&jsonl[..], b"{\"a\":1}\n{\"a\":3}\n");

        // Indexed path
        let mut doc = crate::IndexedDocument::empty(json);
        doc.index_for_plan(&plan).unwrap();
        let mut n = 0usize;
        project_each_indexed(&doc, &plan, |_, _| {
            n += 1;
            Ok(())
        })
        .unwrap();
        assert_eq!(n, 3);
    }

    #[test]
    fn project_each_filter_and_slice_match_array_project() {
        let json = br#"{"items":[
            {"id":1,"ok":true,"t":"a"},
            {"id":2,"ok":false,"t":"b"},
            {"id":3,"ok":true,"t":"c"},
            {"id":4,"ok":true,"t":"d"}
        ]}"#;

        // Filter: keep truthy ok, project thin card
        let plan_f = ProjectPlan::from_jmespath("items[?ok].{id: id, t: t}").unwrap();
        let as_array = project(json, &plan_f).unwrap();
        let mut cards = Vec::new();
        project_each(json, &plan_f, |i, card| {
            cards.push((i, card.to_vec()));
            Ok(())
        })
        .unwrap();
        assert_eq!(cards.len(), 3);
        assert_eq!(cards[0].1, br#"{"id":1,"t":"a"}"#);
        assert_eq!(cards[1].1, br#"{"id":3,"t":"c"}"#);
        assert_eq!(cards[2].1, br#"{"id":4,"t":"d"}"#);
        let mut rebuilt = Vec::from(b"[".as_slice());
        for (i, (_, c)) in cards.iter().enumerate() {
            if i > 0 {
                rebuilt.push(b',');
            }
            rebuilt.extend_from_slice(c);
        }
        rebuilt.push(b']');
        assert_eq!(rebuilt, as_array);

        // Slice [1:3]
        let plan_s = ProjectPlan::from_jmespath("items[1:3].{id: id}").unwrap();
        let as_array = project(json, &plan_s).unwrap();
        let mut cards = Vec::new();
        project_each(json, &plan_s, |_, card| {
            cards.push(card.to_vec());
            Ok(())
        })
        .unwrap();
        assert_eq!(cards, vec![br#"{"id":2}"#.to_vec(), br#"{"id":3}"#.to_vec()]);
        let mut rebuilt = Vec::from(b"[".as_slice());
        for (i, c) in cards.iter().enumerate() {
            if i > 0 {
                rebuilt.push(b',');
            }
            rebuilt.extend_from_slice(c);
        }
        rebuilt.push(b']');
        assert_eq!(rebuilt, as_array);

        // Root slice
        let root = br#"[10,20,30,40]"#;
        let plan_r = ProjectPlan::from_jmespath("[1:3]").unwrap();
        let mut got = Vec::new();
        project_each(root, &plan_r, |_, card| {
            got.push(card.to_vec());
            Ok(())
        })
        .unwrap();
        assert_eq!(got, vec![b"20".to_vec(), b"30".to_vec()]);
        assert_eq!(project(root, &plan_r).unwrap(), br#"[20,30]"#);

        // JSONL via filter plan
        let mut jsonl = Vec::new();
        project_jsonl_write(json, &plan_f, &mut jsonl).unwrap();
        assert_eq!(
            &jsonl[..],
            b"{\"id\":1,\"t\":\"a\"}\n{\"id\":3,\"t\":\"c\"}\n{\"id\":4,\"t\":\"d\"}\n"
        );
    }

    #[test]
    fn plan_prefers_parallel_heuristic() {
        let thin = ProjectPlan::from_jmespath("items[*].{id: id, title: title}").unwrap();
        assert!(
            !plan_prefers_parallel(&thin),
            "pure-field multi-select should stay sequential"
        );
        let heavy = ProjectPlan::from_jmespath(
            "records[*].{id: id, n: length(scores[?@ > `0.5`])}",
        )
        .unwrap();
        assert!(
            plan_prefers_parallel(&heavy),
            "filter + length should prefer parallel"
        );
        let filtered = ProjectPlan::from_jmespath("items[?id > `0`].id").unwrap();
        assert!(plan_prefers_parallel(&filtered));
    }

    #[test]
    fn project_indexed_auto_thin_matches_sequential() {
        let mut json = String::from(r#"{"rows":["#);
        for i in 0..80 {
            if i > 0 {
                json.push(',');
            }
            json.push_str(&format!(r#"{{"id":{i},"name":"n{i}"}}"#));
        }
        json.push_str("]}");
        let bytes = json.as_bytes();
        let plan = ProjectPlan::from_jmespath("rows[*].{id: id, name: name}").unwrap();
        let mut doc = crate::IndexedDocument::empty(bytes);
        doc.index_for_plan(&plan).unwrap();
        let seq = project_indexed(&doc, &plan).unwrap();
        let auto = project_indexed_auto(&doc, &plan).unwrap();
        assert_eq!(seq, auto);
        let prep = project_parallel_auto(bytes, &plan).unwrap();
        assert_eq!(seq, prep);
    }

    #[cfg(feature = "parallel")]
    #[test]
    fn project_parallel_matches_sequential_on_array() {
        let mut json = String::from(r#"{"rows":["#);
        for i in 0..200 {
            if i > 0 {
                json.push(',');
            }
            json.push_str(&format!(r#"{{"id":{i},"name":"n{i}","noise":true}}"#));
        }
        json.push_str("]}");
        let bytes = json.as_bytes();
        let seq = project_object_fields(bytes, "rows", &["id", "name"]).unwrap();
        let par = project_object_fields_parallel(bytes, "rows", &["id", "name"]).unwrap();
        assert_eq!(seq, par);
        let plan = ProjectPlan::from_jmespath("rows[*].{id: id, name: name}").unwrap();
        let mut doc = crate::IndexedDocument::empty(bytes);
        doc.index_for_plan(&plan).unwrap();
        assert_eq!(project_indexed_parallel(&doc, &plan).unwrap(), seq);
    }

    #[test]
    fn project_auto_indexed_mid_element() {
        let mut json = String::from(r#"{"products":["#);
        for i in 0..100 {
            if i > 0 {
                json.push(',');
            }
            json.push_str(&format!(r#"{{"id":{i},"title":"T{i}"}}"#));
        }
        json.push_str("]}");
        let bytes = json.as_bytes();
        let plan = ProjectPlan::from_paths(&["products[50].id"]).unwrap();
        let out = project_auto_indexed(bytes, &plan).unwrap();
        assert_eq!(out, br#"{"products":{"id":50}}"#);

        let mut doc = crate::IndexedDocument::empty(bytes);
        let out2 = project_indexed_prepare(&mut doc, &plan).unwrap();
        assert_eq!(out2, out);
        assert!(doc.array_len(&crate::parse_path("products")).unwrap() >= 100);
    }

    /// Sparse single-index correctness across first / mid / last / negative.
    #[test]
    fn single_index_short_circuit_correctness() {
        let mut json = String::from(r#"{"products":["#);
        for i in 0..200 {
            if i > 0 {
                json.push(',');
            }
            json.push_str(&format!(
                r#"{{"id":{i},"title":"P{i}","pad":"{pad}"}}"#,
                pad = "x".repeat(64)
            ));
        }
        json.push_str("]}");
        let bytes = json.as_bytes();

        assert_eq!(project_jmespath(bytes, "products[0].id").unwrap(), b"0");
        assert_eq!(project_jmespath(bytes, "products[99].id").unwrap(), b"99");
        assert_eq!(project_jmespath(bytes, "products[199].id").unwrap(), b"199");
        assert_eq!(project_jmespath(bytes, "products[-1].id").unwrap(), b"199");
        assert_eq!(
            project_jmespath(bytes, "products[-1].title").unwrap(),
            br#""P199""#
        );

        // Multi-select list of sparse index projects.
        let multi =
            project_jmespath(bytes, "[products[0].id, products[2].id, products[-1].id]").unwrap();
        let v: serde_json::Value = serde_json::from_slice(&multi).unwrap();
        assert_eq!(v, serde_json::json!([0, 2, 199]));

        // Keep-list + index: first product only, structure preserved.
        let mut doc = crate::IndexedDocument::empty(bytes);
        doc.index_array_str("products").unwrap();
        let plan = ProjectPlan::from_paths(&["products[0].id", "products[0].title"]).unwrap();
        let out = project_indexed(&doc, &plan).unwrap();
        assert_eq!(out, project(bytes, &plan).unwrap());
        assert_eq!(out, br#"{"products":{"id":0,"title":"P0"}}"#);
    }


    #[test]
    fn p0_synthetic_sparse_index_is_fast() {
        // ~80 MiB catalog: if we accidentally full-scan, this exceeds 50ms easily.
        let mut json = String::from(r#"{"products":["#);
        let pad = "x".repeat(256);
        for i in 0..50_000 {
            if i > 0 { json.push(','); }
            json.push_str(&format!(r#"{{"id":{i},"title":"T{i}","pad":"{pad}"}}"#));
        }
        json.push_str("]}");
        let bytes = json.as_bytes();
        let t0 = std::time::Instant::now();
        let out = project_jmespath(bytes, "products[0].id").unwrap();
        let dt = t0.elapsed();
        assert_eq!(out, b"0");
        // Full scan of 50k * ~300B ≈ 15MB+ of skip_value work would be tens of ms;
        // true sparse should be well under 5ms on a quiet machine.
        assert!(
            dt.as_millis() < 25,
            "products[0].id took {dt:?} — sparse short-circuit not active?"
        );

        let t1 = std::time::Instant::now();
        let out = project_paths(bytes, &["products[0].id", "products[0].title"]).unwrap();
        let dt1 = t1.elapsed();
        assert_eq!(out, br#"{"products":{"id":0,"title":"T0"}}"#);
        assert!(
            dt1.as_millis() < 25,
            "keep-list products[0] took {dt1:?}"
        );

        let mut doc = crate::IndexedDocument::empty(bytes);
        doc.index_array_str("products").unwrap();
        let plan = ProjectPlan::from_paths(&["products[24999].id"]).unwrap();
        let t2 = std::time::Instant::now();
        let out = project_indexed(&doc, &plan).unwrap();
        let dt2 = t2.elapsed();
        assert_eq!(out, br#"{"products":{"id":24999}}"#);
        assert!(
            dt2.as_millis() < 15,
            "indexed products[24999] took {dt2:?}"
        );
    }

    #[test]
    fn transform_pipeline_rename_drop_inject() {
        use crate::project::transform::{Transform, TransformPipeline};
        let json = br#"{"id":1,"title":"Hat","noise":true}"#;
        let out = TransformPipeline::new()
            .then(Transform::KeepPaths(&["id", "title"]))
            .then(Transform::Rename {
                from: "title",
                to: "name",
            })
            .then(Transform::Inject {
                key: "source",
                value: br#""api""#,
            })
            .then(Transform::Drop("id"))
            .apply(json)
            .unwrap();
        let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(v["name"], "Hat");
        assert_eq!(v["source"], "api");
        assert!(v.get("id").is_none());
        assert!(v.get("noise").is_none());
    }

    #[test]
    fn preserve_source_array_spacing() {
        let json = br#"[ 1 , 2 , 3 ]"#;
        // Identity array projection via jmes
        let plan = ProjectPlan::from_jmespath("@")
            .unwrap()
            .style(ProjectStyle::PreserveSource);
        // Full identity copies raw span including spaces
        let out = project(json, &plan).unwrap();
        assert_eq!(out, json);
    }

    #[test]
    fn serde_accepts_projection() {
        let json = br#"{"id":7,"title":"Hat","images":[1,2,3],"meta":{"x":1}}"#;
        let out = project_paths(json, &["id", "title", "meta.x"]).unwrap();
        let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(v["id"], 7);
        assert!(v.get("images").is_none());
    }
}