ie-schema 0.1.5

A flexible schema specification and parser for information extraction tasks.
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
use crate::ingest::{
    IngestClassification, IngestDType, IngestEntity, IngestEntityAcquired, IngestEntityList,
    IngestEntityProperty, IngestJsonNameKeyedStructure, IngestJsonStructure,
    IngestJsonStructureList, IngestNamedStructure, IngestRelation, IngestSchema,
    IngestStructureProperties, IngestStructureProperty, IngestValidator, IngestValidatorMode,
};
use crate::json_schema::JSONSchemaIngestSchema;
use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryFrom;
use std::fmt;

pub type Description = String;
pub type Regex = String;
pub type Threshold = f64;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Default)]
pub struct ExpandedName(String);

impl ExpandedName {
    pub fn new(s: String) -> Self {
        Self(s)
    }

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

    pub fn into_inner(self) -> String {
        self.0
    }
}

impl fmt::Display for ExpandedName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum DType {
    String,
    Int,
    Float,
    Bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ValidatorMode {
    Partial,
    Full,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Validator {
    pub pattern: Regex,
    pub mode: Option<ValidatorMode>,
    pub exclude: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Default)]
pub struct EntitySpec {
    // TODO I realize here that we should keep the original name, and our ExpandedName (really our slug)
    // could be named slug or something
    pub name: ExpandedName,
    pub dtype: Option<DType>,
    pub validator: Option<Validator>,
    pub threshold: Option<Threshold>,
    pub description: Option<Description>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StructureProperty {
    pub choices: Vec<EntitySpec>,
    pub description: Option<Description>,
    pub value: Option<String>,
    pub dtype: Option<DType>,
    pub validator: Option<Validator>,
    pub threshold: Option<Threshold>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct NamedStructure {
    pub name: ExpandedName,
    pub props: BTreeMap<ExpandedName, StructureProperty>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum JsonStructure {
    NamedStructure(NamedStructure),
    NameKeyedStructure {
        name: ExpandedName,
        props: BTreeMap<ExpandedName, StructureProperty>,
    },
    EntityList(Vec<EntitySpec>),
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Classification {
    pub task: EntitySpec,
    pub labels: Vec<EntitySpec>,
    pub threshold: Option<Threshold>,
    pub multi_label: bool,
    pub label_descriptions: BTreeMap<ExpandedName, EntitySpec>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum RelationAcquired {
    Empty,
    Entity {
        head: Box<EntitySpec>,
        tail: Box<EntitySpec>,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Relation {
    pub name: ExpandedName,
    pub description: Option<Description>,
    pub acquired: Option<RelationAcquired>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Default)]
pub struct NormalizedSchema {
    pub entities: Vec<EntitySpec>,
    pub json_structures: Vec<JsonStructure>,
    pub classifications: Vec<Classification>,
    pub relations: Vec<Relation>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Path(String);

impl Path {
    pub fn root() -> Self {
        Self("$".to_string())
    }

    pub fn field(&self, name: &str) -> Self {
        Self(format!("{}.{}", self.0, name))
    }

    pub fn index(&self, idx: usize) -> Self {
        Self(format!("{}[{idx}]", self.0))
    }

    pub fn key(&self, key: &str) -> Self {
        Self(format!("{}[{key:?}]", self.0))
    }

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

#[derive(Debug, thiserror::Error)]
pub enum SchemaNormalizeError {
    #[error("invalid name at {path}: {value:?}")]
    InvalidName { path: String, value: String },

    #[error("expected exactly one key at {path}, found {found}")]
    ExpectedSingleKey { path: String, found: usize },

    #[error("invalid colon-delimited entity at {path}: {raw:?}")]
    InvalidColonDelimitedEntity { path: String, raw: String },

    #[error("ambiguous colon-delimited entity at {path}: {raw:?}")]
    AmbiguousColonDelimitedEntity { path: String, raw: String },

    #[error(
        "conflicting threshold aliases at {path}: threshold={threshold:?}, cls_threshold={cls_threshold:?}"
    )]
    ConflictingThresholdAliases {
        path: String,
        threshold: Option<f64>,
        cls_threshold: Option<f64>,
    },

    #[error("duplicate normalized name at {path}: {name}")]
    DuplicateNormalizedName { path: String, name: String },

    #[error("invalid empty-acquired relation at {path}")]
    InvalidEmptyAcquiredRelation { path: String },

    #[error("invalid structure properties at {path}")]
    InvalidStructureProperties { path: String },

    #[error("invalid dtype at {path}: {value:?}")]
    InvalidDType { path: String, value: String },

    #[error("nested decode failed at {path}: {message}")]
    NestedDecode { path: String, message: String },
}

fn normalize_name(raw: &str, path: &Path) -> Result<ExpandedName, SchemaNormalizeError> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return Err(SchemaNormalizeError::InvalidName {
            path: path.as_str().to_string(),
            value: raw.to_string(),
        });
    }

    let mut out = String::new();
    let mut prev_us = false;

    for ch in trimmed.chars() {
        match ch {
            'a'..='z' | '0'..='9' => {
                out.push(ch);
                prev_us = false;
            }
            'A'..='Z' => {
                out.push(ch.to_ascii_lowercase());
                prev_us = false;
            }
            '_' | '-' | ' ' => {
                if !prev_us && !out.is_empty() {
                    out.push('_');
                    prev_us = true;
                }
            }
            _ => {
                return Err(SchemaNormalizeError::InvalidName {
                    path: path.as_str().to_string(),
                    value: raw.to_string(),
                });
            }
        }
    }

    while out.ends_with('_') {
        out.pop();
    }

    if out.is_empty() {
        return Err(SchemaNormalizeError::InvalidName {
            path: path.as_str().to_string(),
            value: raw.to_string(),
        });
    }

    Ok(ExpandedName::new(out))
}

fn dtype_1_to_2(v: IngestDType, path: &Path) -> Result<DType, SchemaNormalizeError> {
    match v {
        IngestDType::String(s) => {
            let Some(dt) = looks_like_dtype(&s) else {
                return Err(SchemaNormalizeError::InvalidDType {
                    path: path.as_str().to_string(),
                    value: s,
                });
            };
            Ok(dt)
        }
        IngestDType::Int(_) => Ok(DType::Int),
        IngestDType::Float(_) => Ok(DType::Float),
        IngestDType::Bool(_) => Ok(DType::Bool),
    }
}

fn validator_1_to_2(v: IngestValidator) -> Validator {
    match v {
        IngestValidator::Regex(pattern) => Validator {
            pattern,
            mode: None,
            exclude: false,
        },
        IngestValidator::Dict(d) => Validator {
            pattern: d.pattern,
            mode: d.mode.map(|m| match m {
                IngestValidatorMode::Partial => ValidatorMode::Partial,
                IngestValidatorMode::Full => ValidatorMode::Full,
            }),
            exclude: d.exclude.unwrap_or(false),
        },
    }
}

fn looks_like_dtype(s: &str) -> Option<DType> {
    let x = s.trim().to_ascii_lowercase();
    match x.as_str() {
        "str" | "string" => Some(DType::String),
        "int" | "integer" => Some(DType::Int),
        "float" => Some(DType::Float),
        "bool" | "boolean" => Some(DType::Bool),
        _ => None,
    }
}

type ParsedColonDelimitedEntity = (ExpandedName, Option<DType>, Option<f64>, Option<String>);

fn parse_colon_delimited_entity(
    raw: &str,
    path: &Path,
) -> Result<ParsedColonDelimitedEntity, SchemaNormalizeError> {
    let parts: Vec<&str> = raw.split("::").map(str::trim).collect();
    if parts.is_empty() {
        return Err(SchemaNormalizeError::InvalidColonDelimitedEntity {
            path: path.as_str().to_string(),
            raw: raw.to_string(),
        });
    }

    let name = normalize_name(parts[0], &path.field("name"))?;
    let mut dtype = None;
    let mut threshold = None;
    let mut description = None;

    for seg in parts.into_iter().skip(1) {
        if seg.is_empty() {
            continue;
        }

        if threshold.is_none()
            && let Ok(f) = seg.parse::<f64>()
        {
            threshold = Some(f);
            continue;
        }

        if dtype.is_none()
            && let Some(dt) = looks_like_dtype(seg)
        {
            dtype = Some(dt);
            continue;
        }

        if description.is_none() {
            description = Some(seg.to_string());
            continue;
        }

        return Err(SchemaNormalizeError::AmbiguousColonDelimitedEntity {
            path: path.as_str().to_string(),
            raw: raw.to_string(),
        });
    }

    Ok((name, dtype, threshold, description))
}

fn entity_property_to_spec(
    name_hint: Option<&str>,
    ep: IngestEntityProperty,
    path: &Path,
) -> Result<EntitySpec, SchemaNormalizeError> {
    match ep {
        IngestEntityProperty::Description(desc) => {
            let Some(name_hint) = name_hint else {
                return Err(SchemaNormalizeError::InvalidStructureProperties {
                    path: path.as_str().to_string(),
                });
            };

            Ok(EntitySpec {
                name: normalize_name(name_hint, &path.field("name"))?,
                dtype: None,
                validator: None,
                threshold: None,
                description: Some(desc),
            })
        }
        IngestEntityProperty::Dict(d) => {
            let raw_name = if d.name.trim().is_empty() {
                name_hint.unwrap_or_default()
            } else {
                d.name.as_str()
            };

            let dtype = match d.dtype {
                Some(dt) => Some(dtype_1_to_2(dt, &path.field("dtype"))?),
                None => None,
            };

            Ok(EntitySpec {
                name: normalize_name(raw_name, &path.field("name"))?,
                dtype,
                validator: d.validator.map(validator_1_to_2),
                threshold: d.threshold,
                description: d.description,
            })
        }
    }
}

fn single_entry_map<K, V>(
    map: BTreeMap<K, V>,
    path: &Path,
) -> Result<(K, V), SchemaNormalizeError> {
    if map.len() != 1 {
        return Err(SchemaNormalizeError::ExpectedSingleKey {
            path: path.as_str().to_string(),
            found: map.len(),
        });
    }
    Ok(map.into_iter().next().unwrap())
}

fn entity_1_to_spec(entity: IngestEntity, path: &Path) -> Result<EntitySpec, SchemaNormalizeError> {
    match entity {
        IngestEntity::Stringish(s) => {
            if s.contains("::") {
                let (name, dtype, threshold, description) = parse_colon_delimited_entity(&s, path)?;
                Ok(EntitySpec {
                    name,
                    dtype,
                    validator: None,
                    threshold,
                    description,
                })
            } else {
                Ok(EntitySpec {
                    name: normalize_name(&s, &path.field("name"))?,
                    dtype: None,
                    validator: None,
                    threshold: None,
                    description: None,
                })
            }
        }
        IngestEntity::SingleEntityDict(map) => {
            let (k, v) = single_entry_map(map, path)?;
            entity_property_to_spec(Some(&k), v, path)
        }
    }
}

fn entity_list_1_to_vec(
    list: IngestEntityList,
    path: &Path,
) -> Result<Vec<EntitySpec>, SchemaNormalizeError> {
    match list {
        IngestEntityList::List(items) => items
            .into_iter()
            .enumerate()
            .map(|(i, item)| entity_1_to_spec(item, &path.index(i)))
            .collect(),
        IngestEntityList::Dict(map) => map
            .into_iter()
            .map(|(k, v)| entity_property_to_spec(Some(&k), v, &path.key(&k)))
            .collect(),
    }
}

fn structure_property_1_to_2(
    v: IngestStructureProperty,
    path: &Path,
) -> Result<StructureProperty, SchemaNormalizeError> {
    let dtype = match v.dtype {
        Some(dt) => Some(dtype_1_to_2(dt, &path.field("dtype"))?),
        None => None,
    };

    Ok(StructureProperty {
        choices: match v.choices {
            Some(choices) => entity_list_1_to_vec(choices, &path.field("choices"))?,
            None => Vec::new(),
        },
        description: v.description,
        value: v.value,
        dtype,
        validator: v.validator.map(validator_1_to_2),
        threshold: v.threshold,
    })
}

fn ensure_unique_keys<T>(
    map: &BTreeMap<ExpandedName, T>,
    path: &Path,
) -> Result<(), SchemaNormalizeError> {
    let mut seen = BTreeSet::new();
    for k in map.keys() {
        if !seen.insert(k.clone()) {
            return Err(SchemaNormalizeError::DuplicateNormalizedName {
                path: path.as_str().to_string(),
                name: k.to_string(),
            });
        }
    }
    Ok(())
}

fn insert_unique<T>(
    map: &mut BTreeMap<ExpandedName, T>,
    key: ExpandedName,
    value: T,
    path: &Path,
) -> Result<(), SchemaNormalizeError> {
    if map.insert(key.clone(), value).is_some() {
        return Err(SchemaNormalizeError::DuplicateNormalizedName {
            path: path.as_str().to_string(),
            name: key.to_string(),
        });
    }
    Ok(())
}

fn structure_properties_1_to_map(
    v: IngestStructureProperties,
    path: &Path,
) -> Result<BTreeMap<ExpandedName, StructureProperty>, SchemaNormalizeError> {
    let out = match v {
        IngestStructureProperties::EntityDict(map) => {
            let mut out = BTreeMap::new();
            for (k, v) in map {
                let key = normalize_name(&k, &path.key(&k))?;
                let spec = entity_property_to_spec(Some(&k), v, &path.key(&k))?;
                insert_unique(
                    &mut out,
                    key,
                    StructureProperty {
                        choices: Vec::new(),
                        description: spec.description,
                        value: None,
                        dtype: spec.dtype,
                        validator: spec.validator,
                        threshold: spec.threshold,
                    },
                    path,
                )?;
            }
            out
        }
        IngestStructureProperties::EntityList(list) => {
            let specs = entity_list_1_to_vec(list, path)?;
            let mut out = BTreeMap::new();
            for spec in specs {
                insert_unique(
                    &mut out,
                    spec.name.clone(),
                    StructureProperty {
                        choices: Vec::new(),
                        description: spec.description,
                        value: None,
                        dtype: spec.dtype,
                        validator: spec.validator,
                        threshold: spec.threshold,
                    },
                    path,
                )?;
            }
            out
        }
        IngestStructureProperties::StructurePropertiesDict(map) => {
            let mut out = BTreeMap::new();
            for (k, v) in map {
                let key = normalize_name(&k, &path.key(&k))?;
                let value = structure_property_1_to_2(v, &path.key(&k))?;
                insert_unique(&mut out, key, value, path)?;
            }
            out
        }
    };

    ensure_unique_keys(&out, path)?;
    Ok(out)
}

fn try_parse_structure_properties_value(
    v: serde_json::Value,
    path: &Path,
) -> Result<BTreeMap<ExpandedName, StructureProperty>, SchemaNormalizeError> {
    let parsed: IngestStructureProperties =
        serde_json::from_value(v).map_err(|e| SchemaNormalizeError::NestedDecode {
            path: path.as_str().to_string(),
            message: e.to_string(),
        })?;
    structure_properties_1_to_map(parsed, path)
}

fn named_structure_1_to_2(
    ns: IngestNamedStructure,
    path: &Path,
) -> Result<NamedStructure, SchemaNormalizeError> {
    let name = normalize_name(&ns.name, &path.field("name"))?;
    let mut props = BTreeMap::new();

    for (k, v) in ns.props {
        let key = normalize_name(&k, &path.key(&k))?;
        let value = structure_property_1_to_2(v, &path.key(&k))?;
        insert_unique(&mut props, key, value, &path.field("props"))?;
    }

    ensure_unique_keys(&props, &path.field("props"))?;
    Ok(NamedStructure { name, props })
}

fn json_structure_1_to_2(
    v: IngestJsonStructure,
    path: &Path,
) -> Result<Vec<JsonStructure>, SchemaNormalizeError> {
    match v {
        IngestJsonStructure::NamedStructure(ns) => Ok(vec![JsonStructure::NamedStructure(
            named_structure_1_to_2(ns, path)?,
        )]),
        IngestJsonStructure::EntityList(list) => Ok(vec![JsonStructure::EntityList(
            entity_list_1_to_vec(list, path)?,
        )]),
        IngestJsonStructure::JsonNameKeyedStructure(IngestJsonNameKeyedStructure(map)) => {
            let mut out = Vec::new();
            for (name_raw, value) in map {
                let name = normalize_name(&name_raw, &path.key(&name_raw))?;
                let props = try_parse_structure_properties_value(value, &path.key(&name_raw))?;
                out.push(JsonStructure::NameKeyedStructure { name, props });
            }
            Ok(out)
        }
    }
}

fn classification_1_to_2(
    v: IngestClassification,
    path: &Path,
) -> Result<Classification, SchemaNormalizeError> {
    let threshold = match (v.threshold, v.cls_threshold) {
        (Some(a), Some(b)) if (a - b).abs() > f64::EPSILON => {
            return Err(SchemaNormalizeError::ConflictingThresholdAliases {
                path: path.as_str().to_string(),
                threshold: Some(a),
                cls_threshold: Some(b),
            });
        }
        (Some(a), _) => Some(a),
        (_, Some(b)) => Some(b),
        _ => None,
    };

    let task = entity_1_to_spec(v.task, &path.field("task"))?;
    let labels = entity_list_1_to_vec(v.labels, &path.field("labels"))?;

    let mut label_descriptions = BTreeMap::new();
    if let Some(map) = v.label_descriptions {
        for (k, ep) in map {
            let key = normalize_name(&k, &path.field("label_descriptions").key(&k))?;
            let spec =
                entity_property_to_spec(Some(&k), ep, &path.field("label_descriptions").key(&k))?;
            label_descriptions.insert(key, spec);
        }
    }

    Ok(Classification {
        task,
        labels,
        threshold,
        multi_label: v.multi_label.unwrap_or(false),
        label_descriptions,
    })
}

fn entity_acquired_to_2(
    v: IngestEntityAcquired,
    path: &Path,
) -> Result<RelationAcquired, SchemaNormalizeError> {
    match (v.head, v.tail) {
        (IngestEntity::Stringish(s1), IngestEntity::Stringish(s2))
            if s1.is_empty() && s2.is_empty() =>
        {
            Ok(RelationAcquired::Empty)
        }
        (h, t) => Ok(RelationAcquired::Entity {
            head: Box::new(entity_1_to_spec(h, &path.field("head"))?),
            tail: Box::new(entity_1_to_spec(t, &path.field("tail"))?),
        }),
    }
}

fn relation_1_to_2(v: IngestRelation, path: &Path) -> Result<Relation, SchemaNormalizeError> {
    match v {
        IngestRelation::Name(name) => Ok(Relation {
            name: normalize_name(&name, &path.field("name"))?,
            description: None,
            acquired: None,
        }),
        IngestRelation::NameDescription(map) => {
            let (k, desc) = single_entry_map(map, path)?;
            Ok(Relation {
                name: normalize_name(&k, &path.key(&k))?,
                description: Some(desc),
                acquired: None,
            })
        }
        IngestRelation::RelationEntityAcquired(map) => {
            let (k, acq) = single_entry_map(map, path)?;
            Ok(Relation {
                name: normalize_name(&k, &path.key(&k))?,
                description: None,
                acquired: Some(entity_acquired_to_2(acq, &path.key(&k))?),
            })
        }
    }
}

impl TryFrom<IngestSchema> for NormalizedSchema {
    type Error = SchemaNormalizeError;

    fn try_from(v: IngestSchema) -> Result<Self, Self::Error> {
        let root = Path::root();

        let entities = match v.entities {
            Some(x) => entity_list_1_to_vec(x, &root.field("entities"))?,
            None => Vec::new(),
        };

        let mut json_structures = Vec::new();
        match v.json_structures {
            None => {}
            Some(IngestJsonStructureList::Single(one)) => {
                json_structures.extend(json_structure_1_to_2(one, &root.field("json_structures"))?);
            }
            Some(IngestJsonStructureList::List(list)) => {
                for (i, item) in list.into_iter().enumerate() {
                    json_structures.extend(json_structure_1_to_2(
                        item,
                        &root.field("json_structures").index(i),
                    )?);
                }
            }
        }

        let classifications = match v.classifications {
            Some(list) => list
                .into_iter()
                .enumerate()
                .map(|(i, cls)| classification_1_to_2(cls, &root.field("classifications").index(i)))
                .collect::<Result<Vec<_>, _>>()?,
            None => Vec::new(),
        };

        let relations = match v.relations {
            Some(list) => list
                .into_iter()
                .enumerate()
                .map(|(i, rel)| relation_1_to_2(rel, &root.field("relations").index(i)))
                .collect::<Result<Vec<_>, _>>()?,
            None => Vec::new(),
        };

        Ok(Self {
            entities,
            json_structures,
            classifications,
            relations,
        })
    }
}

fn ingest_error_is_unknown_field(err: &serde_json::Error) -> bool {
    err.to_string().contains("unknown field")
}

/// Serde reports `unknown field `foo`, expected ...` for unknown keys on `deny_unknown_fields`.
fn unknown_field_name_in_ingest_error(err: &serde_json::Error) -> Option<String> {
    let s = err.to_string();
    let prefix = "unknown field `";
    let start = s.find(prefix)? + prefix.len();
    let rest = &s[start..];
    let end = rest.find('`')?;
    Some(rest[..end].to_string())
}

/// When strict IE ingest fails on an unknown field, still try JSON Schema if the field name
/// looks like JSON Schema vocabulary (e.g. `$id`, `type`, `properties`), not a stray IE key.
fn ingest_error_suggests_json_schema_root(err: &serde_json::Error) -> bool {
    let Some(name) = unknown_field_name_in_ingest_error(err) else {
        return false;
    };
    if name.starts_with('$') {
        return true;
    }
    matches!(
        name.as_str(),
        "type"
            | "properties"
            | "required"
            | "items"
            | "title"
            | "description"
            | "definitions"
            | "additionalProperties"
            | "patternProperties"
            | "allOf"
            | "anyOf"
            | "oneOf"
            | "not"
    )
}

impl NormalizedSchema {
    /// Parse IE ingest JSON or a root JSON Schema object from UTF-8 bytes.
    ///
    /// Tries strict IE [`IngestSchema`] first (unknown top-level keys are rejected), then
    /// JSON Schema via [`JSONSchemaIngestSchema`].
    pub fn from_json_bytes(bytes: &[u8]) -> Result<Self, SchemaLoadError> {
        match IngestSchema::from_json_slice(bytes) {
            Ok(phase1) => Ok(Self::try_from(phase1)?),
            Err(json_err) => {
                if ingest_error_is_unknown_field(&json_err)
                    && !ingest_error_suggests_json_schema_root(&json_err)
                {
                    return Err(SchemaLoadError::Json(json_err));
                }
                let schema = JSONSchemaIngestSchema::from_json_utf8(bytes)
                    .map_err(|_| SchemaLoadError::Json(json_err))?;
                let ingest = IngestSchema::try_from(schema).map_err(|e| {
                    SchemaLoadError::Normalize(SchemaNormalizeError::NestedDecode {
                        path: "$".to_string(),
                        message: e.to_string(),
                    })
                })?;
                Ok(Self::try_from(ingest)?)
            }
        }
    }

    pub fn from_json_str(s: &str) -> Result<Self, SchemaLoadError> {
        Self::from_json_bytes(s.as_bytes())
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SchemaLoadError {
    #[error("json parse error: {0}")]
    Json(#[from] serde_json::Error),

    #[error("schema normalization error: {0}")]
    Normalize(#[from] SchemaNormalizeError),
}

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

    #[test]
    fn normalized_parses_colon_delimited_entity() {
        let s = r#"{ "entities": ["Gene::str::0.95::gene symbol"] }"#;
        let schema = NormalizedSchema::from_json_str(s).unwrap();

        assert_eq!(schema.entities.len(), 1);
        let e = &schema.entities[0];
        assert_eq!(e.name.as_str(), "gene");
        assert_eq!(e.threshold, Some(0.95));
        assert_eq!(e.description.as_deref(), Some("gene symbol"));
        assert_eq!(e.dtype, Some(DType::String));
    }

    #[test]
    fn normalized_normalizes_hyphenated_and_camelish_names() {
        let s = r#"{ "entities": ["Gene-Name", "Other_Name"] }"#;
        let schema = NormalizedSchema::from_json_str(s).unwrap();

        assert_eq!(schema.entities[0].name.as_str(), "gene_name");
        assert_eq!(schema.entities[1].name.as_str(), "other_name");
    }

    #[test]
    fn normalized_rejects_conflicting_threshold_aliases() {
        let s = r#"
        {
            "classifications": [
                {
                    "task": "sentiment",
                    "labels": ["positive", "negative"],
                    "threshold": 0.4,
                    "cls_threshold": 0.7
                }
            ]
        }
        "#;

        let err = NormalizedSchema::from_json_str(s).unwrap_err();
        match err {
            SchemaLoadError::Normalize(SchemaNormalizeError::ConflictingThresholdAliases {
                ..
            }) => {}
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn normalized_parses_json_schema_root_string() {
        let raw = r#"
        {
            "$id": "BusinessRecord",
            "type": "object",
            "required": ["business_name"],
            "properties": {
                "business_name": { "type": "string" },
                "status": { "type": "string" }
            }
        }
        "#;

        let schema = NormalizedSchema::from_json_str(raw).unwrap();
        assert_eq!(schema.json_structures.len(), 1);
        let JsonStructure::NameKeyedStructure { name, props } = &schema.json_structures[0] else {
            panic!("expected name-keyed structure");
        };
        assert_eq!(name.as_str(), "businessrecord");
        assert!(props.contains_key(&ExpandedName::new("business_name".into())));
        assert!(props.contains_key(&ExpandedName::new("status".into())));
    }

    #[test]
    fn normalized_rejects_unknown_top_level_ie_key() {
        let s = r#"{ "entities": ["gene"], "extra_root": 1 }"#;
        let err = NormalizedSchema::from_json_str(s).expect_err("unknown IE key");
        match err {
            SchemaLoadError::Json(_) => {}
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn normalized_rejects_multi_key_single_entity_dict() {
        let s = r#"
        {
            "entities": [
                {
                    "gene": "desc",
                    "protein": "desc2"
                }
            ]
        }
        "#;

        let err = NormalizedSchema::from_json_str(s).unwrap_err();
        match err {
            SchemaLoadError::Normalize(SchemaNormalizeError::ExpectedSingleKey { .. }) => {}
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn normalized_named_structure_normalizes_property_keys() {
        let s = r#"
        {
            "json_structures": [
                {
                    "name": "Patient Record",
                    "Field-A": {
                        "description": "field a",
                        "dtype": "str"
                    },
                    "Field B": {
                        "choices": ["x", "y::str::0.5::label"]
                    }
                }
            ]
        }
        "#;

        let schema = NormalizedSchema::from_json_str(s).unwrap();
        let JsonStructure::NamedStructure(ns) = &schema.json_structures[0] else {
            panic!("expected named structure");
        };

        assert_eq!(ns.name.as_str(), "patient_record");
        assert!(ns.props.keys().any(|k| k.as_str() == "field_a"));
        assert!(ns.props.keys().any(|k| k.as_str() == "field_b"));
    }

    #[test]
    fn normalized_rejects_duplicate_normalized_structure_property_names() {
        let s = r#"
        {
            "json_structures": [
                {
                    "name": "Patient Record",
                    "Field-A": { "dtype": "str" },
                    "Field A": { "dtype": "str" }
                }
            ]
        }
        "#;

        let err = NormalizedSchema::from_json_str(s).unwrap_err();
        match err {
            SchemaLoadError::Normalize(SchemaNormalizeError::DuplicateNormalizedName {
                name,
                ..
            }) => assert_eq!(name, "field_a"),
            other => panic!("unexpected error: {other:?}"),
        }
    }
}