dag_diff

Function dag_diff 

Source
pub fn dag_diff(
    dag1: &Ipld,
    dag2: &Ipld,
) -> (HashSet<Cid>, HashSet<Cid>, HashSet<Cid>)
Expand description

Find the differences between two IPLD DAGs

Returns a tuple of (unique_to_first, unique_to_second, common_links). This is useful for determining what changed between two versions of a DAG.

§Arguments

  • dag1 - First DAG to compare
  • dag2 - Second DAG to compare

§Returns

A tuple of:

  • Links unique to dag1
  • Links unique to dag2
  • Links common to both

§Example

use ipfrs_core::{Ipld, CidBuilder};
use ipfrs_core::dag::dag_diff;
use std::collections::BTreeMap;

let cid1 = CidBuilder::new().build(b"a").unwrap();
let cid2 = CidBuilder::new().build(b"b").unwrap();
let cid3 = CidBuilder::new().build(b"c").unwrap();

let mut map1 = BTreeMap::new();
map1.insert("link1".to_string(), Ipld::link(cid1));
map1.insert("link2".to_string(), Ipld::link(cid2));

let mut map2 = BTreeMap::new();
map2.insert("link2".to_string(), Ipld::link(cid2));
map2.insert("link3".to_string(), Ipld::link(cid3));

let dag1 = Ipld::Map(map1);
let dag2 = Ipld::Map(map2);

let (unique1, unique2, common) = dag_diff(&dag1, &dag2);
assert_eq!(unique1.len(), 1); // cid1
assert_eq!(unique2.len(), 1); // cid3
assert_eq!(common.len(), 1);  // cid2