use serde::{Deserialize, Serialize};
pub use nibli_types::logic::{LogicalTerm, ProofRule, ProofStep, ProofTrace};
#[cfg(feature = "compute-client")]
pub mod compute_client;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LineResult {
pub line_number: u32,
pub text: String,
pub success: bool,
pub fact_id: Option<u64>,
pub error: Option<String>,
#[serde(default)]
pub notes: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct KbStatus {
pub asserted: u32,
pub errors: u32,
pub skipped: u32,
pub line_results: Vec<LineResult>,
}
pub fn proof_trace_to_json(trace: &ProofTrace) -> String {
serde_json::to_string(trace).unwrap_or_default()
}
pub fn proof_trace_from_json(s: &str) -> Option<ProofTrace> {
serde_json::from_str(s).ok()
}
#[cfg(test)]
mod tests {
use super::*;
fn one_step(rule: ProofRule) -> ProofTrace {
ProofTrace {
steps: vec![ProofStep {
rule,
holds: true,
children: vec![],
}],
root: 0,
naf_dependent: false,
cwa_false: false,
}
}
#[test]
fn proof_trace_json_roundtrip() {
let trace = one_step(ProofRule::Asserted {
fact: "gerku(adam)".to_string(),
});
let json = proof_trace_to_json(&trace);
let back = proof_trace_from_json(&json).unwrap();
assert_eq!(trace, back);
}
#[test]
fn wire_json_shape_is_byte_stable() {
let trace = one_step(ProofRule::Asserted {
fact: "gerku(adam)".to_string(),
});
let json = proof_trace_to_json(&trace);
assert!(json.contains(r#""type":"asserted""#), "json: {json}");
assert!(json.contains(r#""fact":"gerku(adam)""#), "json: {json}");
}
#[test]
fn predicate_check_serializes_named_fields() {
let trace = one_step(ProofRule::PredicateCheck {
method: "store".to_string(),
detail: "gerku(adam)".to_string(),
});
let json = proof_trace_to_json(&trace);
assert!(json.contains(r#""type":"predicate_check""#), "json: {json}");
assert!(json.contains(r#""method":"store""#), "json: {json}");
assert!(json.contains(r#""detail":"gerku(adam)""#), "json: {json}");
}
#[test]
fn exists_witness_term_encoding_is_pinned() {
let trace = one_step(ProofRule::ExistsWitness {
var: "x".to_string(),
term: LogicalTerm::Constant("adam".to_string()),
});
let json = proof_trace_to_json(&trace);
assert!(json.contains(r#""type":"exists_witness""#), "json: {json}");
assert!(json.contains(r#""var":"x""#), "json: {json}");
assert!(
json.contains(r#""term":{"constant":"adam"}"#),
"json: {json}"
);
}
}