meta_language/
concept_records.rs1use std::collections::{BTreeMap, BTreeSet};
10use std::sync::OnceLock;
11
12use serde::Deserialize;
13
14const CONCEPT_RECORDS_LINKS: &str = include_str!("data/concept-records.lino");
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
18#[serde(rename_all = "lowercase")]
19pub enum ConceptRole {
20 Concept,
22 Operation,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
28pub struct SourceAlias {
29 pub source: String,
31 pub name: String,
33}
34
35#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
37pub struct ConceptDistinction {
38 pub id: String,
40 pub reason: String,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
46#[serde(rename_all = "camelCase", deny_unknown_fields)]
47pub struct ConceptRecord {
48 pub id: String,
50 pub phrase: String,
52 pub role: ConceptRole,
54 pub definition: String,
56 pub constraints: Vec<String>,
58 pub source_aliases: Vec<SourceAlias>,
60 pub former_names: Vec<String>,
62 #[serde(default)]
64 pub represents: Option<String>,
65 #[serde(default)]
67 pub distinct_from: Vec<ConceptDistinction>,
68}
69
70#[derive(Deserialize)]
71struct ConceptRecordFile {
72 concepts: Vec<ConceptRecord>,
73}
74
75#[must_use]
77pub fn concept_records() -> &'static [ConceptRecord] {
78 static RECORDS: OnceLock<Vec<ConceptRecord>> = OnceLock::new();
79 RECORDS.get_or_init(|| {
80 serde_json::from_value::<ConceptRecordFile>(
81 crate::structured_data_links::read_structured_data_lino(CONCEPT_RECORDS_LINKS)
82 .expect("the embedded concept links are valid"),
83 )
84 .expect("the embedded concept records are valid")
85 .concepts
86 })
87}
88
89#[must_use]
91pub fn concept_record(name: &str) -> Option<&'static ConceptRecord> {
92 static NAMES: OnceLock<BTreeMap<&'static str, &'static ConceptRecord>> = OnceLock::new();
93 NAMES
94 .get_or_init(|| {
95 let mut names = BTreeMap::new();
96 for record in concept_records() {
97 names.entry(record.id.as_str()).or_insert(record);
98 for former in &record.former_names {
99 names.entry(former.as_str()).or_insert(record);
100 }
101 }
102 names
103 })
104 .get(name)
105 .copied()
106}
107
108#[must_use]
110pub fn concept_records_for_source_name(source: &str, name: &str) -> Vec<&'static ConceptRecord> {
111 type SourceNameIndex =
112 BTreeMap<&'static str, BTreeMap<&'static str, Vec<&'static ConceptRecord>>>;
113 static SOURCES: OnceLock<SourceNameIndex> = OnceLock::new();
114 SOURCES
115 .get_or_init(|| {
116 let mut sources: SourceNameIndex = BTreeMap::new();
117 for record in concept_records() {
118 let mut seen = BTreeSet::new();
119 for alias in &record.source_aliases {
120 let key = (alias.source.as_str(), alias.name.as_str());
121 if seen.insert(key) {
122 sources
123 .entry(key.0)
124 .or_default()
125 .entry(key.1)
126 .or_default()
127 .push(record);
128 }
129 }
130 }
131 sources
132 })
133 .get(source)
134 .and_then(|names| names.get(name))
135 .cloned()
136 .unwrap_or_default()
137}