cadmpeg 0.5.5

Convert and inspect native CAD files.
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
// SPDX-License-Identifier: Apache-2.0
//! Single-record lookup by arena and ID for `cadmpeg query item`.
//!
//! Default output is pretty-printed JSON records (blank-line separated), not
//! TSV: nested records do not fit the other views' table convention.
//! `--fields` projects a closed list of dotted paths as TSV; cell escaping is
//! projection-specific (see [`project_fields`]).

use std::fmt;

use anyhow::{bail, Context, Result};
use cadmpeg_core::decode::alloc_filled;
use clap::Args;
use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
use serde_json::value::RawValue;

use super::{detect, print_json, read_input, Artifact};

/// Input selection for `query item`.
#[derive(Debug, Args)]
pub struct ItemArgs {
    /// JSON file, or `-` for standard input.
    pub file: std::path::PathBuf,
    /// Arena address: `model.<arena>`, `native.<codec>.<arena>`, or bare
    /// `<arena>` as shorthand for `model.<arena>`. Same dotted names as
    /// `query counts --json`.
    pub arena: String,
    /// Record IDs (exact or unique suffix). Omit for the first record;
    /// conflicts with `--head`.
    pub ids: Vec<String>,
    /// Print the first N records in arena order. Conflicts with explicit IDs.
    #[arg(long, value_name = "N", conflicts_with = "ids")]
    pub head: Option<usize>,
    /// Comma-separated dotted field paths; project as TSV (no expressions).
    /// Conflicts with `--json`.
    #[arg(long, value_delimiter = ',', conflicts_with = "json")]
    pub fields: Option<Vec<String>>,
    /// Wrap matched records in the versioned JSON envelope.
    #[arg(long)]
    pub json: bool,
}

/// Where the requested arena lives in the document.
#[derive(Debug, Clone)]
pub(crate) enum ArenaTarget {
    Model { arena: String },
    Native { codec: String, arena: String },
}

impl ArenaTarget {
    pub(crate) fn parse(spec: &str) -> Result<Self> {
        if !spec.contains('.') {
            return Ok(Self::Model {
                arena: spec.to_owned(),
            });
        }
        let parts: Vec<&str> = spec.split('.').collect();
        match parts.as_slice() {
            ["model", arena] if !arena.is_empty() => Ok(Self::Model {
                arena: (*arena).to_owned(),
            }),
            ["native", codec, arena] if !codec.is_empty() && !arena.is_empty() => {
                Ok(Self::Native {
                    codec: (*codec).to_owned(),
                    arena: (*arena).to_owned(),
                })
            }
            _ => bail!(
                "arena name {spec:?} is not addressable; use `model.<arena>`, \
                 `native.<codec>.<arena>`, or a bare `<arena>` shorthand for \
                 `model.<arena>` (same names as `cadmpeg query counts --json`)"
            ),
        }
    }

    pub(crate) fn dotted(&self) -> String {
        match self {
            Self::Model { arena } => format!("model.{arena}"),
            Self::Native { codec, arena } => format!("native.{codec}.{arena}"),
        }
    }

    fn matches_model(&self, arena: &str) -> bool {
        matches!(self, Self::Model { arena: a } if a == arena)
    }

    fn matches_native(&self, codec: &str, arena: &str) -> bool {
        matches!(self, Self::Native { codec: c, arena: a } if c == codec && a == arena)
    }
}

/// How many / which records to retain while streaming the arena.
#[derive(Debug)]
enum KeepMode<'a> {
    /// Keep records that exact- or suffix-match any requested ID.
    Ids(&'a [String]),
    /// Keep the first N records in arena order.
    Head(usize),
}

/// Result of one targeted deserialize of a CADIR document.
struct Capture {
    /// True when the target key was present and its value was a JSON array.
    found_array: bool,
    entry_count: u64,
    /// Kept raw records (ID hits or first-N).
    kept: Vec<Box<RawValue>>,
    /// Every JSON-string `id` observed in the target arena (ID mode).
    all_ids: Vec<String>,
    /// Dotted names of every addressable (JSON-array) arena, with entry counts.
    addressable: Vec<(String, u64)>,
}

/// Tolerant id probe: a non-string `id` becomes `None` instead of failing the
/// element.
#[derive(Deserialize)]
struct IdProbe {
    #[serde(default)]
    id: Option<serde_json::Value>,
}

fn string_id(raw: &RawValue) -> Option<String> {
    let probe: IdProbe = serde_json::from_str(raw.get()).ok()?;
    match probe.id {
        Some(serde_json::Value::String(s)) => Some(s),
        _ => None,
    }
}

/// Runs `query item` against one artifact.
pub fn run(args: &ItemArgs) -> Result<()> {
    let bytes = read_input(&args.file)?;
    let artifact = detect(&bytes, &args.file)?;
    match artifact {
        Artifact::Cadir(_) => {}
        Artifact::Report(_) => bail!(
            "{} is a command report; reports have no arenas. Use \
             `cadmpeg query findings` / `cadmpeg query losses` on the report, or \
             `cadmpeg dump SOURCE -o doc.json && cadmpeg query item doc.json ARENA ID`",
            args.file.display()
        ),
        Artifact::Sidecar(_) => bail!(
            "{} is a decode sidecar (`<stem>.fidelity.json`); sidecars have no \
             arenas. Run `cadmpeg dump SOURCE -o doc.json && cadmpeg query item \
             doc.json ARENA ID`",
            args.file.display()
        ),
    }

    let target = ArenaTarget::parse(&args.arena)?;
    let text = std::str::from_utf8(&bytes)
        .with_context(|| format!("{} is not valid UTF-8", args.file.display()))?;

    let mode = if args.ids.is_empty() {
        KeepMode::Head(args.head.unwrap_or(1))
    } else {
        KeepMode::Ids(&args.ids)
    };

    let capture = CaptureSeed {
        target: &target,
        mode: &mode,
    }
    .deserialize(&mut serde_json::Deserializer::from_str(text))
    .with_context(|| format!("parsing the CADIR document {}", args.file.display()))?;

    if !capture.found_array {
        bail!("{}", unknown_arena_message(&target, &capture.addressable));
    }

    match mode {
        KeepMode::Head(_) => {
            let values = parse_kept(&capture.kept)?;
            emit(args, &values)
        }
        KeepMode::Ids(ids) => {
            let dotted = target.dotted();
            let (values, errors) = resolve_ids(ids, &capture, &dotted)?;
            match (emit(args, &values), errors.is_empty()) {
                (Ok(()), true) => Ok(()),
                (Ok(()), false) => bail!("{}", errors.join("\n")),
                (Err(err), true) => Err(err),
                (Err(err), false) => bail!("{err}\n{}", errors.join("\n")),
            }
        }
    }
}

fn parse_kept(kept: &[Box<RawValue>]) -> Result<Vec<serde_json::Value>> {
    kept.iter()
        .map(|raw| {
            serde_json::from_str(raw.get())
                .with_context(|| "parsing a retained arena record as JSON")
        })
        .collect()
}

fn resolve_ids(
    ids: &[String],
    capture: &Capture,
    dotted: &str,
) -> Result<(Vec<serde_json::Value>, Vec<String>)> {
    let mut indexed: Vec<(Option<String>, &RawValue)> = Vec::with_capacity(capture.kept.len());
    for raw in &capture.kept {
        indexed.push((string_id(raw), raw.as_ref()));
    }

    let mut values = Vec::new();
    let mut errors = Vec::new();

    for request in ids {
        match resolve_one(request, &indexed) {
            Ok(raw) => {
                let value: serde_json::Value = serde_json::from_str(raw.get())
                    .with_context(|| format!("parsing record for id {request:?}"))?;
                values.push(value);
            }
            Err(ResolveError::Ambiguous(matches)) => {
                errors.push(ambiguous_message(request, dotted, &matches));
            }
            Err(ResolveError::Missing) => {
                errors.push(miss_id_message(
                    dotted,
                    request,
                    capture.entry_count,
                    &capture.all_ids,
                ));
            }
        }
    }
    Ok((values, errors))
}

enum ResolveError {
    Missing,
    Ambiguous(Vec<String>),
}

fn resolve_one<'a>(
    request: &str,
    indexed: &[(Option<String>, &'a RawValue)],
) -> Result<&'a RawValue, ResolveError> {
    let mut exact: Option<&RawValue> = None;
    for (id, raw) in indexed {
        if id.as_deref() == Some(request) {
            exact = Some(*raw);
            break;
        }
    }
    if let Some(raw) = exact {
        return Ok(raw);
    }

    let mut suffix: Vec<(String, &RawValue)> = Vec::new();
    for (id, raw) in indexed {
        if let Some(id) = id {
            if id.ends_with(request) {
                suffix.push((id.clone(), *raw));
            }
        }
    }
    match suffix.len() {
        0 => Err(ResolveError::Missing),
        1 => Ok(suffix[0].1),
        _ => Err(ResolveError::Ambiguous(
            suffix.into_iter().map(|(id, _)| id).collect(),
        )),
    }
}

fn emit(args: &ItemArgs, values: &[serde_json::Value]) -> Result<()> {
    emit_values("item", args.json, args.fields.as_deref(), values)
}

/// Pretty-print records, TSV `--fields`, or the versioned `--json` envelope.
pub(crate) fn emit_values(
    view: &str,
    json: bool,
    fields: Option<&[String]>,
    values: &[serde_json::Value],
) -> Result<()> {
    if json {
        print_json(view, &serde_json::Value::Array(values.to_vec()));
        return Ok(());
    }
    // Empty arena / no matches: empty stdout (no TSV header), exit 0 unless a
    // caller layers ID or --fields teaching errors on top.
    if values.is_empty() {
        return Ok(());
    }
    if let Some(paths) = fields {
        let (tsv, empty_paths) = project_fields(values, paths)?;
        print!("{tsv}");
        if !empty_paths.is_empty() {
            bail!("{}", empty_fields_message(values, &empty_paths));
        }
        return Ok(());
    }
    for (i, value) in values.iter().enumerate() {
        if i > 0 {
            println!();
        }
        println!(
            "{}",
            serde_json::to_string_pretty(value).context("serializing a record")?
        );
    }
    Ok(())
}

pub(crate) fn unknown_arena_message(target: &ArenaTarget, addressable: &[(String, u64)]) -> String {
    let list = if addressable.is_empty() {
        "(none — this document has no array arenas)".to_owned()
    } else {
        addressable
            .iter()
            .map(|(name, _)| name.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    };
    format!(
        "unknown arena {}; addressable arenas in this document: {}; run \
         `cadmpeg query counts FILE` for entries per arena",
        target.dotted(),
        list
    )
}

pub(crate) fn miss_id_message(
    arena: &str,
    request: &str,
    entry_count: u64,
    all_ids: &[String],
) -> String {
    const SHOWN: usize = 10;
    let lower = request.to_lowercase();
    let mut label = "close ids";
    let mut names: Vec<&str> = all_ids
        .iter()
        .filter(|id| id.to_lowercase().contains(&lower))
        .take(SHOWN)
        .map(String::as_str)
        .collect();
    if names.is_empty() {
        label = "ids include";
        names = all_ids.iter().take(SHOWN).map(String::as_str).collect();
    }
    if names.is_empty() {
        format!(
            "no record in {arena} ({entry_count} entries) has string id {request:?}; \
             none of the entries carry a JSON-string `id` field"
        )
    } else {
        format!(
            "no record in {arena} ({entry_count} entries) has id {request:?}; {label}: {}; \
             pass a longer suffix or the full id",
            names.join(", ")
        )
    }
}

pub(crate) fn ambiguous_message(request: &str, arena: &str, matches: &[String]) -> String {
    const SHOWN: usize = 10;
    let shown: Vec<&str> = matches.iter().take(SHOWN).map(String::as_str).collect();
    format!(
        "ambiguous id suffix {request:?} in {arena}; matches: {}; pass a longer \
         suffix or the full id",
        shown.join(", ")
    )
}

pub(crate) fn empty_fields_message(values: &[serde_json::Value], empty_paths: &[String]) -> String {
    let mut parts = Vec::new();
    for path in empty_paths {
        let keys = first_parent_keys(values, path);
        let key_note = match keys {
            Some(keys) if !keys.is_empty() => format!("; observed fields: {}", keys.join(", ")),
            Some(_) => "; parent object has no fields".to_owned(),
            None => "; parent path is absent or not an object in every row".to_owned(),
        };
        parts.push(format!(
            "field path {path:?} was empty in every projected row{key_note}"
        ));
    }
    parts.push(
        "list fields with `cadmpeg query schema FILE ARENA` (native records) or \
         `cadmpeg query schema model.<arena>` (IR types)"
            .to_owned(),
    );
    parts.join("\n")
}

fn first_parent_keys(values: &[serde_json::Value], path: &str) -> Option<Vec<String>> {
    let parent = match path.rsplit_once('.') {
        Some((parent, _)) => parent,
        None => "",
    };
    for value in values {
        let node = if parent.is_empty() {
            Some(value)
        } else {
            navigate(value, parent)
        };
        if let Some(serde_json::Value::Object(map)) = node {
            return Some(map.keys().cloned().collect());
        }
    }
    None
}

/// Projects records to TSV. Returns the full TSV text (including header) and
/// every path that was empty in all rows.
///
/// Cell rules (projection-specific; not the other views' [`super::cell`]):
/// JSON strings/numbers/bools are bare; tab/newline in a string become `\t`/
/// `\n`; null or absent is an empty cell; arrays/objects are compact JSON.
pub(crate) fn project_fields(
    values: &[serde_json::Value],
    paths: &[String],
) -> Result<(String, Vec<String>)> {
    if paths.is_empty() {
        bail!("--fields requires at least one dotted path");
    }
    let mut out = String::new();
    out.push_str(&paths.join("\t"));
    out.push('\n');

    let mut empty_counts = alloc_filled(paths.len(), 0usize, "cli query field counts")?;
    let row_count = values.len();
    for value in values {
        for (i, path) in paths.iter().enumerate() {
            if i > 0 {
                out.push('\t');
            }
            let cell = match navigate(value, path) {
                None | Some(serde_json::Value::Null) => {
                    empty_counts[i] += 1;
                    String::new()
                }
                Some(v) => field_cell(v),
            };
            out.push_str(&cell);
        }
        out.push('\n');
    }

    let empty_paths = if row_count == 0 {
        Vec::new()
    } else {
        paths
            .iter()
            .zip(empty_counts.iter())
            .filter(|&(_, &count)| count == row_count)
            .map(|(path, _)| path.clone())
            .collect()
    };
    Ok((out, empty_paths))
}

pub(crate) fn navigate<'a>(
    value: &'a serde_json::Value,
    path: &str,
) -> Option<&'a serde_json::Value> {
    let mut cur = value;
    for part in path.split('.') {
        cur = cur.as_object()?.get(part)?;
    }
    Some(cur)
}

pub(crate) fn field_cell(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::Null => String::new(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::String(s) => s.replace('\t', "\\t").replace('\n', "\\n"),
        serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
            serde_json::to_string(value).unwrap_or_default()
        }
    }
}

// --- DeserializeSeed capture ------------------------------------------------

struct CaptureSeed<'a> {
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
}

impl<'de> DeserializeSeed<'de> for CaptureSeed<'_> {
    type Value = Capture;

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Capture, D::Error> {
        deserializer.deserialize_map(DocumentVisitor {
            target: self.target,
            mode: self.mode,
        })
    }
}

struct DocumentVisitor<'a> {
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
}

impl<'de> Visitor<'de> for DocumentVisitor<'_> {
    type Value = Capture;

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("a CADIR document object")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Capture, A::Error> {
        let mut capture = Capture {
            found_array: false,
            entry_count: 0,
            kept: Vec::new(),
            all_ids: Vec::new(),
            addressable: Vec::new(),
        };
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "model" => {
                    map.next_value_seed(ModelSeed {
                        target: self.target,
                        mode: self.mode,
                        capture: &mut capture,
                    })?;
                }
                "native" => {
                    map.next_value_seed(NativeRootSeed {
                        target: self.target,
                        mode: self.mode,
                        capture: &mut capture,
                    })?;
                }
                _ => {
                    let _ = map.next_value::<IgnoredAny>()?;
                }
            }
        }
        Ok(capture)
    }
}

struct ModelSeed<'a> {
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> DeserializeSeed<'de> for ModelSeed<'_> {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
        deserializer.deserialize_map(ArenasVisitor {
            namespace: ArenaNamespace::Model,
            codec: None,
            target: self.target,
            mode: self.mode,
            capture: self.capture,
        })
    }
}

struct NativeRootSeed<'a> {
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> DeserializeSeed<'de> for NativeRootSeed<'_> {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
        deserializer.deserialize_map(NativeRootVisitor {
            target: self.target,
            mode: self.mode,
            capture: self.capture,
        })
    }
}

struct NativeRootVisitor<'a> {
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> Visitor<'de> for NativeRootVisitor<'_> {
    type Value = ();

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("a native codec map")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<(), A::Error> {
        while let Some(codec) = map.next_key::<String>()? {
            map.next_value_seed(NativeCodecSeed {
                codec: &codec,
                target: self.target,
                mode: self.mode,
                capture: self.capture,
            })?;
        }
        Ok(())
    }
}

struct NativeCodecSeed<'a> {
    codec: &'a str,
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> DeserializeSeed<'de> for NativeCodecSeed<'_> {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
        deserializer.deserialize_map(NativeCodecVisitor {
            codec: self.codec,
            target: self.target,
            mode: self.mode,
            capture: self.capture,
        })
    }
}

struct NativeCodecVisitor<'a> {
    codec: &'a str,
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> Visitor<'de> for NativeCodecVisitor<'_> {
    type Value = ();

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("a native codec object with an arenas map")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<(), A::Error> {
        while let Some(key) = map.next_key::<String>()? {
            if key == "arenas" {
                map.next_value_seed(ArenasSeed {
                    namespace: ArenaNamespace::Native,
                    codec: Some(self.codec),
                    target: self.target,
                    mode: self.mode,
                    capture: self.capture,
                })?;
            } else {
                let _ = map.next_value::<IgnoredAny>()?;
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy)]
enum ArenaNamespace {
    Model,
    Native,
}

struct ArenasSeed<'a> {
    namespace: ArenaNamespace,
    codec: Option<&'a str>,
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> DeserializeSeed<'de> for ArenasSeed<'_> {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
        deserializer.deserialize_map(ArenasVisitor {
            namespace: self.namespace,
            codec: self.codec,
            target: self.target,
            mode: self.mode,
            capture: self.capture,
        })
    }
}

struct ArenasVisitor<'a> {
    namespace: ArenaNamespace,
    codec: Option<&'a str>,
    target: &'a ArenaTarget,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> Visitor<'de> for ArenasVisitor<'_> {
    type Value = ();

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("an arenas object")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<(), A::Error> {
        while let Some(arena) = map.next_key::<String>()? {
            let is_target = match self.namespace {
                ArenaNamespace::Model => self.target.matches_model(&arena),
                ArenaNamespace::Native => self
                    .codec
                    .is_some_and(|codec| self.target.matches_native(codec, &arena)),
            };
            let dotted = match self.namespace {
                ArenaNamespace::Model => format!("model.{arena}"),
                ArenaNamespace::Native => {
                    format!("native.{}.{arena}", self.codec.unwrap_or(""))
                }
            };
            map.next_value_seed(ArenaValueSeed {
                dotted,
                is_target,
                mode: self.mode,
                capture: self.capture,
            })?;
        }
        Ok(())
    }
}

struct ArenaValueSeed<'a> {
    dotted: String,
    is_target: bool,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> DeserializeSeed<'de> for ArenaValueSeed<'_> {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<(), D::Error> {
        deserializer.deserialize_any(ArenaValueVisitor {
            dotted: self.dotted,
            is_target: self.is_target,
            mode: self.mode,
            capture: self.capture,
        })
    }
}

struct ArenaValueVisitor<'a> {
    dotted: String,
    is_target: bool,
    mode: &'a KeepMode<'a>,
    capture: &'a mut Capture,
}

impl<'de> Visitor<'de> for ArenaValueVisitor<'_> {
    type Value = ();

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("an arena array")
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<(), A::Error> {
        if !self.is_target {
            let mut n = 0u64;
            while seq.next_element::<IgnoredAny>()?.is_some() {
                n += 1;
            }
            self.capture.addressable.push((self.dotted, n));
            return Ok(());
        }
        self.capture.found_array = true;
        match self.mode {
            KeepMode::Head(n) => {
                while let Some(raw) = seq.next_element::<Box<RawValue>>()? {
                    self.capture.entry_count += 1;
                    if self.capture.kept.len() < *n {
                        self.capture.kept.push(raw);
                    }
                }
            }
            KeepMode::Ids(ids) => {
                while let Some(raw) = seq.next_element::<Box<RawValue>>()? {
                    self.capture.entry_count += 1;
                    let id = string_id(&raw);
                    if let Some(ref id) = id {
                        self.capture.all_ids.push(id.clone());
                        if ids.iter().any(|req| id == req || id.ends_with(req)) {
                            self.capture.kept.push(raw);
                        }
                    }
                }
            }
        }
        self.capture
            .addressable
            .push((self.dotted, self.capture.entry_count));
        Ok(())
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<(), A::Error> {
        while map.next_entry::<IgnoredAny, IgnoredAny>()?.is_some() {}
        Ok(())
    }

    fn visit_unit<E: serde::de::Error>(self) -> Result<(), E> {
        Ok(())
    }

    fn visit_none<E: serde::de::Error>(self) -> Result<(), E> {
        Ok(())
    }

    fn visit_bool<E: serde::de::Error>(self, _: bool) -> Result<(), E> {
        Ok(())
    }

    fn visit_i64<E: serde::de::Error>(self, _: i64) -> Result<(), E> {
        Ok(())
    }

    fn visit_u64<E: serde::de::Error>(self, _: u64) -> Result<(), E> {
        Ok(())
    }

    fn visit_f64<E: serde::de::Error>(self, _: f64) -> Result<(), E> {
        Ok(())
    }

    fn visit_str<E: serde::de::Error>(self, _: &str) -> Result<(), E> {
        Ok(())
    }
}

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

    #[test]
    fn arena_target_parses_shorthand_and_dotted() {
        match ArenaTarget::parse("faces").unwrap() {
            ArenaTarget::Model { arena } => assert_eq!(arena, "faces"),
            ArenaTarget::Native { .. } => panic!("expected model"),
        }
        match ArenaTarget::parse("native.creo.curve_parameters").unwrap() {
            ArenaTarget::Native { codec, arena } => {
                assert_eq!(codec, "creo");
                assert_eq!(arena, "curve_parameters");
            }
            ArenaTarget::Model { .. } => panic!("expected native"),
        }
    }

    #[test]
    fn field_cell_escapes_tab_and_compacts_arrays() {
        assert_eq!(field_cell(&serde_json::json!("a\tb")), "a\\tb".to_owned());
        assert_eq!(field_cell(&serde_json::json!([1, 2])), "[1,2]".to_owned());
    }
}