use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
use super::store::Store;
use super::BqlError;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TableDiff {
pub table: String,
pub stored_rows: usize,
pub rebuilt_rows: usize,
pub missing: Vec<String>,
pub extra: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DriftReport {
pub stored_sha256: String,
pub rebuilt_sha256: String,
pub header: Vec<String>,
pub tables: Vec<TableDiff>,
}
impl DriftReport {
pub fn is_clean(&self) -> bool {
self.header.is_empty()
&& self
.tables
.iter()
.all(|t| t.missing.is_empty() && t.extra.is_empty())
}
pub fn render(&self) -> String {
let mut out = String::new();
for h in &self.header {
out.push_str(&format!("header {h}\n"));
}
for t in &self.tables {
out.push_str(&format!(
"{:<10} stored {:>5} rebuilt {:>5} missing {:>4} extra {:>4}\n",
t.table,
t.stored_rows,
t.rebuilt_rows,
t.missing.len(),
t.extra.len()
));
for row in &t.missing {
out.push_str(&format!(" - missing {row}\n"));
}
for row in &t.extra {
out.push_str(&format!(" + extra {row}\n"));
}
}
let verdict = if self.is_clean() { "agree" } else { "DRIFT" };
out.push_str(&format!(
"{verdict}: stored {} rebuilt {}\n",
&self.stored_sha256[..12.min(self.stored_sha256.len())],
&self.rebuilt_sha256[..12.min(self.rebuilt_sha256.len())]
));
out
}
}
fn rows<T: Serialize>(rows: &[T]) -> BTreeSet<String> {
rows.iter()
.map(|r| serde_json::to_string(r).expect("rows are plain data"))
.collect()
}
fn table<T: Serialize>(name: &str, stored: &[T], rebuilt: &[T]) -> TableDiff {
let (s, r) = (rows(stored), rows(rebuilt));
TableDiff {
table: name.to_string(),
stored_rows: stored.len(),
rebuilt_rows: rebuilt.len(),
missing: r.difference(&s).cloned().collect(),
extra: s.difference(&r).cloned().collect(),
}
}
pub fn diff_stores(stored: &Store, rebuilt: &Store) -> DriftReport {
let mut header = Vec::new();
let fields = [
("schema", &stored.schema, &rebuilt.schema),
("module", &stored.module, &rebuilt.module),
(
"source_sha256",
&stored.source_sha256,
&rebuilt.source_sha256,
),
("mir_sha256", &stored.mir_sha256, &rebuilt.mir_sha256),
];
for (name, s, r) in fields {
if s != r {
header.push(format!("{name}: stored {s} rebuilt {r}"));
}
}
DriftReport {
stored_sha256: stored.digest(),
rebuilt_sha256: rebuilt.digest(),
header,
tables: vec![
table("functions", &stored.functions, &rebuilt.functions),
table("effects", &stored.effects, &rebuilt.effects),
table("calls", &stored.calls, &rebuilt.calls),
table("items", &stored.items, &rebuilt.items),
table("refs", &stored.refs, &rebuilt.refs),
],
}
}
pub fn verify_against_source(
stored: &Store,
module: &str,
source: &str,
) -> Result<DriftReport, BqlError> {
let rebuilt = Store::load_source(module, source)?;
Ok(diff_stores(stored, &rebuilt))
}
fn shifted(pos: u32, edits: &[(u32, u32)], old: &str, new: &str) -> u32 {
let before = edits.iter().filter(|(s, _)| *s < pos).count() as i64;
let delta = new.len() as i64 - old.len() as i64;
(i64::from(pos) + before * delta) as u32
}
fn sub(name: &mut String, old: &str, new: &str) {
if name == old {
*name = new.to_string();
}
}
pub fn rename_rows(
store: &Store,
old: &str,
new: &str,
edits: &[(u32, u32)],
source_sha256: &str,
mir_sha256: &str,
) -> Store {
let mut out = store.clone();
out.source_sha256 = source_sha256.to_string();
out.mir_sha256 = mir_sha256.to_string();
for f in &mut out.functions {
sub(&mut f.name, old, new);
if let Some((s, e)) = f.name_span {
f.name_span = Some((shifted(s, edits, old, new), shifted(e, edits, old, new)));
}
}
for e in &mut out.effects {
sub(&mut e.function, old, new);
if let Some(via) = &mut e.via {
sub(via, old, new);
}
}
for c in &mut out.calls {
sub(&mut c.caller, old, new);
sub(&mut c.callee, old, new);
}
for i in &mut out.items {
sub(&mut i.name, old, new);
}
for r in &mut out.refs {
sub(&mut r.symbol, old, new);
r.span = (
shifted(r.span.0, edits, old, new),
shifted(r.span.1, edits, old, new),
);
}
out.functions.sort();
out.effects.sort();
out.calls.sort();
out.items.sort();
out.refs.sort();
out
}