lemma/serializers/
mod.rs

1mod json;
2mod msgpack;
3mod protobuf;
4
5pub use json::to_lemma_syntax as from_json;
6pub use msgpack::to_lemma_syntax as from_msgpack;
7pub use protobuf::to_lemma_syntax as from_protobuf;
8
9use crate::{FactValue, LemmaDoc, LemmaError, LemmaType, TypeAnnotation};
10use std::collections::HashMap;
11
12/// Find the type of a fact in a document
13pub(crate) fn find_fact_type(
14    name: &str,
15    doc: &LemmaDoc,
16    all_docs: &HashMap<String, LemmaDoc>,
17) -> Result<LemmaType, LemmaError> {
18    for fact in &doc.facts {
19        let fact_name = crate::analysis::fact_display_name(fact);
20        if fact_name == name {
21            return match &fact.value {
22                FactValue::Literal(lit) => Ok(lit.to_type()),
23                FactValue::TypeAnnotation(TypeAnnotation::LemmaType(t)) => Ok(t.clone()),
24                FactValue::DocumentReference(ref_doc) => {
25                    if let Some((_, field)) = name.split_once('.') {
26                        if let Some(referenced) = all_docs.get(ref_doc) {
27                            return find_fact_type(field, referenced, all_docs);
28                        }
29                    }
30                    Err(LemmaError::Engine(format!(
31                        "Cannot override document reference '{}'",
32                        name
33                    )))
34                }
35            };
36        }
37    }
38    Err(LemmaError::Engine(format!(
39        "Fact '{}' not found in document",
40        name
41    )))
42}