alix 0.8.0

A learning tool built for understanding, not just remembering.
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
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
use std::{collections::HashSet, path::Path};

use serde::{Deserialize, Serialize};

use super::{Browsing, Examining, Reviewing, Walking, catalog::img_key};
use crate::{
    answer::{Input, Mode, mode_name},
    augment::AugmentCache,
    config::{AskConfig, Bindings, BrowseBindings, Key, KeyPattern, PickerKeys, Strictness},
    deck::{self, Deck, DeckState},
    depth::{Depth, depth_name},
    doctor, exam,
    inline::{DisplayProjector, InlineRun},
    render::ContentUnit,
    review::{self, CardView},
    session::{Cell, now_ms},
    source::{Excerpt, relabel_for_display},
    store::Store,
    trace::{Delta, Phase},
};

#[derive(Debug, Serialize)]
pub(super) struct CardDto {
    pub(super) id: Option<String>,
    pub(super) front: String,
    pub(super) front_runs: Vec<InlineRun>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(super) front_units: Option<Vec<ContentUnit>>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(super) section_context: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(super) section_context_runs: Vec<Vec<InlineRun>>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(super) section_context_units: Vec<ContentUnit>,
    pub(super) context: Vec<String>,
    pub(super) context_leads: bool,
    pub(super) context_runs: Vec<Vec<InlineRun>>,
    pub(super) context_units: Vec<ContentUnit>,
    pub(super) back: Vec<String>,
    pub(super) back_runs: Vec<Vec<InlineRun>>,
    pub(super) back_units: Vec<ContentUnit>,
    pub(super) answer_steps: Vec<crate::render::AnswerStep>,
    pub(super) reshaped: bool,
    pub(super) note: Vec<NoteDto>,
    pub(super) images: Vec<ImageDto>,
    pub(super) images_back: Vec<ImageDto>,
    pub(super) citations: Vec<CitationDto>,
    pub(super) crumb: Option<CrumbDto>,
}

/// One note on the wire. `badge` is absent for a note no blockquote opened
/// (a table's note column, an augmentation, a personal note).
#[derive(Debug, Serialize)]
pub(super) struct NoteDto {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) badge: Option<crate::card::Badge>,
    pub(super) units: Vec<ContentUnit>,
}

#[derive(Debug, Serialize)]
pub(super) struct CitationDto {
    pub(super) locator: String,
    pub(super) excerpt: Option<ExcerptDto>,
    pub(super) error: Option<String>,
}

#[derive(Debug, Serialize)]
pub(super) struct ImageDto {
    pub(super) src: String,
    pub(super) alt: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(super) regions: Vec<review::RegionView>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) crop: Option<review::CropView>,
}

#[derive(Debug, Serialize)]
pub(super) struct CrumbDto {
    pub(super) regions: Vec<String>,
    pub(super) current: usize,
    pub(super) cells: Vec<Vec<Cell>>,
}

#[derive(Debug, Serialize, Default)]
pub(super) struct DeckDrawerDto {
    pub(super) description: Option<String>,
    pub(super) heatmap: Vec<Cell>,
    pub(super) topologies: Vec<TopologyInfoDto>,
    /// Total cards in the deck. Not derivable from `heatmap.len()`, which counts
    /// only stamped cards.
    pub(super) total: usize,
    /// A nested funnel over `total`: `retired <= graduated <= seen <= total`.
    /// `seen` is any card with a store entry, `graduated` reaches FSRS review
    /// (surfaced to the user as "learned"), `retired` is past the retire cap.
    pub(super) seen: usize,
    pub(super) graduated: usize,
    pub(super) retired: usize,
}

#[derive(Debug, Serialize)]
pub(super) struct TopologyInfoDto {
    pub(super) name: String,
    pub(super) principle: String,
    pub(super) regions: Vec<RegionInfoDto>,
}

#[derive(Debug, Serialize)]
pub(super) struct RegionInfoDto {
    pub(super) name: String,
    pub(super) cells: Vec<Cell>,
}

#[derive(Debug, Serialize)]
pub(super) struct StateDto {
    pub(super) kind: &'static str,
    /// Monotonic identity of the current review transition. Card-relative
    /// mutations echo it in `X-Alix-Study-Revision`; a stale echo is a 409.
    pub(super) study_revision: u64,
    /// No separate `finished` flag: a finished session is just the `done` phase.
    pub(super) phase: &'static str,
    pub(super) card: Option<CardDto>,
    pub(super) choices: Option<Vec<String>>,
    pub(super) choices_multiple: Option<bool>,
    pub(super) choice_runs: Option<Vec<Vec<InlineRun>>>,
    pub(super) keypoints: Option<Vec<String>>,
    pub(super) keypoint_runs: Option<Vec<Vec<InlineRun>>>,
    pub(super) introducing: bool,
    pub(super) mode: &'static str,
    pub(super) depth: &'static str,
    pub(super) input: &'static str,
    pub(super) remaining: u32,
    pub(super) initial: u32,
    pub(super) reviews: u32,
    pub(super) passed: u32,
    pub(super) failed: u32,
    pub(super) introduced: u32,
    pub(super) partial: u32,
    pub(super) exam_due: Vec<String>,
    pub(super) can_restart: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) next_due_ms: Option<u64>,
    pub(super) due_left: u32,
    pub(super) new_left: u32,
    pub(super) met_total: u32,
    pub(super) deck_total: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) recognize_gap: Option<crate::session::RecognizeGap>,
    pub(super) label: String,
    /// The sitting's scope, so a client that re-selects this deck can ask for
    /// the same slice instead of silently widening to the whole deck.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) topology: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) region: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) save_error: Option<String>,
    /// Deck-load diagnostics for the open session (a stamped diagram that
    /// did not resolve); absent when there are none.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(super) load_warnings: Vec<String>,
}

#[derive(Debug, Serialize)]
pub(super) struct BrowseDto {
    pub(super) phase: &'static str,
    pub(super) label: String,
    pub(super) cards: Vec<CardDto>,
}

/// A deck inside a workspace stays out of `recent`: reachable only via its
/// workspace.
#[derive(Clone, Debug, Serialize)]
pub(super) struct DeckListDto {
    pub(super) workspaces: Vec<DeckItemDto>,
    pub(super) recent: Vec<DeckItemDto>,
    pub(super) folders: Vec<DeckItemDto>,
}

#[derive(Clone, Debug, Serialize)]
pub(super) struct DeckItemDto {
    pub(super) name: String,
    /// STRUCTURAL: whether `name` is a selectable deck row vs a
    /// workspace/folder group; unlike `reviewable*` this never changes with
    /// progress.
    pub(super) selectable: bool,
    pub(super) label: String,
    pub(super) meta: Option<String>,
    pub(super) state: &'static str,
    pub(super) locked: bool,
    pub(super) reviewable: bool,
    pub(super) reviewable_recognize: bool,
    pub(super) can_recognize: bool,
    pub(super) reviewable_recall: bool,
    pub(super) reviewable_reconstruct: bool,
    pub(super) mastered: bool,
    pub(super) is_trace: bool,
    pub(super) examable: bool,
    pub(super) has_exam: bool,
    pub(super) recent: bool,
    pub(super) is_workspace: bool,
    pub(super) description: Option<String>,
    pub(super) members: Vec<MemberDto>,
    pub(super) path: Option<String>,
    pub(super) icon: Option<String>,
    pub(super) icon_svg: bool,
    pub(super) has_topology: bool,
    pub(super) badge_depth: Option<&'static str>,
    pub(super) badge_dotted: bool,
    pub(super) new_cards: bool,
    pub(super) crammable: bool,
    pub(super) last_depth: &'static str,
    pub(super) deadline: Option<DeadlineDto>,
}

#[derive(Clone, Debug, Serialize)]
pub(super) struct DeadlineDto {
    pub(super) date: String, // ISO YYYY-MM-DD
    pub(super) days_left: i64,
    pub(super) ready: usize,
    pub(super) total: usize,
}

#[derive(Clone, Debug, Serialize)]
pub(super) struct MemberDto {
    pub(super) name: String,
    pub(super) selectable: bool,
    pub(super) label: String,
    pub(super) meta: Option<String>,
    pub(super) state: &'static str,
    pub(super) locked: bool,
    pub(super) reviewable: bool,
    pub(super) reviewable_recognize: bool,
    pub(super) can_recognize: bool,
    pub(super) reviewable_recall: bool,
    pub(super) reviewable_reconstruct: bool,
    pub(super) mastered: bool,
    pub(super) is_trace: bool,
    pub(super) examable: bool,
    pub(super) has_exam: bool,
    pub(super) indent: usize,
    pub(super) tree: String,
    pub(super) has_topology: bool,
    pub(super) badge_depth: Option<&'static str>,
    pub(super) badge_dotted: bool,
    pub(super) new_cards: bool,
    pub(super) crammable: bool,
    pub(super) last_depth: &'static str,
}

#[derive(Debug, Serialize)]
pub(super) struct ExchangeDto {
    pub(super) q: String,
    pub(super) a: String,
}

#[derive(Debug, Serialize)]
pub(super) struct AskDto {
    pub(super) transcript: Vec<ExchangeDto>,
    pub(super) thinking: bool,
    pub(super) status: Option<String>,
    pub(super) error: Option<String>,
    pub(super) draft: Option<DraftCardDto>,
}

#[derive(Debug, Clone, Serialize)]
pub(super) struct DraftCardDto {
    pub(super) front: String,
    pub(super) back: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub(super) struct CreateCardReq {
    pub(super) front: String,
    pub(super) back: Vec<String>,
}

#[derive(Debug, Serialize)]
pub(super) struct CreateCardResp {
    /// The card's identity token: `token`, or a suffixed `token-N`/`token-r`,
    /// the same string the store keys it by.
    pub(super) id: String,
}

/// `model`/`effort` show `"default"` unless `[ask]` pins one; never the model
/// that built the deck.
#[derive(Debug, Serialize)]
pub(super) struct AskInfoDto {
    pub(super) backend: &'static str,
    pub(super) model: String,
    pub(super) effort: String,
}

#[derive(Serialize)]
pub(super) struct VersionDto {
    pub(super) version: &'static str,
}

#[derive(Serialize)]
pub(super) struct DoctorDto {
    pub(super) rows: Vec<DoctorRowDto>,
}

#[derive(Serialize)]
pub(super) struct DoctorRowDto {
    pub(super) name: &'static str,
    pub(super) status: &'static str,
    pub(super) detail: String,
    pub(super) remedy: Option<String>,
}

impl From<doctor::Finding> for DoctorRowDto {
    fn from(f: doctor::Finding) -> Self {
        DoctorRowDto {
            name: f.name,
            status: match f.status {
                doctor::Status::Ok => "ok",
                doctor::Status::Warn => "warn",
                doctor::Status::Fail => "fail",
            },
            detail: f.detail,
            remedy: f.remedy,
        }
    }
}

#[derive(Serialize)]
pub(super) struct PairDto {
    pub(super) url: String,
    pub(super) svg: Option<String>,
    pub(super) lan: bool,
}

#[derive(Serialize)]
pub(super) struct ResetDto {
    pub(super) deck: String,
    pub(super) cards_cleared: usize,
}

#[derive(Debug, Serialize)]
pub(super) struct RemovalPreviewDto {
    pub(super) target: String,
    pub(super) kind: &'static str,
    pub(super) decks: usize,
    pub(super) cards_with_progress: usize,
    pub(super) earliest_review_ms: Option<u64>,
    pub(super) files: Vec<String>,
    pub(super) directories: Vec<String>,
    pub(super) dependents: Vec<String>,
}

#[derive(Debug, Serialize)]
pub(super) struct RemovalDto {
    pub(super) target: String,
    pub(super) kind: &'static str,
    pub(super) removed: Vec<String>,
    pub(super) decks_removed: usize,
    pub(super) directory_removed: bool,
    pub(super) dependents: Vec<String>,
}

#[derive(Debug, Serialize)]
pub(super) struct RemovalFailureDto {
    pub(super) target: String,
    pub(super) error: &'static str,
    pub(super) completed: Vec<String>,
    pub(super) failed: String,
    pub(super) recovery: &'static str,
}

/// Unlike `generate`'s lenient save, a non-parsing upload is rejected outright.
#[derive(Serialize)]
pub(super) struct ImportDto {
    pub(super) deck: String,
    pub(super) cards: usize,
}

impl AskInfoDto {
    pub(super) fn from(cfg: &AskConfig) -> Self {
        let or_default = |s: Option<String>| s.unwrap_or_else(|| "default".to_string());
        let backend = cfg.backend.name();
        Self {
            backend,
            // A pinned model is known up front; otherwise report what the
            // backend itself said it loaded, rather than guessing for it.
            model: or_default(
                crate::backend::resolved_ask_model(cfg)
                    .or_else(|| crate::ask::observed_model(backend)),
            ),
            effort: or_default(crate::backend::resolved_ask_effort(cfg)),
        }
    }
}

#[derive(Debug, Serialize)]
pub(super) struct KeyDto {
    k: String,
    ctrl: bool,
}

pub(super) fn key_dto(p: &KeyPattern) -> KeyDto {
    let k = match p.key {
        Key::Char(' ') => " ".to_string(),
        Key::Char(c) => c.to_string(),
        Key::Enter => "Enter".to_string(),
        Key::Tab => "Tab".to_string(),
        Key::Esc => "Escape".to_string(),
        Key::Backspace => "Backspace".to_string(),
    };
    KeyDto { k, ctrl: p.ctrl }
}

pub(super) fn key_list(list: &[KeyPattern]) -> Vec<KeyDto> {
    list.iter().map(key_dto).collect()
}

#[derive(Debug, Serialize)]
pub(super) struct ReviewKeys {
    reveal: Vec<KeyDto>,
    failed: Vec<KeyDto>,
    partly: Vec<KeyDto>,
    passed: Vec<KeyDto>,
    up: Vec<KeyDto>,
    down: Vec<KeyDto>,
    skip: Vec<KeyDto>,
    remove: Vec<KeyDto>,
    restart: Vec<KeyDto>,
    ask: Vec<KeyDto>,
    context: Vec<KeyDto>,
    make_note: Vec<KeyDto>,
    make_card: Vec<KeyDto>,
}

impl ReviewKeys {
    pub(super) fn from(b: &Bindings) -> Self {
        Self {
            reveal: key_list(&b.reveal),
            failed: key_list(&b.failed),
            partly: key_list(&b.partly),
            passed: key_list(&b.passed),
            up: key_list(&b.up),
            down: key_list(&b.down),
            skip: key_list(&b.skip),
            remove: key_list(&b.remove),
            restart: key_list(&b.restart),
            ask: key_list(&b.ask),
            context: key_list(&b.context),
            make_note: key_list(&b.make_note),
            make_card: key_list(&b.make_card),
        }
    }
}

#[cfg(test)]
mod review_keys_parity {
    /// Every review binding reaches the clients. `ReviewKeys` is hand-written
    /// beside `Bindings`, so a binding added to one and forgotten in the other
    /// is silent: the config accepts a key the web client never receives.
    /// Deliberate exclusions are listed, so dropping one is a decision.
    #[test]
    fn every_binding_is_either_served_or_deliberately_withheld() {
        // `Bindings` fields are `pub`, `ReviewKeys` fields are private, so the
        // scrape keys on the `name: Type,` shape both share.
        let fields = |source: &str, marker: &str| -> Vec<String> {
            let at = source
                .find(marker)
                .expect("the struct moved or was renamed");
            let body = &source[at..];
            let end = body.find("\n}").expect("the struct closes");
            body[..end]
                .lines()
                .skip(1)
                .map(|line| line.trim().trim_start_matches("pub "))
                .filter(|line| line.ends_with(',') && !line.starts_with("//"))
                .filter_map(|line| line.split_once(": "))
                .map(|(name, _)| name.to_string())
                .collect()
        };
        let bindings = fields(include_str!("../config.rs"), "pub struct Bindings {");
        let served: Vec<String> = fields(include_str!("dto.rs"), "struct ReviewKeys {")
            .into_iter()
            .collect();
        // Terminal-only actions: the browser has its own affordances for
        // these and never binds them.
        let withheld = ["submit", "cont", "quit"];

        assert!(bindings.len() > 10, "the scrape found {bindings:?}");
        for name in &bindings {
            assert!(
                served.contains(name) || withheld.contains(&name.as_str()),
                "binding `{name}` is neither served in ReviewKeys nor listed as \
                 deliberately withheld"
            );
        }
        for name in &served {
            assert!(
                bindings.contains(name),
                "ReviewKeys serves `{name}`, which is not a binding"
            );
        }
    }
}

#[derive(Debug, Serialize)]
pub(super) struct PickerKeysDto {
    up: Vec<KeyDto>,
    down: Vec<KeyDto>,
    open: Vec<KeyDto>,
    back: Vec<KeyDto>,
    filter: Vec<KeyDto>,
    mastered: Vec<KeyDto>,
    depth: Vec<KeyDto>,
    recognize: Vec<KeyDto>,
    recall: Vec<KeyDto>,
    reconstruct: Vec<KeyDto>,
    cram: Vec<KeyDto>,
}

impl PickerKeysDto {
    pub(super) fn from(k: &PickerKeys) -> Self {
        Self {
            up: key_list(&k.up),
            down: key_list(&k.down),
            open: key_list(&k.open),
            back: key_list(&k.back),
            filter: key_list(&k.filter),
            mastered: key_list(&k.mastered),
            depth: key_list(&k.depth),
            recognize: key_list(&k.recognize),
            recall: key_list(&k.recall),
            reconstruct: key_list(&k.reconstruct),
            cram: key_list(&k.cram),
        }
    }
}

#[derive(Debug, Serialize)]
pub(super) struct BrowseKeys {
    next: Vec<KeyDto>,
    prev: Vec<KeyDto>,
    remove: Vec<KeyDto>,
}

impl BrowseKeys {
    pub(super) fn from(b: &BrowseBindings) -> Self {
        Self {
            next: key_list(&b.next),
            prev: key_list(&b.prev),
            remove: key_list(&b.remove),
        }
    }
}

#[derive(Serialize)]
pub(super) struct ExamDto {
    pub(super) phase: &'static str,
    pub(super) deck: String,
    pub(super) strictness: &'static str,
    pub(super) total: usize,
    pub(super) current: usize,
    pub(super) question: Option<String>,
    pub(super) answer: String,
    pub(super) on_last: bool,
    pub(super) grades: Vec<ExamGradeDto>,
    pub(super) passed: Option<bool>,
    pub(super) gaps: Vec<String>,
    /// A trace deck is re-walked on fail, never remediated (fact decks only).
    pub(super) can_remediate: bool,
    pub(super) remediated_count: Option<usize>,
    pub(super) is_trace: bool,
    pub(super) unlocks: Vec<String>,
    pub(super) thinking: bool,
    pub(super) error: Option<String>,
    pub(super) elapsed: Option<u64>,
    pub(super) cooldown_ms: Option<u64>,
}

pub(super) fn cooldown_dto(deck: &str, cooldown_ms: u64) -> ExamDto {
    ExamDto {
        phase: "cooldown",
        deck: deck.to_string(),
        strictness: "balanced",
        total: 0,
        current: 0,
        question: None,
        answer: String::new(),
        on_last: false,
        grades: Vec::new(),
        passed: None,
        gaps: Vec::new(),
        can_remediate: false,
        remediated_count: None,
        is_trace: true,
        unlocks: Vec::new(),
        thinking: false,
        error: None,
        elapsed: None,
        cooldown_ms: Some(cooldown_ms),
    }
}

#[derive(Serialize)]
pub(super) struct ExamGradeDto {
    pub(super) question: String,
    pub(super) points: Vec<String>,
    pub(super) answer: String,
    pub(super) verdict: &'static str,
    pub(super) feedback: String,
    pub(super) missed: Vec<String>,
}

pub(super) fn exam_phase_name(phase: &exam::Phase) -> &'static str {
    match phase {
        exam::Phase::Generating => "generating",
        exam::Phase::Answering => "answering",
        exam::Phase::Grading => "grading",
        exam::Phase::Results => "results",
        exam::Phase::Remediating => "remediating",
        exam::Phase::Remediated => "remediated",
    }
}

pub(super) fn strictness_name(s: Strictness) -> &'static str {
    match s {
        Strictness::Strict => "strict",
        Strictness::Balanced => "balanced",
        Strictness::Lenient => "lenient",
    }
}

pub(super) fn exam_dto(ex: &Examining) -> ExamDto {
    let s = &ex.sitting;
    let result = s.result();
    let grades = result
        .map(|r| {
            s.questions()
                .iter()
                .zip(s.answers())
                .zip(&r.grades)
                .map(|((q, a), g)| ExamGradeDto {
                    question: q.prompt.clone(),
                    points: q.points.clone(),
                    answer: a.clone(),
                    verdict: g.verdict.label(),
                    feedback: g.feedback.clone(),
                    missed: g.missed.clone(),
                })
                .collect()
        })
        .unwrap_or_default();
    let passed = result.map(|r| r.passed);
    let unlocks = if passed == Some(true) {
        deck::dependents(&ex.deck_path)
    } else {
        Vec::new()
    };
    ExamDto {
        phase: exam_phase_name(s.phase()),
        deck: s.subject().to_string(),
        strictness: strictness_name(s.strictness()),
        total: s.total(),
        current: s.current_index(),
        question: s.question().map(|q| q.prompt.clone()),
        answer: s.answer().to_string(),
        on_last: s.on_last(),
        grades,
        passed,
        gaps: s.gaps(),
        can_remediate: s.can_remediate(),
        remediated_count: s.remediated_count(),
        is_trace: s.kind() == exam::SittingKind::Trace,
        unlocks,
        thinking: s.thinking(),
        error: s.error().map(str::to_string),
        elapsed: s.elapsed_secs(),
        cooldown_ms: None,
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct RemoteCard {
    pub(super) subject: String,
    pub(super) front: String,
    pub(super) back: Vec<String>,
    pub(super) at: Option<String>,
}

#[derive(Debug, Deserialize)]
pub(super) struct RemoteTurn {
    pub(super) q: String,
    pub(super) a: String,
}

#[derive(Debug, Deserialize)]
pub(super) struct RemoteAskReq {
    pub(super) card: RemoteCard,
    pub(super) history: Vec<RemoteTurn>,
    pub(super) question: String,
}

#[derive(Debug, Deserialize)]
pub(super) struct RemoteDraftReq {
    pub(super) card: RemoteCard,
    pub(super) history: Vec<RemoteTurn>,
}

#[derive(Debug, Deserialize)]
pub(super) struct RemoteNoteReq {
    pub(super) card: RemoteCard,
    pub(super) history: Vec<RemoteTurn>,
}

#[derive(Debug, Serialize)]
pub(super) struct RemoteAskDto {
    pub(super) thinking: bool,
    pub(super) answer: Option<String>,
    pub(super) draft: Option<DraftCardDto>,
    /// An empty vec is a valid settled result ("nothing to save"), not an error.
    pub(super) note: Option<Vec<String>>,
    pub(super) error: Option<String>,
    pub(super) elapsed: Option<u64>,
}

#[derive(Serialize)]
pub(super) struct RemoteExamDto {
    pub(super) phase: &'static str,
    pub(super) deck: String,
    pub(super) strictness: &'static str,
    /// Prompts only: the rubric never leaves the server.
    pub(super) questions: Vec<String>,
    pub(super) passed: Option<bool>,
    pub(super) grades: Vec<ExamGradeDto>,
    pub(super) gaps: Vec<String>,
    pub(super) can_remediate: bool,
    pub(super) cards: Option<String>,
    pub(super) is_trace: bool,
    pub(super) thinking: bool,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
}

#[derive(Serialize)]
pub(super) struct RemoteGenerateDto {
    pub(super) phase: &'static str,
    pub(super) deck: Option<String>,
    pub(super) filename: Option<String>,
    /// Unlike `GenerateDto`, a parse failure here doesn't flip `phase` to
    /// `error`: nothing is saved either way.
    pub(super) cards: Option<usize>,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
}

#[derive(Serialize)]
pub(super) struct AugmentDto {
    pub(super) deck: String,
    pub(super) cards: usize,
    pub(super) rows: Vec<AugmentRowDto>,
    pub(super) busy: Option<&'static str>,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
    pub(super) queued: Vec<&'static str>,
    pub(super) done: Vec<&'static str>,
    /// Partial-failure safe: one target's error doesn't stop the rest.
    pub(super) failed: Vec<FailedTargetDto>,
}

#[derive(Debug, Clone, Serialize)]
pub(super) struct FailedTargetDto {
    pub(super) target: &'static str,
    pub(super) error: String,
}

#[derive(Serialize)]
pub(super) struct AugmentRowDto {
    pub(super) kind: &'static str,
    pub(super) label: &'static str,
    pub(super) covered: usize,
    pub(super) eligible: usize,
    pub(super) items: Vec<String>,
    pub(super) busy: bool,
}

#[derive(Serialize)]
pub(super) struct GenerateDto {
    pub(super) phase: &'static str,
    pub(super) deck: Option<String>,
    pub(super) cards: Option<usize>,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
}

#[derive(Serialize)]
pub(super) struct ShareDto {
    pub(super) phase: &'static str,
    pub(super) code: Option<String>,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
}

#[derive(Serialize)]
pub(super) struct ReceiveDto {
    pub(super) phase: &'static str,
    pub(super) landed: Option<String>,
    pub(super) stripped: Vec<String>,
    pub(super) elapsed: Option<u64>,
    pub(super) error: Option<String>,
}

#[derive(Serialize)]
pub(super) struct HopDto {
    pub(super) prompt: String,
    pub(super) delta: Option<&'static str>,
    pub(super) current: bool,
}

#[derive(Debug, Serialize)]
pub(super) struct ExcerptDto {
    pub(super) path: String,
    pub(super) lines: Vec<LineDto>,
    pub(super) truncated: bool,
}

#[derive(Debug, Serialize)]
pub(super) struct LineDto {
    pub(super) n: usize,
    pub(super) text: String,
}

#[derive(Serialize)]
pub(super) struct SummaryDto {
    pub(super) passed: usize,
    pub(super) partly: usize,
    pub(super) failed: usize,
    pub(super) weak: Vec<usize>,
    pub(super) total: usize,
}

#[derive(Serialize)]
pub(super) struct WalkDto {
    pub(super) kind: &'static str,
    pub(super) phase: &'static str,
    pub(super) description: String,
    pub(super) description_runs: Vec<InlineRun>,
    pub(super) source: Option<String>,
    pub(super) total: usize,
    pub(super) current: usize,
    pub(super) path: Vec<HopDto>,
    pub(super) prompt: Option<String>,
    pub(super) prompt_runs: Option<Vec<InlineRun>>,
    pub(super) givens: Vec<String>,
    pub(super) given_runs: Vec<Vec<InlineRun>>,
    pub(super) locator: Option<String>,
    pub(super) prediction: Option<String>,
    pub(super) excerpt: Option<ExcerptDto>,
    pub(super) excerpt_error: Option<String>,
    pub(super) points: Vec<String>,
    pub(super) point_runs: Vec<Vec<InlineRun>>,
    pub(super) note: Option<String>,
    pub(super) note_runs: Option<Vec<InlineRun>>,
    pub(super) summary: Option<SummaryDto>,
}

pub(super) fn walk_phase_name(phase: Phase) -> &'static str {
    match phase {
        Phase::Predict => "predict",
        Phase::Reveal => "reveal",
        Phase::Done => "done",
    }
}

pub(super) fn delta_name(delta: Delta) -> &'static str {
    match delta {
        Delta::Passed => "passed",
        Delta::Partial => "partly",
        Delta::Failed => "failed",
    }
}

pub(super) fn excerpt_dto(excerpt: &Excerpt) -> ExcerptDto {
    ExcerptDto {
        path: excerpt.path.display().to_string(),
        lines: excerpt
            .lines
            .iter()
            .map(|(n, text)| LineDto {
                n: *n,
                text: text.clone(),
            })
            .collect(),
        truncated: excerpt.truncated,
    }
}

pub(super) fn walk_dto(w: &Walking) -> WalkDto {
    let walk = &w.walk;
    let trace = walk.trace();
    let phase = walk.phase();
    let on_a_hop = matches!(phase, Phase::Predict | Phase::Reveal);
    let mut projector = DisplayProjector::default();

    let path = trace
        .checkpoints
        .iter()
        .enumerate()
        .map(|(i, c)| HopDto {
            prompt: c.prompt.clone(),
            delta: walk.delta(i).map(delta_name),
            current: on_a_hop && i == walk.current_index(),
        })
        .collect();

    let mut dto = WalkDto {
        kind: "walk",
        phase: walk_phase_name(phase),
        description: trace.description.clone(),
        description_runs: projector.project(&trace.description),
        source: trace.source.clone(),
        total: walk.total(),
        current: walk.current_index() + 1,
        path,
        prompt: None,
        prompt_runs: None,
        givens: Vec::new(),
        given_runs: Vec::new(),
        locator: None,
        prediction: None,
        excerpt: None,
        excerpt_error: None,
        points: Vec::new(),
        point_runs: Vec::new(),
        note: None,
        note_runs: None,
        summary: None,
    };

    match phase {
        Phase::Predict => {
            if let Some(c) = walk.checkpoint() {
                dto.prompt = Some(c.prompt.clone());
                dto.prompt_runs = Some(projector.project(&c.prompt));
                dto.givens = c.givens.clone();
                dto.given_runs = c
                    .givens
                    .iter()
                    .map(|given| projector.project(given))
                    .collect();
                dto.locator = c.locator.clone();
            }
        }
        Phase::Reveal => {
            if let Some(c) = walk.checkpoint() {
                dto.prompt = Some(c.prompt.clone());
                dto.prompt_runs = Some(projector.project(&c.prompt));
                dto.givens = c.givens.clone();
                dto.given_runs = c
                    .givens
                    .iter()
                    .map(|given| projector.project(given))
                    .collect();
                dto.locator = c.locator.clone();
                dto.points = c.points.clone();
                dto.point_runs = c
                    .points
                    .iter()
                    .map(|point| projector.project(point))
                    .collect();
                dto.note = c.note.clone();
                dto.note_runs = c.note.as_deref().map(|note| projector.project(note));
                match trace.excerpt(c) {
                    Ok(ex) => {
                        // Repoint a frozen excerpt's asset path at the real
                        // `at:` source path for display.
                        let ex =
                            if let Some(at) = c.locator.as_deref().filter(|_| c.asset.is_some()) {
                                let (ex, label) = relabel_for_display(ex, at);
                                if let Some(label) = label {
                                    dto.locator = Some(label);
                                }
                                ex
                            } else {
                                ex
                            };
                        dto.excerpt = Some(excerpt_dto(&ex.capped_for_display()));
                    }
                    Err(e) => dto.excerpt_error = Some(format!("{e:#}")),
                }
            }
            dto.prediction = walk
                .prediction(walk.current_index())
                .map(str::to_string)
                .filter(|p| !p.is_empty());
        }
        Phase::Done => {
            let s = walk.summary();
            dto.summary = Some(SummaryDto {
                passed: s.passed,
                partly: s.partly,
                failed: s.failed,
                weak: s.weak.iter().map(|i| i + 1).collect(),
                total: walk.total(),
            });
        }
    }
    dto
}

pub(super) fn browse_payload(browsing: Option<&Browsing>) -> BrowseDto {
    match browsing {
        Some(b) => {
            let mut projector = DisplayProjector::default();
            BrowseDto {
                phase: "browse",
                label: b.label.clone(),
                cards: b
                    .cards
                    .iter()
                    .map(|card| card_dto(CardView::project(card, &mut projector), card.id()))
                    .collect(),
            }
        }
        None => BrowseDto {
            phase: "select",
            label: "select decks".to_string(),
            cards: Vec::new(),
        },
    }
}

/// A choice card's options are seeded by the card id plus its appearance
/// count, so they're stable across `/api/state` and `/api/choose` yet
/// reshuffle next time the card is served.
pub(super) fn review_state(
    reviewing: Option<&Reviewing>,
    store: &Store,
    save_error: Option<&str>,
    study_revision: u64,
) -> StateDto {
    let Some(r) = reviewing else {
        return StateDto {
            kind: "review",
            study_revision,
            phase: "select",
            card: None,
            choices: None,
            choices_multiple: None,
            choice_runs: None,
            keypoints: None,
            keypoint_runs: None,
            introducing: false,
            mode: mode_name(Mode::default()),
            depth: depth_name(Depth::default()),
            input: input_name(Input::default()),
            remaining: 0,
            initial: 0,
            reviews: 0,
            passed: 0,
            failed: 0,
            introduced: 0,
            partial: 0,
            exam_due: Vec::new(),
            can_restart: false,
            next_due_ms: None,
            due_left: 0,
            new_left: 0,
            met_total: 0,
            deck_total: 0,
            recognize_gap: None,
            label: "select decks".to_string(),
            topology: None,
            region: None,
            save_error: save_error.map(str::to_string),
            load_warnings: Vec::new(),
        };
    };
    let session = &r.session;
    // Every fact here comes from the shared `crate::review` contract, the same
    // state embedded mobile renders; this envelope only adds wire naming and
    // serve-held context.
    let s = review::state(session, store, &r.augment, None);
    // Only computed when finished: it reloads decks, so this stays off the hot path.
    let exam_due = if s.finished {
        // `r.files.paths` is keyed by deck_id (routing only); the wire value
        // clients resolve `/api/exam/start` by is the deck's own name, read
        // back off the loaded deck, not the map key.
        let mut due: Vec<String> = r
            .files
            .paths
            .values()
            .filter_map(|path| {
                Deck::load(path)
                    .ok()
                    .filter(|d| d.state(store) == DeckState::ExamDue)
                    .map(|d| d.subject)
            })
            .collect();
        due.sort();
        due
    } else {
        Vec::new()
    };
    let card_with_citation = s.card.zip(session.current()).map(|(view, c)| {
        let mut dto = card_dto(view, c.id());
        // A cache can hold several like-named topologies (decks sharing a
        // store); the card id disambiguates which one actually applies.
        if let Some(name) = &r.topology_name
            && let Some((topo, regions, current)) = r
                .augment
                .topologies()
                .iter()
                .filter(|t| t.name == *name)
                .find_map(|t| {
                    c.id()
                        .as_deref()
                        .and_then(|id| t.region_path(id))
                        .map(|(rg, cur)| (t, rg, cur))
                })
        {
            let locks = session.locks(store);
            let locked = locks.locked_ids_everywhere();
            dto.crumb = Some(CrumbDto {
                regions: regions.into_iter().map(str::to_string).collect(),
                current,
                cells: topo
                    .regions
                    .iter()
                    .map(|reg| {
                        crate::session::card_cells(
                            &reg.cards,
                            locked,
                            store,
                            now_ms(),
                            session.retire_after_days(),
                        )
                    })
                    .collect(),
            });
        }
        dto.citations = c
            .citations
            .iter()
            .map(|citation| {
                let mut resolved = CitationDto {
                    locator: citation.locator.clone(),
                    excerpt: None,
                    error: None,
                };
                if let Some(base) = r.source_bases.get(&*c.deck_id) {
                    match base.checked_excerpt(citation) {
                        Ok(ex) => {
                            // Repoint a frozen excerpt's asset path at the real
                            // `at:` source path, so the citation reads
                            // `store.rs:36-66`, not the asset object's path.
                            let ex = if citation.asset.is_some() {
                                let (ex, label) = relabel_for_display(ex, &citation.locator);
                                if let Some(label) = label {
                                    resolved.locator = label;
                                }
                                ex
                            } else {
                                ex
                            };
                            resolved.excerpt = Some(excerpt_dto(&ex.capped_for_display()));
                        }
                        Err(e) => resolved.error = Some(format!("{e:#}")),
                    }
                }
                resolved
            })
            .collect();
        dto
    });
    StateDto {
        kind: "review",
        study_revision,
        phase: if s.finished { "done" } else { "review" },
        card: card_with_citation,
        choices: s.choices,
        choices_multiple: s.choices_multiple,
        choice_runs: s.choice_runs,
        keypoints: s.keypoints,
        keypoint_runs: s.keypoint_runs,
        introducing: s.introducing,
        mode: mode_name(s.mode),
        depth: depth_name(s.depth),
        input: input_name(s.input),
        remaining: s.remaining,
        initial: s.initial,
        reviews: s.reviews,
        passed: s.passed,
        failed: s.failed,
        introduced: s.introduced,
        partial: s.partial,
        exam_due,
        can_restart: s.can_restart,
        next_due_ms: s.next_due_ms,
        due_left: s.due_left,
        new_left: s.new_left,
        met_total: s.met_total,
        deck_total: s.deck_total,
        recognize_gap: s.recognize_gap,
        label: r.label.clone(),
        topology: r.topology_name.clone(),
        region: r.region_name.clone(),
        save_error: save_error.map(str::to_string),
        load_warnings: r.load_warnings.clone(),
    }
}

pub(super) fn state_name(s: DeckState) -> &'static str {
    match s {
        DeckState::NotStarted => "new",
        DeckState::Started => "started",
        DeckState::Finished => "finished",
        DeckState::ExamDue => "examdue",
    }
}

pub(super) fn deck_drawer_dto(
    augment: &AugmentCache,
    store: &Store,
    deck: &Deck,
    retire_after_days: Option<u32>,
) -> DeckDrawerDto {
    let deck_tokens: HashSet<String> = deck.deck_token.iter().cloned().collect();
    let now = now_ms();
    // A flat per-card heatmap over the whole deck, in file order; a topology (if
    // any) re-groups the same signal into named regions below. The learned
    // bands are pinned to Recall retrievability: a deck-wide signal, not
    // per-session.
    let ids: Vec<String> = deck.cards.iter().filter_map(|c| c.id()).collect();
    let locked = crate::session::LockGraph::build(&deck.cards)
        .evaluate(store)
        .locked_ids(&deck.cards);
    let heatmap = crate::session::card_cells(&ids, &locked, store, now, retire_after_days);
    let topologies = augment
        .topologies_for(&deck_tokens)
        .into_iter()
        .map(|t| TopologyInfoDto {
            name: t.name.clone(),
            principle: t.principle.clone(),
            regions: t
                .regions
                .iter()
                .map(|r| RegionInfoDto {
                    name: r.name.clone(),
                    cells: crate::session::card_cells(
                        &r.cards,
                        &locked,
                        store,
                        now,
                        retire_after_days,
                    ),
                })
                .collect(),
        })
        .collect();
    let seen = deck
        .cards
        .iter()
        .filter(|c| c.id().and_then(|id| store.get(&id)).is_some())
        .count();
    let graduated = deck
        .cards
        .iter()
        .filter(|c| crate::session::has_graduated(c, store))
        .count();
    let retired = deck
        .cards
        .iter()
        .filter(|c| crate::session::is_retired(c, store, retire_after_days))
        .count();
    DeckDrawerDto {
        description: deck.description.clone(),
        heatmap,
        topologies,
        total: deck.cards.len(),
        seen,
        graduated,
        retired,
    }
}

/// A Diagram unit's lib-side `src` is an absolute file path (the mobile
/// projection reads it directly); over HTTP it becomes the same opaque
/// `/img/<key>` URL every card image uses.
fn web_units(units: Vec<crate::render::ContentUnit>) -> Vec<crate::render::ContentUnit> {
    units
        .into_iter()
        .map(|unit| match unit {
            crate::render::ContentUnit::Diagram {
                src,
                width,
                height,
                alt,
                regions,
                revealed_alt,
            } => crate::render::ContentUnit::Diagram {
                src: format!("/img/{}", img_key(Path::new(&src))),
                width,
                height,
                alt,
                regions,
                revealed_alt,
            },
            other => other,
        })
        .collect()
}

pub(super) fn card_dto(view: CardView, id: Option<String>) -> CardDto {
    let img_dto = |i: &review::ImageView| ImageDto {
        src: format!("/img/{}", img_key(Path::new(&i.src))),
        alt: i.alt.clone(),
        regions: i.regions.clone(),
        crop: i.crop.clone(),
    };
    CardDto {
        id,
        images: view.images.iter().map(&img_dto).collect(),
        images_back: view.images_back.iter().map(&img_dto).collect(),
        front: view.front,
        front_runs: view.front_runs,
        front_units: view.front_units.map(web_units),
        section_context: view.section_context,
        section_context_runs: view.section_context_runs,
        section_context_units: web_units(view.section_context_units),
        context: view.context,
        context_leads: view.context_leads,
        context_runs: view.context_runs,
        context_units: web_units(view.context_units),
        back: view.back,
        back_runs: view.back_runs,
        back_units: web_units(view.back_units),
        answer_steps: view
            .answer_steps
            .into_iter()
            .map(|step| match step {
                crate::render::AnswerStep::Quote {
                    back_from,
                    back_to,
                    units,
                } => crate::render::AnswerStep::Quote {
                    back_from,
                    back_to,
                    units: web_units(units),
                },
                line => line,
            })
            .collect(),
        reshaped: view.reshaped,
        note: view
            .note
            .into_iter()
            .map(|note| NoteDto {
                badge: note.badge,
                units: web_units(note.units),
            })
            .collect(),
        citations: view
            .citations
            .into_iter()
            .map(|locator| CitationDto {
                locator,
                excerpt: None,
                error: None,
            })
            .collect(),
        crumb: None,
    }
}

pub(super) fn input_name(input: Input) -> &'static str {
    match input {
        Input::Type => "type",
        Input::Draw => "draw",
    }
}

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

    #[test]
    fn deck_drawer_total_counts_unstamped_cards_the_heatmap_omits() {
        let dir = tempfile::tempdir().unwrap();
        let deck_path = dir.path().join("rust.md");
        std::fs::write(
            &deck_path,
            "---\nformat-version: 1\nid: \"deck-rust\"\n---\n## q1\na1\n<!-- id: card-q1 -->\n## q2\na2\n",
        )
        .unwrap();
        let deck = Deck::load(&deck_path).unwrap();
        let store = Store::open(dir.path().join("deck1.json")).unwrap();
        let augment = AugmentCache::open(dir.path().join("deck1-generated.json"));

        let dto = deck_drawer_dto(&augment, &store, &deck, None);

        assert_eq!(2, dto.total, "both cards count toward the deck size");
        assert_eq!(
            1,
            dto.heatmap.len(),
            "only the stamped card lands in the heatmap"
        );
        assert_eq!(
            (0, 0, 0),
            (dto.seen, dto.graduated, dto.retired),
            "a fresh deck's funnel is all zeros (each hidden client-side)"
        );
    }

    #[test]
    fn deck_drawer_funnel_counts_nest_total_seen_graduated_retired() {
        use crate::scheduler::{Fsrs, Grade, Scheduler};

        let dir = tempfile::tempdir().unwrap();
        let deck_path = dir.path().join("rust.md");
        std::fs::write(
            &deck_path,
            "---\nformat-version: 1\nid: \"deck-rust\"\n---\n\
             ## a\n1\n<!-- id: card-a -->\n\
             ## b\n2\n<!-- id: card-b -->\n\
             ## c\n3\n<!-- id: card-c -->\n\
             ## d\n4\n<!-- id: card-d -->\n",
        )
        .unwrap();
        let deck = Deck::load(&deck_path).unwrap();
        let augment = AugmentCache::open(dir.path().join("deck1-generated.json"));
        let mut store = Store::open(dir.path().join("deck1.json")).unwrap();
        let now = 1_000_000;
        let sched = Fsrs::default();

        // a: untouched (no store entry) → unseen.
        // b: a bare store entry → seen only.
        store.get_or_insert(&deck.cards[1].id().unwrap());
        // c: two real Recall Goods → graduated (state 2) at a sub-cap interval.
        let c = store.get_or_insert(&deck.cards[2].id().unwrap());
        sched.apply(c, Depth::Recall, Grade::Pass, now, false);
        sched.apply(c, Depth::Recall, Grade::Pass, now, false);
        assert!(
            c.schedule(Depth::Recall).is_some_and(|f| f.graduated()),
            "two Goods must graduate the card"
        );
        // d: a matured Review state past the retire cap → retired (and graduated).
        store.get_or_insert(&deck.cards[3].id().unwrap()).recall = Some(crate::store::FsrsState {
            state: 2,
            stability: 400.0,
            scheduled_days: 400,
            due_ms: now + 400 * 86_400_000,
            ..Default::default()
        });

        let dto = deck_drawer_dto(&augment, &store, &deck, Some(365));

        assert_eq!(4, dto.total);
        assert_eq!(3, dto.seen, "b, c, d have store entries");
        assert_eq!(2, dto.graduated, "c and d reached FSRS review");
        assert_eq!(1, dto.retired, "only d is past the 365-day cap");
        assert!(
            dto.total >= dto.seen && dto.seen >= dto.graduated && dto.graduated >= dto.retired,
            "the funnel must nest: {} >= {} >= {} >= {}",
            dto.total,
            dto.seen,
            dto.graduated,
            dto.retired
        );
    }
}