use std::collections::{BTreeMap, BTreeSet};
use calcit_core::snapshot::{self, Snapshot};
use md5::{Digest, Md5};
use serde::Serialize;
const SCOPE_PREFIX: &str = "scope:";
const ABSENT: &str = "absent";
pub(crate) fn snapshot_units(snapshot: &Snapshot) -> Result<BTreeMap<String, String>, String> {
let mut units = BTreeMap::new();
units.insert("project".to_owned(), project_fingerprint(snapshot)?);
for (name, entry) in &snapshot.entries {
units.insert(format!("entry:{name}"), json_fingerprint(entry)?);
}
for (namespace, file) in &snapshot.files {
if namespace.ends_with(".$meta") {
continue;
}
units.insert(format!("ns:{namespace}"), json_fingerprint(&file.ns)?);
for (definition, entry) in &file.defs {
units.insert(format!("def:{namespace}/{definition}"), snapshot::definition_revision(entry)?);
}
}
Ok(units)
}
fn json_fingerprint<T: Serialize>(value: &T) -> Result<String, String> {
let mut canonical = serde_json::to_value(value).map_err(|error| format!("Failed to fingerprint Snapshot unit: {error}"))?;
canonical.sort_all_objects();
let encoded = serde_json::to_string(&canonical).map_err(|error| format!("Failed to fingerprint Snapshot unit: {error}"))?;
let mut hasher = Md5::new();
hasher.update(encoded.as_bytes());
Ok(format!("md5:{}", hex::encode(hasher.finalize())))
}
fn project_fingerprint(snapshot: &Snapshot) -> Result<String, String> {
json_fingerprint(&(&snapshot.package, &snapshot.about, &snapshot.version, &snapshot.verification))
}
pub(crate) fn changed_units(before: &BTreeMap<String, String>, after: &BTreeMap<String, String>) -> Vec<String> {
before
.keys()
.chain(after.keys())
.collect::<BTreeSet<_>>()
.into_iter()
.filter(|unit| before.get(*unit) != after.get(*unit))
.cloned()
.collect()
}
pub(crate) fn scoped_revision(original: &BTreeMap<String, String>, touched: &[String]) -> String {
let parts = touched
.iter()
.map(|unit| format!("{unit}@{}", original.get(unit).map(String::as_str).unwrap_or(ABSENT)))
.collect::<Vec<_>>();
format!("{SCOPE_PREFIX}{}", parts.join(","))
}
pub(crate) fn is_scoped_revision(revision: &str) -> bool {
revision.starts_with(SCOPE_PREFIX)
}
fn parse_scoped_revision(revision: &str) -> Result<BTreeMap<String, Option<String>>, String> {
let body = revision
.strip_prefix(SCOPE_PREFIX)
.ok_or_else(|| format!("Scoped revision must start with `{SCOPE_PREFIX}`"))?;
let mut units = BTreeMap::new();
for part in body.split(',').filter(|part| !part.is_empty()) {
let (unit, fingerprint) = part
.rsplit_once('@')
.ok_or_else(|| format!("Scoped revision part `{part}` must be `<unit>@<fingerprint>`"))?;
units.insert(unit.to_owned(), (fingerprint != ABSENT).then(|| fingerprint.to_owned()));
}
Ok(units)
}
pub(crate) fn check_scoped_revision(revision: &str, current: &BTreeMap<String, String>) -> Result<BTreeSet<String>, String> {
let expected = parse_scoped_revision(revision)?;
let conflicts = expected
.iter()
.filter(|(unit, fingerprint)| current.get(*unit) != fingerprint.as_ref())
.map(|(unit, _)| unit.as_str())
.collect::<Vec<_>>();
if !conflicts.is_empty() {
return Err(format!(
"Definition conflict: {} changed since the transaction was previewed. Re-run the dry-run and review the new plan.",
conflicts.join(", ")
));
}
Ok(expected.into_keys().collect())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Pick {
Ours,
Theirs,
Conflict,
}
fn pick<T: PartialEq>(base: Option<&T>, ours: Option<&T>, theirs: Option<&T>) -> Pick {
if ours == theirs || base == theirs {
Pick::Ours
} else if base == ours {
Pick::Theirs
} else {
Pick::Conflict
}
}
pub(crate) struct SnapshotMerge {
pub merged: Snapshot,
pub conflicts: Vec<String>,
}
pub(crate) fn merge_snapshots(base: &Snapshot, ours: &Snapshot, theirs: &Snapshot) -> Result<SnapshotMerge, String> {
let base_units = snapshot_units(base)?;
let our_units = snapshot_units(ours)?;
let their_units = snapshot_units(theirs)?;
let mut merged = ours.clone();
let mut conflicts = Vec::new();
let all_units = base_units
.keys()
.chain(our_units.keys())
.chain(their_units.keys())
.cloned()
.collect::<BTreeSet<_>>();
for unit in all_units {
match pick(base_units.get(&unit), our_units.get(&unit), their_units.get(&unit)) {
Pick::Ours => {}
Pick::Conflict => conflicts.push(unit),
Pick::Theirs => {
if !take_theirs(&mut merged, theirs, &unit)? {
conflicts.push(unit);
}
}
}
}
Ok(SnapshotMerge { merged, conflicts })
}
fn take_theirs(merged: &mut Snapshot, theirs: &Snapshot, unit: &str) -> Result<bool, String> {
if unit == "project" {
merged.package.clone_from(&theirs.package);
merged.about.clone_from(&theirs.about);
merged.version.clone_from(&theirs.version);
merged.verification = theirs.verification.clone();
} else if let Some(name) = unit.strip_prefix("entry:") {
match theirs.entries.get(name) {
Some(entry) => {
merged.entries.insert(name.to_owned(), entry.clone());
}
None => {
merged.entries.remove(name);
}
}
} else if let Some(namespace) = unit.strip_prefix("ns:") {
match theirs.files.get(namespace) {
Some(file) => {
merged
.files
.entry(namespace.to_owned())
.or_insert_with(|| snapshot::FileInSnapShot {
ns: file.ns.clone(),
defs: Default::default(),
})
.ns = file.ns.clone();
}
None => {
if merged.files.get(namespace).is_some_and(|file| !file.defs.is_empty()) {
return Ok(false);
}
merged.files.remove(namespace);
}
}
} else if let Some(path) = unit.strip_prefix("def:") {
let (namespace, definition) = path.split_once('/').ok_or_else(|| format!("Malformed definition unit `{unit}`"))?;
match theirs.files.get(namespace).and_then(|file| file.defs.get(definition)) {
Some(entry) => {
let ns_entry = theirs.files[namespace].ns.clone();
merged
.files
.entry(namespace.to_owned())
.or_insert_with(|| snapshot::FileInSnapShot {
ns: ns_entry,
defs: Default::default(),
})
.defs
.insert(definition.to_owned(), entry.clone());
}
None => {
if let Some(file) = merged.files.get_mut(namespace) {
file.defs.remove(definition);
}
}
}
} else {
return Err(format!("Unknown Snapshot unit `{unit}`"));
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::json_fingerprint;
use serde::{Serialize, Serializer, ser::SerializeMap};
struct OrderedMap<'a>(&'a [(&'a str, &'a str)]);
impl Serialize for OrderedMap<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(self.0.len()))?;
for (key, value) in self.0 {
map.serialize_entry(key, value)?;
}
map.end()
}
}
#[test]
fn fingerprints_ignore_object_order_but_preserve_values_and_array_order() {
let forward = OrderedMap(&[("fs", "error"), ("js-ffi", "warn")]);
let reverse = OrderedMap(&[("js-ffi", "warn"), ("fs", "error")]);
assert_eq!(json_fingerprint(&forward).unwrap(), json_fingerprint(&reverse).unwrap());
#[derive(Serialize)]
struct Entry<T> {
feature_policy: T,
}
assert_eq!(
json_fingerprint(&Entry { feature_policy: forward }).unwrap(),
json_fingerprint(&Entry { feature_policy: reverse }).unwrap()
);
assert_ne!(
json_fingerprint(&OrderedMap(&[("fs", "error"), ("js-ffi", "warn")])).unwrap(),
json_fingerprint(&OrderedMap(&[("fs", "warn"), ("js-ffi", "warn")])).unwrap()
);
assert_ne!(
json_fingerprint(&["first", "second"]).unwrap(),
json_fingerprint(&["second", "first"]).unwrap()
);
}
}