brain-brew-formats 1.0.0-alpha.8

Implementation package: YAML and CrowdAnki codecs for Brain Brew; no public Rust API commitment
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
//! Include-preserving, validated Canonical Deck source editing.
//!
//! A document parses through the strict Canonical YAML codec, keeps YAML details
//! private, and offers closed typed edits. Emission canonicalizes the complete
//! root document. Unedited scalar include files are not emitted; a targeted
//! scalar include edit emits only that file. An edited structural media include
//! is canonicalized as a complete standalone media map.

use std::collections::BTreeMap;

use brain_brew_core::{CanonicalDeck, FieldValue, StableId};

use crate::canonical_yaml;
use crate::csv_note_source::{
    CsvCellProvenance, CsvNoteSourceDeclaration, CsvNoteSourceDescriptor,
    CsvNoteSourceMaterializer, CsvSourceFile, CsvSourceRequest, CsvSourceRequestKind,
    NoteSourceExpression, NoteSourceItem,
};
use crate::source_document::{
    EditLocation, ImageConversionReport, IncludeRequest, IncludeState, IncludedSource,
    SourceDocumentEmission, SourceDocumentError, SourceFile, SourceProvenance,
    convert_text_to_images, prepare_source,
};

/// Closed set of scalar Canonical Deck source locations supported by mutators.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CanonicalScalarTarget {
    DeckName,
    DeckDescription,
    DeckVariable(String),
    NoteTypeName {
        note_type_id: StableId,
    },
    NoteTypeVariable {
        note_type_id: StableId,
        key: String,
    },
    NoteTypeStyling {
        note_type_id: StableId,
    },
    FieldName {
        note_type_id: StableId,
        field_id: StableId,
    },
    CardTemplateName {
        note_type_id: StableId,
        template_id: StableId,
    },
    CardTemplateQuestion {
        note_type_id: StableId,
        template_id: StableId,
    },
    CardTemplateAnswer {
        note_type_id: StableId,
        template_id: StableId,
    },
    CardTemplateVariable {
        note_type_id: StableId,
        template_id: StableId,
        key: String,
    },
    NoteVariable {
        note_id: StableId,
        key: String,
    },
    NoteField {
        note_id: StableId,
        field_id: StableId,
    },
}

impl CanonicalScalarTarget {
    fn schema_path(&self) -> String {
        match self {
            Self::DeckName => "deck.name".to_owned(),
            Self::DeckDescription => "deck.description".to_owned(),
            Self::DeckVariable(key) => format!("deck.variables.{key}"),
            Self::NoteTypeName { note_type_id } => format!("note_types.{note_type_id}.name"),
            Self::NoteTypeVariable { note_type_id, key } => {
                format!("note_types.{note_type_id}.variables.{key}")
            }
            Self::NoteTypeStyling { note_type_id } => {
                format!("note_types.{note_type_id}.styling")
            }
            Self::FieldName {
                note_type_id,
                field_id,
            } => format!("note_types.{note_type_id}.fields.{field_id}.name"),
            Self::CardTemplateName {
                note_type_id,
                template_id,
            } => format!("note_types.{note_type_id}.card_templates.{template_id}.name"),
            Self::CardTemplateQuestion {
                note_type_id,
                template_id,
            } => format!("note_types.{note_type_id}.card_templates.{template_id}.question_format"),
            Self::CardTemplateAnswer {
                note_type_id,
                template_id,
            } => format!("note_types.{note_type_id}.card_templates.{template_id}.answer_format"),
            Self::CardTemplateVariable {
                note_type_id,
                template_id,
                key,
            } => format!("note_types.{note_type_id}.card_templates.{template_id}.variables.{key}"),
            Self::NoteVariable { note_id, key } => format!("notes.{note_id}.variables.{key}"),
            Self::NoteField { note_id, field_id } => {
                format!("notes.{note_id}.fields.{field_id}")
            }
        }
    }
}

/// The authoring representation that owns a materialized canonical note path.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NoteAuthoringSourceKind {
    Inline,
    Csv,
}

/// Read-only authoring location for one materialized note or note field.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NoteAuthoringLocation {
    source_kind: NoteAuthoringSourceKind,
    root_declaration: SourceProvenance,
    declaration_path: String,
    descriptor: Option<SourceProvenance>,
    table: Option<String>,
    file: Option<SourceProvenance>,
    logical_row: Option<u64>,
    header: Option<String>,
    column: Option<usize>,
    canonical_path: String,
}

impl NoteAuthoringLocation {
    pub(crate) fn csv_field(
        root_declaration: SourceProvenance,
        declaration_path: String,
        descriptor: SourceProvenance,
        cell: &CsvCellProvenance,
        canonical_path: String,
    ) -> Self {
        Self {
            source_kind: NoteAuthoringSourceKind::Csv,
            root_declaration,
            declaration_path,
            descriptor: Some(descriptor),
            table: Some(cell.table_alias.clone()),
            file: Some(cell.source.clone()),
            logical_row: cell.logical_row,
            header: Some(cell.header.clone()),
            column: Some(cell.column),
            canonical_path,
        }
    }

    pub fn source_kind(&self) -> NoteAuthoringSourceKind {
        self.source_kind
    }

    pub fn root_declaration(&self) -> &SourceProvenance {
        &self.root_declaration
    }

    pub fn declaration_path(&self) -> &str {
        &self.declaration_path
    }

    pub fn descriptor(&self) -> Option<&SourceProvenance> {
        self.descriptor.as_ref()
    }

    pub fn table(&self) -> Option<&str> {
        self.table.as_deref()
    }

    pub fn file(&self) -> Option<&SourceProvenance> {
        self.file.as_ref()
    }

    pub fn logical_row(&self) -> Option<u64> {
        self.logical_row
    }

    pub fn header(&self) -> Option<&str> {
        self.header.as_deref()
    }

    /// One-based CSV column number, when the owned value came from a CSV cell.
    pub fn column(&self) -> Option<usize> {
        self.column
    }

    pub fn canonical_path(&self) -> &str {
        &self.canonical_path
    }
}

/// Deterministically ordered note and field authoring provenance sidecar.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct NoteAuthoringProvenance {
    notes: BTreeMap<StableId, NoteAuthoringLocation>,
    fields: BTreeMap<(StableId, StableId), NoteAuthoringLocation>,
}

impl NoteAuthoringProvenance {
    pub(crate) fn insert_field(
        &mut self,
        note_id: StableId,
        field_id: StableId,
        location: NoteAuthoringLocation,
    ) -> Option<NoteAuthoringLocation> {
        self.fields.insert((note_id, field_id), location)
    }

    pub fn note(&self, note_id: &StableId) -> Option<&NoteAuthoringLocation> {
        self.notes.get(note_id)
    }

    pub fn field(&self, note_id: &StableId, field_id: &StableId) -> Option<&NoteAuthoringLocation> {
        self.fields.get(&(note_id.clone(), field_id.clone()))
    }

    pub fn notes(&self) -> impl Iterator<Item = (&StableId, &NoteAuthoringLocation)> {
        self.notes.iter()
    }

    pub fn fields(&self) -> impl Iterator<Item = (&(StableId, StableId), &NoteAuthoringLocation)> {
        self.fields.iter()
    }
}

/// Deep source module for one Canonical Deck file and its loaded includes.
#[derive(Clone)]
pub struct CanonicalSourceDocument {
    provenance: SourceProvenance,
    deck: CanonicalDeck,
    resolved_deck: CanonicalDeck,
    includes: IncludeState,
    note_sources: Option<NoteSourceExpression>,
    authoring_provenance: NoteAuthoringProvenance,
    csv_sources: Vec<(CsvSourceRequestKind, CsvSourceFile)>,
    original_sources: BTreeMap<SourceProvenance, SourceFile>,
}

impl std::fmt::Debug for CanonicalSourceDocument {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("CanonicalSourceDocument")
            .field("provenance", &self.provenance)
            .field("deck", &self.deck)
            .field("note_sources", &self.note_sources)
            .finish_non_exhaustive()
    }
}

impl CanonicalSourceDocument {
    /// Parse source that contains no includes.
    pub fn parse(source: SourceFile) -> Result<Self, SourceDocumentError> {
        Self::parse_with_includes(source, |request| {
            Err(format!(
                "no include loader was provided for {:?}",
                request.target()
            ))
        })
    }

    /// Parse source with caller-owned include loading and provenance.
    ///
    /// The loader owns path authorization and bytes retrieval. This method owns
    /// strict duplicate/scalar/union/schema validation and never performs I/O.
    pub fn parse_with_includes(
        source: SourceFile,
        mut loader: impl FnMut(&IncludeRequest) -> Result<SourceFile, String>,
    ) -> Result<Self, SourceDocumentError> {
        Self::parse_with_loaders(source, &mut loader, &mut |request| {
            Err(format!(
                "no CSV source loader was provided for {:?}",
                request.target()
            ))
        })
    }

    /// Parse source with caller-owned include and CSV Authoring Source loading.
    ///
    /// Both loaders authorize and inject bytes. This formats crate never opens a
    /// path. The CSV loader receives explicit descriptor/table request kinds.
    pub fn parse_with_csv_sources(
        source: SourceFile,
        mut include_loader: impl FnMut(&IncludeRequest) -> Result<SourceFile, String>,
        mut csv_loader: impl FnMut(&CsvSourceRequest) -> Result<CsvSourceFile, String>,
    ) -> Result<Self, SourceDocumentError> {
        Self::parse_with_loaders(source, &mut include_loader, &mut csv_loader)
    }

    fn parse_with_loaders(
        source: SourceFile,
        include_loader: &mut impl FnMut(&IncludeRequest) -> Result<SourceFile, String>,
        csv_loader: &mut impl FnMut(&CsvSourceRequest) -> Result<CsvSourceFile, String>,
    ) -> Result<Self, SourceDocumentError> {
        let prepared = prepare_source(source, true, include_loader)?;
        let root_yaml = yaml_with_included_structures_for_validation(
            &prepared.yaml_without_directives,
            prepared.includes.note_types(),
            prepared.includes.media(),
        )?;
        let (root_yaml, note_sources) =
            strip_note_source_expression(&root_yaml, prepared.root.provenance())?;
        let mut deck = canonical_yaml::from_str(&root_yaml).map_err(|error| {
            SourceDocumentError::source(prepared.root.provenance(), error.to_string())
        })?;
        if let Some(media) = prepared.includes.media() {
            deck.media = media.clone();
        }
        for id in prepared.includes.media_asset_sources().keys() {
            if !deck.media.contains_key(id) {
                return Err(SourceDocumentError::at(
                    prepared.root.provenance(),
                    format!("media.{id}.source"),
                    "media source requires a complete path and sha256 declaration",
                ));
            }
        }
        let materialized_yaml = yaml_with_included_structures_for_validation(
            &prepared.materialized_yaml,
            prepared.includes.resolved_note_types(),
            prepared.includes.media(),
        )?;
        let (materialized_yaml, materialized_note_sources) =
            strip_note_source_expression(&materialized_yaml, prepared.root.provenance())?;
        if materialized_note_sources != note_sources {
            return Err(SourceDocumentError::at(
                prepared.root.provenance(),
                "notes",
                "scalar include materialization changed the notes source declarations",
            ));
        }
        let mut resolved_deck = canonical_yaml::from_str(&materialized_yaml).map_err(|error| {
            SourceDocumentError::source(prepared.root.provenance(), error.to_string())
        })?;
        if let Some(media) = prepared.includes.media() {
            resolved_deck.media = media.clone();
        }
        let (authoring_provenance, csv_sources) = materialize_note_sources(
            &mut resolved_deck,
            note_sources.as_ref(),
            prepared.root.provenance(),
            csv_loader,
        )?;
        canonical_yaml::to_string(&resolved_deck).map_err(|error| {
            SourceDocumentError::source(prepared.root.provenance(), error.to_string())
        })?;
        canonical_yaml::to_string(&deck).map_err(|error| {
            SourceDocumentError::source(prepared.root.provenance(), error.to_string())
        })?;
        let original_sources = prepared.original_sources()?;
        Ok(Self {
            provenance: prepared.root.provenance().clone(),
            deck,
            resolved_deck,
            includes: prepared.includes,
            note_sources,
            authoring_provenance,
            csv_sources,
            original_sources,
        })
    }

    /// Construct a source document from an already validated domain deck.
    pub fn from_deck(
        provenance: SourceProvenance,
        deck: CanonicalDeck,
    ) -> Result<Self, SourceDocumentError> {
        canonical_yaml::to_string(&deck)
            .map_err(|error| SourceDocumentError::source(&provenance, error.to_string()))?;
        let authoring_provenance = inline_provenance(&deck, &provenance, "notes");
        Ok(Self {
            provenance,
            resolved_deck: deck.clone(),
            deck,
            includes: IncludeState::default(),
            note_sources: None,
            authoring_provenance,
            csv_sources: Vec::new(),
            original_sources: BTreeMap::new(),
        })
    }

    pub fn provenance(&self) -> &SourceProvenance {
        &self.provenance
    }

    /// Source-preserved direct CSV note declaration, when `notes: !csv` is used.
    pub fn csv_note_source(&self) -> Option<&CsvNoteSourceDeclaration> {
        self.note_sources
            .as_ref()
            .and_then(NoteSourceExpression::direct_csv)
    }

    /// Per-note and per-field authoring ownership for the materialized deck.
    pub fn authoring_provenance(&self) -> &NoteAuthoringProvenance {
        &self.authoring_provenance
    }

    /// Every authoritative CSV descriptor and table loaded by this document.
    pub fn csv_sources(&self) -> &[(CsvSourceRequestKind, CsvSourceFile)] {
        &self.csv_sources
    }

    /// Every scalar or structural media source loaded by this document.
    pub fn included_sources(&self) -> Vec<IncludedSource> {
        self.includes.source_provenance()
    }

    /// Package-relative asset locations for source-backed media declarations.
    pub fn media_asset_sources(&self) -> &BTreeMap<StableId, String> {
        self.includes.media_asset_sources()
    }

    /// Read-only validated domain view. Mutation remains behind typed methods.
    pub fn deck(&self) -> &CanonicalDeck {
        &self.deck
    }

    /// Fully materialized read view with scalar include contents substituted.
    pub fn resolved_deck(&self) -> &CanonicalDeck {
        &self.resolved_deck
    }

    /// Compare-and-set one scalar field or metadata value.
    pub fn set_scalar(
        &mut self,
        target: CanonicalScalarTarget,
        expected: &str,
        replacement: &str,
    ) -> Result<EditLocation, SourceDocumentError> {
        let path = target.schema_path();
        let mut next = self.clone();
        if let Some(location) =
            next.includes
                .edit_scalar(&path, expected, replacement, &next.provenance)?
        {
            let value = scalar_mut(&mut next.resolved_deck, &target).ok_or_else(|| {
                SourceDocumentError::at(
                    &next.provenance,
                    &path,
                    "typed scalar target is not present in the resolved Canonical Deck",
                )
            })?;
            *value = replacement.to_owned();
            if path.starts_with("note_types.") && next.includes.note_types().is_some() {
                next.includes
                    .replace_resolved_note_types(next.resolved_deck.note_types.clone());
            }
            next.validate()?;
            *self = next;
            return Ok(location);
        }
        let value = scalar_mut(&mut next.deck, &target).ok_or_else(|| {
            SourceDocumentError::at(
                &next.provenance,
                &path,
                "typed scalar target is not present in this Canonical Deck",
            )
        })?;
        if value != expected {
            return Err(SourceDocumentError::at(
                &next.provenance,
                &path,
                format!("expected {expected:?}, found {value:?}"),
            ));
        }
        *value = replacement.to_owned();
        if path.starts_with("note_types.") && next.includes.note_types().is_some() {
            let resolved_value = scalar_mut(&mut next.resolved_deck, &target).ok_or_else(|| {
                SourceDocumentError::at(
                    &next.provenance,
                    &path,
                    "typed scalar target is not present in the resolved Canonical Deck",
                )
            })?;
            *resolved_value = replacement.to_owned();
            next.includes.replace_note_types(
                next.deck.note_types.clone(),
                next.resolved_deck.note_types.clone(),
            );
            let location = next
                .includes
                .note_types_source()
                .map(EditLocation::Included)
                .expect("note-types include source exists");
            next.validate()?;
            *self = next;
            return Ok(location);
        }
        next.validate()?;
        *self = next;
        Ok(EditLocation::Root)
    }

    /// Compare-and-set one declared media hash, whether inline or included.
    pub fn set_media_hash(
        &mut self,
        media_id: &StableId,
        expected_path: &str,
        sha256: &str,
    ) -> Result<EditLocation, SourceDocumentError> {
        let path = format!("media.{media_id}.sha256");
        let mut next = self.clone();
        if next.includes.media().is_some() {
            let (media, dirty) = next.includes.media_mut().expect("media include exists");
            let reference = media.get_mut(media_id).ok_or_else(|| {
                SourceDocumentError::at(
                    &next.provenance,
                    &path,
                    format!("media ID {media_id} is not declared in the included media map"),
                )
            })?;
            ensure_media_path(
                &next.provenance,
                &path,
                reference.path.as_str(),
                expected_path,
            )?;
            reference.sha256 = sha256.to_owned();
            *dirty = true;
            next.deck.media = media.clone();
            let location = next
                .includes
                .media_source()
                .map(EditLocation::Included)
                .expect("media source exists");
            next.validate()?;
            *self = next;
            return Ok(location);
        }
        let reference = next.deck.media.get_mut(media_id).ok_or_else(|| {
            SourceDocumentError::at(
                &next.provenance,
                &path,
                format!("media ID {media_id} is not declared"),
            )
        })?;
        ensure_media_path(
            &next.provenance,
            &path,
            reference.path.as_str(),
            expected_path,
        )?;
        reference.sha256 = sha256.to_owned();
        next.validate()?;
        *self = next;
        Ok(EditLocation::Root)
    }

    /// Convert strict whole-field image HTML using a caller-built path lookup.
    pub fn convert_strict_image_fields(
        &mut self,
        lookup: &BTreeMap<String, Option<StableId>>,
    ) -> Result<ImageConversionReport, SourceDocumentError> {
        let mut next = self.clone();
        let mut report = ImageConversionReport::default();
        for note in next.deck.notes.values_mut() {
            let field_ids = note.fields.keys().cloned().collect::<Vec<_>>();
            for field_id in field_ids {
                let Some(text) = note.fields[&field_id].as_scalar() else {
                    continue;
                };
                if let Some(images) = convert_text_to_images(text, lookup, &mut report) {
                    note.fields.insert(field_id, FieldValue::Images(images));
                }
            }
        }
        next.validate()?;
        *self = next;
        Ok(report)
    }

    /// Emit deterministic canonical root YAML and changed include outputs.
    pub fn emit(&self) -> Result<SourceDocumentEmission, SourceDocumentError> {
        self.validate()?;
        let canonical = canonical_yaml::to_string(&self.deck)
            .map_err(|error| SourceDocumentError::source(&self.provenance, error.to_string()))?;
        let canonical = if let Some(expression) = &self.note_sources {
            expression
                .restore(canonical, &self.deck)
                .map_err(|message| SourceDocumentError::at(&self.provenance, "notes", message))?
        } else {
            canonical
        };
        let canonical = self.includes.restore_directives(canonical)?;
        // The codec generated every schema value; this final strict pass protects
        // directive restoration from introducing a duplicate mapping key.
        crate::strict_yaml::reject_duplicate_keys(&canonical)
            .map_err(|error| SourceDocumentError::source(&self.provenance, error.to_string()))?;
        let root = SourceFile::new(self.provenance.clone(), canonical);
        Ok(SourceDocumentEmission::new(
            root,
            self.includes.changed_sources()?,
            self.original_sources.clone(),
        ))
    }

    fn validate(&self) -> Result<(), SourceDocumentError> {
        canonical_yaml::to_string(&self.deck)
            .and_then(|_| canonical_yaml::to_string(&self.resolved_deck))
            .map(|_| ())
            .map_err(|error| SourceDocumentError::source(&self.provenance, error.to_string()))
    }
}

fn materialize_note_sources(
    resolved_deck: &mut CanonicalDeck,
    expression: Option<&NoteSourceExpression>,
    root: &SourceProvenance,
    csv_loader: &mut impl FnMut(&CsvSourceRequest) -> Result<CsvSourceFile, String>,
) -> Result<
    (
        NoteAuthoringProvenance,
        Vec<(CsvSourceRequestKind, CsvSourceFile)>,
    ),
    SourceDocumentError,
> {
    let mut provenance = NoteAuthoringProvenance::default();
    let mut owners = BTreeMap::<StableId, String>::new();
    let mut declarations = Vec::new();
    let mut loaded_sources = Vec::new();

    match expression {
        None => {
            provenance = inline_provenance(resolved_deck, root, "notes");
            owners.extend(
                resolved_deck
                    .notes
                    .keys()
                    .cloned()
                    .map(|id| (id, "notes".to_owned())),
            );
        }
        Some(NoteSourceExpression::Csv(declaration)) => {
            resolved_deck.notes.clear();
            declarations.push(("notes".to_owned(), declaration));
        }
        Some(NoteSourceExpression::Sequence(sources)) => {
            for (index, source) in sources.iter().enumerate() {
                let declaration_path = format!("notes[{index}]");
                match source {
                    NoteSourceItem::Csv(declaration) => {
                        declarations.push((declaration_path, declaration));
                    }
                    NoteSourceItem::Inline { note_ids } => {
                        for note_id in note_ids {
                            let note = resolved_deck.notes.get(note_id).ok_or_else(|| {
                                SourceDocumentError::at(
                                    root,
                                    &declaration_path,
                                    format!("inline-owned note {note_id} did not materialize"),
                                )
                            })?;
                            insert_inline_provenance(
                                &mut provenance,
                                note,
                                root,
                                &declaration_path,
                            );
                            owners.insert(note_id.clone(), declaration_path.clone());
                        }
                    }
                }
            }
        }
    }

    let mut exclusions = Vec::<(StableId, String)>::new();
    for (declaration_path, declaration) in declarations {
        let descriptor_path = format!("{declaration_path}.descriptor");
        let descriptor_request = CsvSourceRequest::descriptor(
            root.clone(),
            descriptor_path.clone(),
            declaration.descriptor().to_owned(),
        );
        let descriptor_bytes = csv_loader(&descriptor_request).map_err(|message| {
            SourceDocumentError::at(
                root,
                &descriptor_path,
                format!(
                    "could not load CSV note descriptor {:?}: {message}",
                    declaration.descriptor()
                ),
            )
        })?;
        loaded_sources.push((CsvSourceRequestKind::Descriptor, descriptor_bytes.clone()));
        let descriptor_text = std::str::from_utf8(descriptor_bytes.bytes()).map_err(|error| {
            SourceDocumentError::at(
                descriptor_bytes.provenance(),
                &descriptor_path,
                format!("descriptor is not valid UTF-8: {error}"),
            )
        })?;
        let descriptor = CsvNoteSourceDescriptor::parse(SourceFile::new(
            descriptor_bytes.provenance().clone(),
            descriptor_text,
        ))
        .map_err(|error| SourceDocumentError::at(root, &declaration_path, error.to_string()))?;
        let descriptor_provenance = descriptor.provenance().clone();
        let materializer = CsvNoteSourceMaterializer::new(descriptor)
            .with_parameters(declaration.parameters())
            .map_err(|error| SourceDocumentError::at(root, &declaration_path, error.to_string()))?;
        let table_requests = materializer
            .table_paths()
            .map(|(alias, target)| {
                CsvSourceRequest::table(
                    descriptor_provenance.clone(),
                    format!("{declaration_path}.tables.{alias}"),
                    alias.to_owned(),
                    target.to_owned(),
                )
            })
            .collect::<Vec<_>>();
        let mut tables = BTreeMap::new();
        for request in table_requests {
            let alias = match request.kind() {
                crate::csv_note_source::CsvSourceRequestKind::Table { alias } => alias.clone(),
                crate::csv_note_source::CsvSourceRequestKind::Descriptor => unreachable!(),
            };
            let table = csv_loader(&request).map_err(|message| {
                SourceDocumentError::at(
                    root,
                    &declaration_path,
                    format!("could not load CSV table {:?}: {message}", request.target()),
                )
            })?;
            loaded_sources.push((request.kind().clone(), table.clone()));
            tables.insert(alias, table);
        }
        let mut materialized = materializer
            .materialize_with_provenance(&tables, &resolved_deck.note_types)
            .map_err(|error| SourceDocumentError::at(root, &declaration_path, error.to_string()))?;

        for note_id in declaration.excluded_note_ids() {
            if materialized.notes.remove(note_id).is_none() {
                return Err(SourceDocumentError::at(
                    root,
                    &declaration_path,
                    format!(
                        "unknown excluded note ID {note_id}; this CSV source does not materialize it"
                    ),
                ));
            }
            materialized.note_provenance.remove(note_id);
            materialized
                .field_provenance
                .retain(|(owned_note_id, _), _| owned_note_id != note_id);
            materialized
                .adapter_provenance
                .retain(|(owned_note_id, _), _| owned_note_id != note_id);
            exclusions.push((note_id.clone(), declaration_path.clone()));
        }

        for (note_id, note) in materialized.notes {
            if let Some(previous) = owners.get(&note_id) {
                return Err(SourceDocumentError::at(
                    root,
                    "notes",
                    format!(
                        "duplicate ownership of note ID {note_id} by {previous} and {declaration_path}; source order never overrides"
                    ),
                ));
            }
            let cell = &materialized.note_provenance[&note_id];
            provenance.notes.insert(
                note_id.clone(),
                csv_authoring_location(
                    root,
                    &declaration_path,
                    &descriptor_provenance,
                    cell,
                    format!("notes.{note_id}"),
                ),
            );
            for ((owned_note_id, field_id), cell) in &materialized.field_provenance {
                if owned_note_id == &note_id {
                    provenance.fields.insert(
                        (note_id.clone(), field_id.clone()),
                        csv_authoring_location(
                            root,
                            &declaration_path,
                            &descriptor_provenance,
                            cell,
                            format!("notes.{note_id}.fields.{field_id}"),
                        ),
                    );
                }
            }
            owners.insert(note_id.clone(), declaration_path.clone());
            resolved_deck.notes.insert(note_id, note);
        }
    }

    for (note_id, declaration_path) in exclusions {
        if !owners.contains_key(&note_id) {
            return Err(SourceDocumentError::at(
                root,
                declaration_path,
                format!(
                    "excluded note ID {note_id} is not owned by another source; ownership transfer is missing"
                ),
            ));
        }
    }
    Ok((provenance, loaded_sources))
}

fn inline_provenance(
    deck: &CanonicalDeck,
    root: &SourceProvenance,
    declaration_path: &str,
) -> NoteAuthoringProvenance {
    let mut provenance = NoteAuthoringProvenance::default();
    for note in deck.notes.values() {
        insert_inline_provenance(&mut provenance, note, root, declaration_path);
    }
    provenance
}

fn insert_inline_provenance(
    provenance: &mut NoteAuthoringProvenance,
    note: &brain_brew_core::Note,
    root: &SourceProvenance,
    declaration_path: &str,
) {
    provenance.notes.insert(
        note.id.clone(),
        inline_authoring_location(root, declaration_path, format!("notes.{}", note.id)),
    );
    for field_id in note.fields.keys() {
        provenance.fields.insert(
            (note.id.clone(), field_id.clone()),
            inline_authoring_location(
                root,
                declaration_path,
                format!("notes.{}.fields.{field_id}", note.id),
            ),
        );
    }
}

fn inline_authoring_location(
    root: &SourceProvenance,
    declaration_path: &str,
    canonical_path: String,
) -> NoteAuthoringLocation {
    NoteAuthoringLocation {
        source_kind: NoteAuthoringSourceKind::Inline,
        root_declaration: root.clone(),
        declaration_path: declaration_path.to_owned(),
        descriptor: None,
        table: None,
        file: None,
        logical_row: None,
        header: None,
        column: None,
        canonical_path,
    }
}

fn csv_authoring_location(
    root: &SourceProvenance,
    declaration_path: &str,
    descriptor: &SourceProvenance,
    cell: &CsvCellProvenance,
    canonical_path: String,
) -> NoteAuthoringLocation {
    NoteAuthoringLocation {
        source_kind: NoteAuthoringSourceKind::Csv,
        root_declaration: root.clone(),
        declaration_path: declaration_path.to_owned(),
        descriptor: Some(descriptor.clone()),
        table: Some(cell.table_alias.clone()),
        file: Some(cell.source.clone()),
        logical_row: cell.logical_row,
        header: Some(cell.header.clone()),
        column: Some(cell.column),
        canonical_path,
    }
}

fn strip_note_source_expression(
    yaml: &str,
    provenance: &SourceProvenance,
) -> Result<(String, Option<NoteSourceExpression>), SourceDocumentError> {
    let mut value = serde_yaml::from_str::<serde_yaml::Value>(yaml)
        .map_err(|error| SourceDocumentError::source(provenance, error.to_string()))?;
    let expression = NoteSourceExpression::take_from_root(&mut value, provenance)
        .map_err(|error| SourceDocumentError::at(provenance, "notes", error.to_string()))?;
    let Some(expression) = expression else {
        return Ok((yaml.to_owned(), None));
    };
    let yaml = serde_yaml::to_string(&value)
        .map_err(|error| SourceDocumentError::source(provenance, error.to_string()))?;
    Ok((yaml, Some(expression)))
}

fn yaml_with_included_structures_for_validation(
    yaml: &str,
    note_types: Option<&BTreeMap<StableId, brain_brew_core::NoteType>>,
    media: Option<&BTreeMap<StableId, brain_brew_core::MediaReference>>,
) -> Result<String, SourceDocumentError> {
    let mut yaml = yaml.to_owned();
    if let Some(note_types) = note_types
        && !note_types.is_empty()
    {
        let body = crate::note_type_map::to_string(note_types)
            .map_err(|error| {
                SourceDocumentError::source(
                    &SourceProvenance::new("canonical deck"),
                    format!("could not materialize included note types: {error}"),
                )
            })?
            .lines()
            .map(|line| format!("  {line}\n"))
            .collect::<String>();
        yaml = replace_structural_placeholder(&yaml, "note_types", &body)?;
    }
    if let Some(media) = media
        && !media.is_empty()
    {
        let body = crate::media_map::to_string(media)
            .lines()
            .map(|line| format!("  {line}\n"))
            .collect::<String>();
        yaml = replace_structural_placeholder(&yaml, "media", &body)?;
    }
    Ok(yaml)
}

fn replace_structural_placeholder(
    yaml: &str,
    key: &str,
    body: &str,
) -> Result<String, SourceDocumentError> {
    let invalid_placeholder = || {
        SourceDocumentError::source(
            &SourceProvenance::new("canonical deck"),
            format!("expected one empty {key} placeholder while loading structural include"),
        )
    };
    let start = crate::strict_yaml::top_level_mapping_key_offset(yaml, key)
        .ok_or_else(invalid_placeholder)?;
    let line_end = yaml[start..]
        .find('\n')
        .map_or(yaml.len(), |offset| start + offset + 1);
    let line = yaml[start..line_end]
        .strip_suffix('\n')
        .unwrap_or(&yaml[start..line_end]);
    let line = line.strip_suffix('\r').unwrap_or(line);
    if line != format!("{key}: {{}}") {
        return Err(invalid_placeholder());
    }

    let mut materialized = yaml.to_owned();
    materialized.replace_range(start..line_end, &format!("{key}:\n{body}"));
    Ok(materialized)
}

fn scalar_mut<'a>(
    deck: &'a mut CanonicalDeck,
    target: &CanonicalScalarTarget,
) -> Option<&'a mut String> {
    match target {
        CanonicalScalarTarget::DeckName => Some(&mut deck.name),
        CanonicalScalarTarget::DeckDescription => Some(&mut deck.description),
        CanonicalScalarTarget::DeckVariable(key) => deck.variables.get_mut(key),
        CanonicalScalarTarget::NoteTypeName { note_type_id } => {
            Some(&mut deck.note_types.get_mut(note_type_id)?.name)
        }
        CanonicalScalarTarget::NoteTypeVariable { note_type_id, key } => deck
            .note_types
            .get_mut(note_type_id)?
            .variables
            .get_mut(key),
        CanonicalScalarTarget::NoteTypeStyling { note_type_id } => {
            Some(&mut deck.note_types.get_mut(note_type_id)?.styling)
        }
        CanonicalScalarTarget::FieldName {
            note_type_id,
            field_id,
        } => deck
            .note_types
            .get_mut(note_type_id)?
            .fields
            .iter_mut()
            .find(|field| &field.id == field_id)
            .map(|field| &mut field.name),
        CanonicalScalarTarget::CardTemplateName {
            note_type_id,
            template_id,
        } => template_mut(deck, note_type_id, template_id).map(|template| &mut template.name),
        CanonicalScalarTarget::CardTemplateQuestion {
            note_type_id,
            template_id,
        } => template_mut(deck, note_type_id, template_id)
            .map(|template| &mut template.question_format),
        CanonicalScalarTarget::CardTemplateAnswer {
            note_type_id,
            template_id,
        } => template_mut(deck, note_type_id, template_id)
            .map(|template| &mut template.answer_format),
        CanonicalScalarTarget::CardTemplateVariable {
            note_type_id,
            template_id,
            key,
        } => template_mut(deck, note_type_id, template_id)?
            .variables
            .get_mut(key),
        CanonicalScalarTarget::NoteVariable { note_id, key } => {
            deck.notes.get_mut(note_id)?.variables.get_mut(key)
        }
        CanonicalScalarTarget::NoteField { note_id, field_id } => deck
            .notes
            .get_mut(note_id)?
            .fields
            .get_mut(field_id)?
            .as_scalar_mut(),
    }
}

fn template_mut<'a>(
    deck: &'a mut CanonicalDeck,
    note_type_id: &StableId,
    template_id: &StableId,
) -> Option<&'a mut brain_brew_core::CardTemplate> {
    deck.note_types
        .get_mut(note_type_id)?
        .card_templates
        .iter_mut()
        .find(|template| &template.id == template_id)
}

fn ensure_media_path(
    provenance: &SourceProvenance,
    schema_path: &str,
    actual: &str,
    expected: &str,
) -> Result<(), SourceDocumentError> {
    if actual == expected {
        Ok(())
    } else {
        Err(SourceDocumentError::at(
            provenance,
            schema_path,
            format!("expected media path {expected:?}, found {actual:?}"),
        ))
    }
}

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

    #[test]
    fn ordinary_yaml_is_not_reserialized_when_no_csv_declaration_exists() {
        let yaml = "deck: {id: deck.test, name: Test}\nnotes: {}\n";
        let (stripped, declaration) =
            strip_note_source_expression(yaml, &SourceProvenance::new("deck.yaml")).unwrap();

        assert!(declaration.is_none());
        assert_eq!(stripped, yaml);
    }
}