openbim-dt 0.3.0

Lossless ISO 23387 data-template model and XML codec for Rust.
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
//! Lossless XML codec for the owned ISO 23387 types.
//!
//! Every owned type has `to_element` and `from_element`:
//!
//! * `to_element` builds a `dt:`-qualified element named after the type's
//!   global element (`dt:ObjectType`, `dt:Unit`, ...). It emits no `xmlns`
//!   declarations: the embedding document owns them. Use
//!   [`Document::standalone`](crate::Document::standalone) for a self-contained
//!   document. Children are written in the order the schema declares them
//!   (from the generated tables), so the output passes the order checks in
//!   [`Document::validate_schema`](crate::Document::validate_schema).
//! * `from_element` reads the element's *content* and ignores its local
//!   name, because standards such as ISO 7817-3 embed ISO 23387 content under
//!   their own unqualified element (`<ObjectType>` with `dt:`-qualified
//!   children). The element itself must be ISO 23387 qualified or
//!   unqualified; any other namespace is refused.
//!
//! # Contract
//!
//! `T::from_element(&value.to_element()) == Ok(value)` for every value. The
//! reverse is a semantic projection: comments, processing instructions and
//! child order within a repeating choice are not retained. Use the wire tree
//! ([`Element`]) when those must survive.
//!
//! Decoding fails closed with a structured [`CodecError`] rather than dropping
//! anything. In particular, a child the owned model does not represent,
//! including a foreign-namespace extension, is reported as
//! [`CodecErrorKind::UnknownChild`] instead of being skipped, and an
//! attribute it cannot hold as [`CodecErrorKind::UnknownAttribute`].
//!
//! Values that are well typed but violate a schema minimum (for example a
//! [`Subject`] with neither `HasPartRef` nor `IsSubtypeOfRef`) still encode;
//! run schema validation on the result to detect that.

use std::{error::Error, fmt, str::FromStr};

use crate::{
    schema::DEFINITIONS, AnyUri, Attribute, Base, Base64Binary, Concept, DataTemplate, DataType,
    DataTypeConstraint, DataTypeName, DataValue, DateTime, Decimal, Dimension, Element,
    GroupOfProperties, Guid, Language, MultiLanguageText, Node, NonNegativeInteger, ObjectType,
    Property, QuantityKind, Rational, Reference, ReferenceDocument, Scale, Subject, Unit,
    ValueError, ValueList, NAMESPACE,
};

const PREFIX: &str = "dt";

/// Why decoding an owned value from an element failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CodecErrorKind {
    /// A required attribute is absent.
    MissingAttribute,
    /// A required child element is absent.
    MissingChild,
    /// A child occurs more often than the owned model can hold.
    TooManyChildren,
    /// A child the owned model does not represent (including extensions).
    UnknownChild,
    /// An attribute the owned model does not represent, including a schema
    /// attribute in the wrong namespace (e.g. an unqualified `GUID`).
    UnknownAttribute,
    /// An attribute or text value is outside its lexical space.
    InvalidValue,
    /// Element content where the schema declares simple content, or the reverse.
    UnexpectedContent,
}

/// A structured decoding failure: what went wrong and where.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodecError {
    kind: CodecErrorKind,
    path: String,
    detail: String,
}

impl CodecError {
    fn new(kind: CodecErrorKind, path: impl Into<String>, detail: impl Into<String>) -> Self {
        Self {
            kind,
            path: path.into(),
            detail: detail.into(),
        }
    }

    /// Stable failure category.
    #[must_use]
    pub const fn kind(&self) -> CodecErrorKind {
        self.kind
    }

    /// Location relative to the decoded element: `Name[2]`, `@GUID`,
    /// `DataType/PossibleValues[1]/ValueList[1]`. Empty for the element itself.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Human-readable detail. Not stable; match on [`CodecError::kind`].
    #[must_use]
    pub fn detail(&self) -> &str {
        &self.detail
    }

    fn nested(mut self, parent: &str) -> Self {
        self.path = if self.path.is_empty() {
            parent.to_owned()
        } else {
            format!("{parent}/{}", self.path)
        };
        self
    }
}

impl fmt::Display for CodecError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "{:?} at `{}`: {}",
            self.kind, self.path, self.detail
        )
    }
}

impl Error for CodecError {}

// ---------------------------------------------------------------------------
// Schema-driven ordering
// ---------------------------------------------------------------------------

/// Declared child order for a type definition, from the generated tables.
fn declared_children(handle: &str) -> &'static [crate::schema::ChildRule] {
    DEFINITIONS
        .iter()
        .find(|definition| definition.handle == handle)
        .map_or(&[], |definition| definition.children)
}

/// Accumulates children by name, then emits them in schema order.
struct ChildWriter {
    handle: &'static str,
    children: Vec<(&'static str, Element)>,
}

impl ChildWriter {
    const fn new(handle: &'static str) -> Self {
        Self {
            handle,
            children: Vec::new(),
        }
    }

    fn push(&mut self, name: &'static str, element: Element) {
        self.children.push((name, element));
    }

    fn texts(&mut self, name: &'static str, values: &[MultiLanguageText]) {
        for value in values {
            self.push(name, multilingual(name, value));
        }
    }

    fn references(&mut self, name: &'static str, values: &[Reference]) {
        for value in values {
            self.push(name, reference(name, value));
        }
    }

    fn simple<'a>(&mut self, name: &'static str, values: impl IntoIterator<Item = &'a str>) {
        for value in values {
            self.push(name, dt_element(name).with_text(value));
        }
    }

    /// Emits children grouped in declared order, preserving insertion order
    /// within each name. Panics in debug builds if a pushed name is not
    /// declared for this handle: that would be a codec bug, and the drift
    /// tests exercise every name.
    fn finish(self, mut element: Element) -> Element {
        let order = declared_children(self.handle);
        debug_assert!(
            self.children
                .iter()
                .all(|(name, _)| order.iter().any(|rule| rule.name == *name)),
            "codec wrote a child not declared on {}",
            self.handle
        );
        let mut children = self.children;
        for rule in order {
            let (matching, rest): (Vec<_>, Vec<_>) = children
                .into_iter()
                .partition(|(name, _)| *name == rule.name);
            children = rest;
            for (_, child) in matching {
                element = element.with_child(child);
            }
        }
        element
    }
}

fn dt_element(local_name: &str) -> Element {
    Element::new(Some(NAMESPACE), Some(PREFIX), local_name)
}

fn dt_attribute(local_name: &str, value: &str) -> Attribute {
    Attribute::new(Some(PREFIX), local_name, Some(NAMESPACE), value)
}

fn plain_attribute(local_name: &str, value: &str) -> Attribute {
    Attribute::new(None, local_name, None, value)
}

fn multilingual(name: &str, value: &MultiLanguageText) -> Element {
    dt_element(name)
        .with_attribute(plain_attribute("language", value.language()))
        .with_text(value.text())
}

fn reference(name: &str, value: &Reference) -> Element {
    let mut element = dt_element(name);
    if let Some(uri) = value.uri() {
        element = element.with_attribute(dt_attribute("referenceURI", uri));
    }
    if let Some(guid) = value.guid() {
        element = element.with_attribute(dt_attribute("GUID", guid.as_str()));
    }
    element
}

// ---------------------------------------------------------------------------
// Encoding
// ---------------------------------------------------------------------------

fn concept_attributes(element: Element, concept: &Concept) -> Element {
    let mut element = element;
    if let Some(about) = concept.about_value() {
        element = element.with_attribute(dt_attribute("about", about.as_str()));
    }
    element
        .with_attribute(dt_attribute("GUID", concept.guid().as_str()))
        .with_attribute(plain_attribute(
            "dateOfCreation",
            concept.date_of_creation_value().as_str(),
        ))
}

fn write_concept(children: &mut ChildWriter, concept: &Concept) {
    children.texts("Name", concept.names());
    children.texts("Definition", concept.definitions());
    children.references("ReferenceDocumentRef", concept.reference_document_refs());
    children.texts("Description", concept.descriptions());
    children.texts("Example", concept.examples());
    children.references("SimilarToRef", concept.similar_to_refs());
    children.simple(
        "LanguageOfCreator",
        concept.languages_of_creator().iter().map(Language::as_str),
    );
    children.simple(
        "CountryOfOrigin",
        concept.countries_of_origin().iter().map(String::as_str),
    );
    children.simple(
        "VisualRepresentation",
        concept
            .visual_representations()
            .iter()
            .map(Base64Binary::as_str),
    );
    children.simple(
        "MajorVersion",
        concept
            .major_versions()
            .iter()
            .map(NonNegativeInteger::as_str),
    );
    children.simple(
        "MinorVersion",
        concept
            .minor_versions()
            .iter()
            .map(NonNegativeInteger::as_str),
    );
    children.simple("Status", concept.statuses().iter().map(String::as_str));
    children.references("ReplacedObjectsRef", concept.replaced_objects_refs());
    children.simple(
        "DeprecationExplanation",
        concept
            .deprecation_explanations()
            .iter()
            .map(String::as_str),
    );
    children.references("DictionaryRef", concept.dictionary_refs());
}

fn write_subject(children: &mut ChildWriter, subject: &Subject) {
    write_concept(children, subject.concept());
    children.references("HasPartRef", subject.has_part_refs());
    children.references("IsSubtypeOfRef", subject.is_subtype_of_refs());
}

fn encode(
    local_name: &'static str,
    handle: &'static str,
    concept: &Concept,
    fill: impl FnOnce(&mut ChildWriter),
) -> Element {
    let mut children = ChildWriter::new(handle);
    fill(&mut children);
    children.finish(concept_attributes(dt_element(local_name), concept))
}

fn data_type_element(value: &DataType) -> Element {
    let mut element = dt_element("DataType");
    if let Some(name) = value.name() {
        element = element.with_attribute(plain_attribute("name", name.as_str()));
    }
    let mut children = ChildWriter::new("DataTypeType");
    for constraint in value.constraints() {
        let (name, bound) = match constraint {
            DataTypeConstraint::MinInclusive(bound) => ("MinInclusive", bound),
            DataTypeConstraint::MinExclusive(bound) => ("MinExclusive", bound),
            DataTypeConstraint::MaxInclusive(bound) => ("MaxInclusive", bound),
            DataTypeConstraint::MaxExclusive(bound) => ("MaxExclusive", bound),
        };
        children.push(
            name,
            dt_element(name).with_attribute(plain_attribute("value", bound)),
        );
    }
    for format in value.data_formats() {
        children.push(
            "DataFormat",
            dt_element("DataFormat").with_attribute(plain_attribute("value", format)),
        );
    }
    for list in value.possible_values() {
        let mut values = dt_element("ValueList")
            .with_attribute(plain_attribute("language", list.language().as_str()));
        for item in list.values() {
            let mut entry = dt_element("Value");
            if let Some(order) = item.order() {
                entry = entry.with_attribute(plain_attribute("order", &order.to_string()));
            }
            values = values.with_child(entry.with_text(item.value()));
        }
        children.push(
            "PossibleValues",
            dt_element("PossibleValues").with_child(values),
        );
    }
    children.finish(element)
}

impl Concept {
    /// Encodes this concept as a `dt:Subject`-free `ConceptType` element named
    /// `local_name`. Concrete types use their own `to_element`.
    #[must_use]
    pub fn to_element_named(&self, local_name: &'static str) -> Element {
        encode(local_name, "ConceptType", self, |children| {
            write_concept(children, self);
        })
    }
}

impl Subject {
    /// Encodes as a global `dt:Subject` element.
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode("Subject", "SubjectType", self.concept(), |children| {
            write_subject(children, self);
        })
    }
}

impl ObjectType {
    /// Encodes as a global `dt:ObjectType` element.
    #[must_use]
    pub fn to_element(&self) -> Element {
        let subject = self.subject();
        encode(
            "ObjectType",
            "ObjectTypeType",
            subject.concept(),
            |children| {
                write_subject(children, subject);
            },
        )
    }
}

impl GroupOfProperties {
    /// Encodes as a global `dt:GroupOfProperties` element.
    #[must_use]
    pub fn to_element(&self) -> Element {
        let subject = self.subject();
        encode(
            "GroupOfProperties",
            "GroupOfPropertiesType",
            subject.concept(),
            |children| {
                write_subject(children, subject);
                children.references("HasPropertyRef", self.property_refs());
            },
        )
    }
}

impl DataTemplate {
    /// Encodes as a global `dt:DataTemplate` element.
    #[must_use]
    pub fn to_element(&self) -> Element {
        let subject = self.subject();
        encode(
            "DataTemplate",
            "DataTemplateType",
            subject.concept(),
            |children| {
                write_subject(children, subject);
                children.references("HasObjectTypeRef", self.object_type_refs());
                children.references("HasPropertyRef", self.property_refs());
                children.references("HasGroupOfPropertiesRef", self.group_of_properties_refs());
            },
        )
    }
}

impl Property {
    /// Encodes as a global `dt:Property` element.
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode("Property", "PropertyType", self.concept(), |children| {
            write_concept(children, self.concept());
            children.push("DataType", data_type_element(self.data_type()));
            children.simple("Symbol", self.symbols().iter().map(String::as_str));
            children.references("DimensionRef", self.dimension_refs());
            children.references("UnitRef", self.unit_refs());
            children.references("QuantityKindRef", self.quantity_kind_refs());
            children.references("IsDependentOnRef", self.dependency_refs());
            children.references(
                "IsSpecializationOfRef",
                self.specialization_ref().map_or(&[], std::slice::from_ref),
            );
        })
    }
}

impl QuantityKind {
    /// Encodes as a `dt:QuantityKind` element (library member, not a global root).
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode(
            "QuantityKind",
            "QuantityKindType",
            self.concept(),
            |children| {
                write_concept(children, self.concept());
                children.references("DimensionRef", std::slice::from_ref(self.dimension_ref()));
            },
        )
    }
}

/// Exponent element names in the order of [`Dimension::exponents`].
pub(crate) const DIMENSION_EXPONENTS: [&str; 7] = [
    "DimensionExponentForAmountOfSubstance",
    "DimensionExponentForElectricCurrent",
    "DimensionExponentForLength",
    "DimensionExponentForLuminousIntensity",
    "DimensionExponentForMass",
    "DimensionExponentForThermodynamicTemperature",
    "DimensionExponentForTime",
];

impl Dimension {
    /// Encodes as a `dt:Dimension` element (library member, not a global root).
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode("Dimension", "DimensionType", self.concept(), |children| {
            write_concept(children, self.concept());
            for (name, exponent) in DIMENSION_EXPONENTS.into_iter().zip(self.exponents()) {
                children.simple(name, [exponent.as_str()]);
            }
        })
    }
}

impl Unit {
    /// Encodes as a `dt:Unit` element (library member, not a global root).
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode("Unit", "UnitType", self.concept(), |children| {
            write_concept(children, self.concept());
            children.texts("Symbol", self.symbols());
            children.references("DimensionRef", std::slice::from_ref(self.dimension_ref()));
            children.simple("Scale", [self.scale().as_str()]);
            children.simple("Base", [self.base().as_str()]);
            children.simple("Coefficient", [self.coefficient().as_str()]);
            children.simple("Offset", [self.offset().as_str()]);
        })
    }
}

impl ReferenceDocument {
    /// Encodes as a `dt:ReferenceDocument` element (library member).
    #[must_use]
    pub fn to_element(&self) -> Element {
        encode(
            "ReferenceDocument",
            "ReferenceDocumentType",
            self.concept(),
            |children| {
                write_concept(children, self.concept());
                children.simple(
                    "DateOfPublication",
                    self.date_of_publication().map(DateTime::as_str),
                );
                children.simple("Author", self.author());
                children.simple("ISBN", self.isbn());
                children.simple("Language", self.languages().iter().map(Language::as_str));
                children.simple("Publisher", self.publisher());
                children.simple("URI", self.uri());
            },
        )
    }
}

// ---------------------------------------------------------------------------
// Decoding
// ---------------------------------------------------------------------------

/// Children of one element, indexed for positional error paths and checked
/// off as the decoder consumes them. Anything left over is an unknown child.
struct Reader<'a> {
    element: &'a Element,
    consumed: Vec<bool>,
}

impl<'a> Reader<'a> {
    fn new(element: &'a Element) -> Result<Self, CodecError> {
        for node in element.nodes() {
            if let Node::Text(text) | Node::CData(text) = node {
                if !text.chars().all(|c| matches!(c, ' ' | '\t' | '\r' | '\n')) {
                    return Err(CodecError::new(
                        CodecErrorKind::UnexpectedContent,
                        "",
                        "character data in element-only content",
                    ));
                }
            }
        }
        let count = element.children().count();
        Ok(Self {
            element,
            consumed: vec![false; count],
        })
    }

    /// Every dt child named `name`, with its 1-based path index.
    fn take(&mut self, name: &str) -> Vec<(String, &'a Element)> {
        let mut index = 0;
        let mut found = Vec::new();
        for (position, child) in self.element.children().enumerate() {
            if child.namespace_uri() == Some(NAMESPACE) && child.local_name() == name {
                index += 1;
                self.consumed[position] = true;
                found.push((format!("{name}[{index}]"), child));
            }
        }
        found
    }

    fn texts(&mut self, name: &str) -> Result<Vec<MultiLanguageText>, CodecError> {
        self.take(name)
            .into_iter()
            .map(|(path, child)| read_multilingual(child).map_err(|error| error.nested(&path)))
            .collect()
    }

    fn references(&mut self, name: &str) -> Result<Vec<Reference>, CodecError> {
        self.take(name)
            .into_iter()
            .map(|(path, child)| read_reference(child).map_err(|error| error.nested(&path)))
            .collect()
    }

    fn simple<T>(
        &mut self,
        name: &str,
        parse: impl Fn(&str) -> Result<T, ValueError>,
    ) -> Result<Vec<T>, CodecError> {
        self.take(name)
            .into_iter()
            .map(|(path, child)| {
                refuse_unrepresented_attributes(child, &[]).map_err(|error| error.nested(&path))?;
                let text = simple_text(child).map_err(|error| error.nested(&path))?;
                parse(&text).map_err(|error| invalid(&path, &error))
            })
            .collect()
    }

    fn strings(&mut self, name: &str) -> Result<Vec<String>, CodecError> {
        self.simple(name, |text| Ok(text.to_owned()))
    }

    fn at_most_one<T>(name: &str, mut values: Vec<T>) -> Result<Option<T>, CodecError> {
        if values.len() > 1 {
            return Err(CodecError::new(
                CodecErrorKind::TooManyChildren,
                format!("{name}[2]"),
                format!(
                    "`{name}` occurs {} times; at most one is declared",
                    values.len()
                ),
            ));
        }
        Ok(values.pop())
    }

    fn exactly_one<T>(name: &str, values: Vec<T>) -> Result<T, CodecError> {
        Self::at_most_one(name, values)?.ok_or_else(|| {
            CodecError::new(
                CodecErrorKind::MissingChild,
                "",
                format!("required child `{name}` is missing"),
            )
        })
    }

    /// Fails on the first child nothing consumed.
    fn finish(self) -> Result<(), CodecError> {
        let unknown = self
            .element
            .children()
            .zip(&self.consumed)
            .find(|(_, consumed)| !**consumed);
        match unknown {
            None => Ok(()),
            Some((child, _)) => Err(CodecError::new(
                CodecErrorKind::UnknownChild,
                child.qname(),
                format!(
                    "`{}` ({}) is not represented by the owned model",
                    child.local_name(),
                    child.namespace_uri().unwrap_or("no namespace")
                ),
            )),
        }
    }
}

/// An attribute identity: namespace and local name.
type AttributeName = (Option<&'static str>, &'static str);

/// `ConceptType` attributes. `about` and `GUID` are global attribute
/// declarations, hence namespace-qualified; `dateOfCreation` is local and
/// unqualified.
const CONCEPT_ATTRIBUTES: &[AttributeName] = &[
    (Some(NAMESPACE), "about"),
    (Some(NAMESPACE), "GUID"),
    (None, "dateOfCreation"),
];
const REFERENCE_ATTRIBUTES: &[AttributeName] =
    &[(Some(NAMESPACE), "GUID"), (Some(NAMESPACE), "referenceURI")];
const LANGUAGE_ATTRIBUTE: &[AttributeName] = &[(None, "language")];
const VALUE_ATTRIBUTE: &[AttributeName] = &[(None, "value")];

/// Refuses any attribute the owned model cannot hold, so decoding never
/// silently drops content. Namespace declarations are syntax, not content.
fn refuse_unrepresented_attributes(
    element: &Element,
    allowed: &[AttributeName],
) -> Result<(), CodecError> {
    for attribute in element.attributes() {
        if attribute.qname() == "xmlns" || attribute.prefix() == Some("xmlns") {
            continue;
        }
        let namespace = attribute.namespace_uri();
        let local_name = attribute.local_name();
        if !allowed
            .iter()
            .any(|(ns, name)| *ns == namespace && *name == local_name)
        {
            return Err(CodecError::new(
                CodecErrorKind::UnknownAttribute,
                format!("@{}", attribute.qname()),
                format!(
                    "attribute `{local_name}` ({}) is not represented by the owned model",
                    namespace.unwrap_or("no namespace")
                ),
            ));
        }
    }
    Ok(())
}

/// Refuses child elements and non-whitespace text in empty content.
fn empty_content(element: &Element) -> Result<(), CodecError> {
    let text = simple_text(element)?;
    if text.chars().all(|c| matches!(c, ' ' | '\t' | '\r' | '\n')) {
        Ok(())
    } else {
        Err(CodecError::new(
            CodecErrorKind::UnexpectedContent,
            "",
            "character data where empty content is declared",
        ))
    }
}

fn invalid(path: &str, error: &ValueError) -> CodecError {
    CodecError::new(CodecErrorKind::InvalidValue, path, error.to_string())
}

/// Text content of a simple-content element; child elements are refused.
fn simple_text(element: &Element) -> Result<String, CodecError> {
    if element.children().next().is_some() {
        return Err(CodecError::new(
            CodecErrorKind::UnexpectedContent,
            "",
            "element content where simple content is declared",
        ));
    }
    Ok(element.direct_text())
}

fn read_multilingual(element: &Element) -> Result<MultiLanguageText, CodecError> {
    refuse_unrepresented_attributes(element, LANGUAGE_ATTRIBUTE)?;
    let language = element.attribute_ns(None, "language").ok_or_else(|| {
        CodecError::new(
            CodecErrorKind::MissingAttribute,
            "@language",
            "required attribute `language` is missing",
        )
    })?;
    let text = simple_text(element)?;
    MultiLanguageText::new(language, text).map_err(|error| invalid("@language", &error))
}

fn read_reference(element: &Element) -> Result<Reference, CodecError> {
    refuse_unrepresented_attributes(element, REFERENCE_ATTRIBUTES)?;
    empty_content(element)?;
    let guid = element
        .attribute_ns(Some(NAMESPACE), "GUID")
        .map(Guid::from_str)
        .transpose()
        .map_err(|error| invalid("@GUID", &error))?;
    let uri = element
        .attribute_ns(Some(NAMESPACE), "referenceURI")
        .map(AnyUri::from_str)
        .transpose()
        .map_err(|error| invalid("@referenceURI", &error))?;
    Ok(Reference::new(guid, uri))
}

fn required_attribute<'a>(
    element: &'a Element,
    namespace: Option<&str>,
    name: &str,
) -> Result<&'a str, CodecError> {
    element.attribute_ns(namespace, name).ok_or_else(|| {
        CodecError::new(
            CodecErrorKind::MissingAttribute,
            format!("@{name}"),
            format!("required attribute `{name}` is missing"),
        )
    })
}

fn read_concept(element: &Element, children: &mut Reader<'_>) -> Result<Concept, CodecError> {
    refuse_unrepresented_attributes(element, CONCEPT_ATTRIBUTES)?;
    let guid = Guid::from_str(required_attribute(element, Some(NAMESPACE), "GUID")?)
        .map_err(|error| invalid("@GUID", &error))?;
    let date = DateTime::from_str(required_attribute(element, None, "dateOfCreation")?)
        .map_err(|error| invalid("@dateOfCreation", &error))?;
    let mut concept = Concept::from_identity(guid, date);
    if let Some(about) = element.attribute_ns(Some(NAMESPACE), "about") {
        concept.set_about(Some(
            AnyUri::from_str(about).map_err(|error| invalid("@about", &error))?,
        ));
    }
    for value in children.texts("Name")? {
        concept.add_name(value);
    }
    for value in children.texts("Definition")? {
        concept.add_definition(value);
    }
    for value in children.references("ReferenceDocumentRef")? {
        concept.add_reference_document_ref(value);
    }
    for value in children.texts("Description")? {
        concept.add_description(value);
    }
    for value in children.texts("Example")? {
        concept.add_example(value);
    }
    for value in children.references("SimilarToRef")? {
        concept.add_similar_to_ref(value);
    }
    for value in children.simple("LanguageOfCreator", Language::from_str)? {
        concept.add_language_of_creator(value);
    }
    for value in children.strings("CountryOfOrigin")? {
        concept.add_country_of_origin(value);
    }
    for value in children.simple("VisualRepresentation", Base64Binary::from_str)? {
        concept.add_visual_representation(value);
    }
    for value in children.simple("MajorVersion", NonNegativeInteger::from_str)? {
        concept.add_major_version(value);
    }
    for value in children.simple("MinorVersion", NonNegativeInteger::from_str)? {
        concept.add_minor_version(value);
    }
    for value in children.strings("Status")? {
        concept.add_status(value);
    }
    for value in children.references("ReplacedObjectsRef")? {
        concept.add_replaced_objects_ref(value);
    }
    for value in children.strings("DeprecationExplanation")? {
        concept.add_deprecation_explanation(value);
    }
    for value in children.references("DictionaryRef")? {
        concept.add_dictionary_ref(value);
    }
    Ok(concept)
}

fn read_subject(element: &Element, children: &mut Reader<'_>) -> Result<Subject, CodecError> {
    let mut subject = Subject::new(read_concept(element, children)?);
    for value in children.references("HasPartRef")? {
        subject.add_has_part_ref(value);
    }
    for value in children.references("IsSubtypeOfRef")? {
        subject.add_is_subtype_of_ref(value);
    }
    Ok(subject)
}

fn read_data_type(element: &Element) -> Result<DataType, CodecError> {
    refuse_unrepresented_attributes(element, &[(None, "name")])?;
    let mut children = Reader::new(element)?;
    let mut value = DataType::new(element.attribute_ns(None, "name").map(DataTypeName::from));
    // Constraints are one choice group; read them in document order so a
    // mixed Min/Max sequence round-trips in its original order.
    for (position, child) in element.children().enumerate() {
        if child.namespace_uri() != Some(NAMESPACE) {
            continue;
        }
        let make: fn(String) -> DataTypeConstraint = match child.local_name() {
            "MinInclusive" => DataTypeConstraint::MinInclusive,
            "MinExclusive" => DataTypeConstraint::MinExclusive,
            "MaxInclusive" => DataTypeConstraint::MaxInclusive,
            "MaxExclusive" => DataTypeConstraint::MaxExclusive,
            _ => continue,
        };
        children.consumed[position] = true;
        let path = format!("{}[{}]", child.local_name(), position + 1);
        refuse_unrepresented_attributes(child, VALUE_ATTRIBUTE)
            .map_err(|error| error.nested(&path))?;
        let bound = child.attribute_ns(None, "value").ok_or_else(|| {
            CodecError::new(
                CodecErrorKind::MissingAttribute,
                format!("{path}/@value"),
                "a bound without `value` cannot be represented",
            )
        })?;
        simple_text(child).map_err(|error| error.nested(&path))?;
        empty_content(child).map_err(|error| error.nested(&path))?;
        value.add_constraint(make(bound.to_owned()));
    }
    for (path, child) in children.take("DataFormat") {
        refuse_unrepresented_attributes(child, VALUE_ATTRIBUTE)
            .map_err(|error| error.nested(&path))?;
        empty_content(child).map_err(|error| error.nested(&path))?;
        let format = child.attribute_ns(None, "value").ok_or_else(|| {
            CodecError::new(
                CodecErrorKind::MissingAttribute,
                format!("{path}/@value"),
                "a data format without `value` cannot be represented",
            )
        })?;
        value.add_data_format(format);
    }
    for (path, possible) in children.take("PossibleValues") {
        refuse_unrepresented_attributes(possible, &[]).map_err(|error| error.nested(&path))?;
        let mut lists = Reader::new(possible).map_err(|error| error.nested(&path))?;
        for (list_path, list) in lists.take("ValueList") {
            let nested = format!("{path}/{list_path}");
            value
                .add_possible_values(read_value_list(list).map_err(|error| error.nested(&nested))?);
        }
        lists.finish().map_err(|error| error.nested(&path))?;
    }
    children.finish()?;
    Ok(value)
}

fn read_value_list(element: &Element) -> Result<ValueList, CodecError> {
    refuse_unrepresented_attributes(element, LANGUAGE_ATTRIBUTE)?;
    let language = Language::from_str(required_attribute(element, None, "language")?)
        .map_err(|error| invalid("@language", &error))?;
    let mut children = Reader::new(element)?;
    let mut values = Vec::new();
    for (path, child) in children.take("Value") {
        refuse_unrepresented_attributes(child, &[(None, "order")])
            .map_err(|error| error.nested(&path))?;
        let order = child
            .attribute_ns(None, "order")
            .map(|order| {
                order.trim().parse::<i32>().map_err(|_| {
                    CodecError::new(
                        CodecErrorKind::InvalidValue,
                        format!("{path}/@order"),
                        format!("`{order}` is not an xs:int"),
                    )
                })
            })
            .transpose()?;
        let text = simple_text(child).map_err(|error| error.nested(&path))?;
        values.push(DataValue::new(text, order));
    }
    children.finish()?;
    let mut values = values.into_iter();
    let first = values.next().ok_or_else(|| {
        CodecError::new(
            CodecErrorKind::MissingChild,
            "",
            "a value list requires at least one `Value`",
        )
    })?;
    let mut list = ValueList::new(language, first);
    for value in values {
        list.add_value(value);
    }
    Ok(list)
}

/// Decodes one owned value. The element's local name is deliberately not
/// checked: the same type appears under several names (a `Unit` is also a
/// `Library` child, LOIN embeds `ObjectType` content under its own element),
/// so the caller chooses. Its namespace must be ISO 23387, or it has none
/// when a host standard such as LOIN wraps DT content in a local element.
fn decode<T>(
    element: &Element,
    read: impl FnOnce(&mut Reader<'_>) -> Result<T, CodecError>,
) -> Result<T, CodecError> {
    if !matches!(element.namespace_uri(), None | Some(NAMESPACE)) {
        return Err(CodecError::new(
            CodecErrorKind::UnexpectedContent,
            "",
            format!(
                "`{}` is in namespace {}, not ISO 23387",
                element.local_name(),
                element.namespace_uri().unwrap_or_default()
            ),
        ));
    }
    let mut children = Reader::new(element)?;
    let value = read(&mut children)?;
    children.finish()?;
    Ok(value)
}

impl Concept {
    /// Decodes plain `ConceptType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| read_concept(element, children))
    }
}

impl Subject {
    /// Decodes `SubjectType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| read_subject(element, children))
    }
}

impl ObjectType {
    /// Decodes `ObjectTypeType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            read_subject(element, children).map(Self::new)
        })
    }
}

impl GroupOfProperties {
    /// Decodes `GroupOfPropertiesType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let subject = read_subject(element, children)?;
            let mut refs = children.references("HasPropertyRef")?.into_iter();
            // The owned type requires one property ref, matching the schema's
            // minimum of one for its choice group.
            let first = refs.next().ok_or_else(|| {
                CodecError::new(
                    CodecErrorKind::MissingChild,
                    "",
                    "required child `HasPropertyRef` is missing",
                )
            })?;
            let mut group = Self::new(subject, first);
            for value in refs {
                group.add_property_ref(value);
            }
            Ok(group)
        })
    }
}

impl DataTemplate {
    /// Decodes `DataTemplateType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let mut template = Self::new(read_subject(element, children)?);
            for value in children.references("HasObjectTypeRef")? {
                template.add_object_type_ref(value);
            }
            for value in children.references("HasPropertyRef")? {
                template.add_property_ref(value);
            }
            for value in children.references("HasGroupOfPropertiesRef")? {
                template.add_group_of_properties_ref(value);
            }
            Ok(template)
        })
    }
}

impl Property {
    /// Decodes `PropertyType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let concept = read_concept(element, children)?;
            let data_type = Reader::exactly_one("DataType", children.take("DataType"))?;
            let data_type =
                read_data_type(data_type.1).map_err(|error| error.nested(&data_type.0))?;
            let mut property = Self::new(concept, data_type);
            for value in children.strings("Symbol")? {
                property.add_symbol(value);
            }
            for value in children.references("DimensionRef")? {
                property.add_dimension_ref(value);
            }
            for value in children.references("UnitRef")? {
                property.add_unit_ref(value);
            }
            for value in children.references("QuantityKindRef")? {
                property.add_quantity_kind_ref(value);
            }
            for value in children.references("IsDependentOnRef")? {
                property.add_dependency_ref(value);
            }
            property.set_specialization_ref(Reader::at_most_one(
                "IsSpecializationOfRef",
                children.references("IsSpecializationOfRef")?,
            )?);
            Ok(property)
        })
    }
}

impl QuantityKind {
    /// Decodes `QuantityKindType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let concept = read_concept(element, children)?;
            let dimension =
                Reader::exactly_one("DimensionRef", children.references("DimensionRef")?)?;
            Ok(Self::new(concept, dimension))
        })
    }
}

impl Dimension {
    /// Decodes `DimensionType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let concept = read_concept(element, children)?;
            let mut exponents = Vec::with_capacity(DIMENSION_EXPONENTS.len());
            for name in DIMENSION_EXPONENTS {
                exponents.push(Reader::exactly_one(
                    name,
                    children.simple(name, Decimal::from_str)?,
                )?);
            }
            let exponents: [Decimal; 7] = exponents
                .try_into()
                .unwrap_or_else(|_| unreachable!("seven names yield seven exponents"));
            Ok(Self::new(concept, exponents))
        })
    }
}

impl Unit {
    /// Decodes `UnitType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let concept = read_concept(element, children)?;
            let symbols = children.texts("Symbol")?;
            let dimension =
                Reader::exactly_one("DimensionRef", children.references("DimensionRef")?)?;
            let scale = Reader::exactly_one("Scale", children.strings("Scale")?)?;
            let base = Reader::exactly_one("Base", children.strings("Base")?)?;
            let coefficient = Reader::exactly_one(
                "Coefficient",
                children.simple("Coefficient", Rational::from_str)?,
            )?;
            let offset =
                Reader::exactly_one("Offset", children.simple("Offset", Rational::from_str)?)?;
            let mut unit = Self::new(
                concept,
                dimension,
                Scale::from(scale.as_str()),
                Base::from(base.as_str()),
                coefficient,
                offset,
            );
            for symbol in symbols {
                unit.add_symbol(symbol);
            }
            Ok(unit)
        })
    }
}

impl ReferenceDocument {
    /// Decodes `ReferenceDocumentType` content (any element name).
    pub fn from_element(element: &Element) -> Result<Self, CodecError> {
        decode(element, |children| {
            let concept = read_concept(element, children)?;
            let mut languages = children.simple("Language", Language::from_str)?.into_iter();
            let first = languages.next().ok_or_else(|| {
                CodecError::new(
                    CodecErrorKind::MissingChild,
                    "",
                    "required child `Language` is missing",
                )
            })?;
            let mut document = Self::new(concept, first);
            for language in languages {
                document.add_language(language);
            }
            document.set_date_of_publication(Reader::at_most_one(
                "DateOfPublication",
                children.simple("DateOfPublication", DateTime::from_str)?,
            )?);
            document.set_author(Reader::at_most_one("Author", children.strings("Author")?)?);
            document.set_isbn(Reader::at_most_one("ISBN", children.strings("ISBN")?)?);
            document.set_publisher(Reader::at_most_one(
                "Publisher",
                children.strings("Publisher")?,
            )?);
            document.set_uri(Reader::at_most_one(
                "URI",
                children.simple("URI", AnyUri::from_str)?,
            )?);
            Ok(document)
        })
    }
}

#[cfg(test)]
mod drift {
    //! Drift guard: the codec must write every child element the generated
    //! schema tables declare for each owned type, so a schema regeneration
    //! that adds a child fails here instead of being silently dropped.

    use super::*;
    use crate::schema::DEFINITIONS;

    fn declared(handle: &str) -> Vec<&'static str> {
        let definition = DEFINITIONS
            .iter()
            .find(|definition| definition.handle == handle)
            .unwrap_or_else(|| panic!("schema tables no longer define {handle}"));
        definition.children.iter().map(|rule| rule.name).collect()
    }

    fn written(element: &Element) -> Vec<String> {
        let mut names: Vec<String> = element
            .children()
            .map(|child| child.local_name().to_owned())
            .collect();
        names.dedup();
        names
    }

    fn assert_writes_every_declared_child(handle: &str, element: &Element) {
        let written = written(element);
        let missing: Vec<_> = declared(handle)
            .into_iter()
            .filter(|name| !written.iter().any(|w| w == name))
            .collect();
        assert!(
            missing.is_empty(),
            "{handle}: codec never writes {missing:?}"
        );
    }

    // The same maximal fixtures the integration tests round-trip.
    use crate::{
        Base, Base64Binary, DataTypeConstraint, DataTypeName, DataValue, Decimal, Language,
        NonNegativeInteger, Rational, Scale, ValueList,
    };
    include!("../tests/support/maximal.rs");

    #[test]
    fn codec_writes_every_child_the_schema_declares() {
        let cases = [
            (
                "ObjectTypeType",
                ObjectType::new(maximal_subject(1)).to_element(),
            ),
            ("PropertyType", maximal_property().to_element()),
            ("GroupOfPropertiesType", maximal_group().to_element()),
            ("DataTemplateType", maximal_template().to_element()),
            ("QuantityKindType", maximal_quantity_kind().to_element()),
            ("DimensionType", maximal_dimension().to_element()),
            ("UnitType", maximal_unit().to_element()),
            (
                "ReferenceDocumentType",
                maximal_reference_document().to_element(),
            ),
        ];
        for (handle, element) in &cases {
            assert_writes_every_declared_child(handle, element);
        }
    }
}