use rusqlite::Connection;
use crate::sheaf::{CohomologyConflictReport, Section};
pub fn store_section_merkle(
section: &Section,
conn: &Connection,
) -> Result<[u8; 32], String> {
let yaml = serde_yaml::to_string(section).map_err(|e| e.to_string())?;
let record = crate::layer0::mcard_record::MCardRecord::create(yaml.as_bytes(),
Some(crate::layer0::vocabulary::HASH_ALGORITHM_BLAKE3))?;
crate::layer2::record_store::insert_record(conn, &record).map_err(|error| error.to_string())?;
hex::decode(record.hash).map_err(|error| error.to_string())?.try_into()
.map_err(|_| "invalid BLAKE3 digest width".to_string())
}
pub fn load_section_merkle(
hash: &[u8; 32],
conn: &Connection,
) -> Result<Section, String> {
let hash_hex = hex::encode(hash);
let content = crate::layer2::record_store::read_text(conn, &hash_hex)
.map_err(|error| error.to_string())?.ok_or("section is missing")?;
serde_yaml::from_str(&content).map_err(|e| format!("Failed to deserialize section: {}", e))
}
pub fn store_conflict_report(
report: &CohomologyConflictReport,
conn: &Connection,
) -> Result<String, String> {
let yaml = report.to_yaml();
let record = crate::layer0::mcard_record::MCardRecord::create(yaml.as_bytes(), None)?;
crate::layer2::record_store::insert_record(conn, &record).map_err(|error| error.to_string())?;
Ok(record.hash)
}