use rusqlite::Connection;
use crate::sheaf::{section_to_merkle, CohomologyConflictReport, Section};
pub fn store_section_merkle(
section: &Section,
conn: &Connection,
) -> Result<[u8; 32], String> {
let node = section_to_merkle(section);
let yaml = serde_yaml::to_string(section).map_err(|e| e.to_string())?;
let hash_hex = hex::encode(node.hash);
let now = crate::fs_utils::compute_g_time();
conn.execute(
"INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params![hash_hex, yaml, now],
)
.map_err(|e| e.to_string())?;
Ok(node.hash)
}
pub fn load_section_merkle(
hash: &[u8; 32],
conn: &Connection,
) -> Result<Section, String> {
let hash_hex = hex::encode(hash);
let content: String = conn
.query_row(
"SELECT content FROM card WHERE hash = ?",
[&hash_hex],
|row| row.get(0),
)
.map_err(|e| format!("Failed to load section: {}", e))?;
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 hash = report.content_hash();
let now = crate::fs_utils::compute_g_time();
conn.execute(
"INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params![hash, yaml, now],
)
.map_err(|e| e.to_string())?;
Ok(hash)
}