radicle_cob/object/collaboration/info.rs
1// Copyright © 2022 The Radicle Link Contributors
2
3//! [`ChangeGraphInfo`] provides a useful debugging structure for
4//! representing a single [`crate::CollaborativeObject`]'s underlying
5//! change graph.
6
7use std::collections::BTreeSet;
8
9use git_ext::Oid;
10
11use crate::{change_graph::ChangeGraph, ObjectId, Store, TypeName};
12
13use super::error;
14
15/// Additional information about the change graph of an object
16pub struct ChangeGraphInfo {
17 /// The ID of the object
18 pub object_id: ObjectId,
19 /// The number of nodes in the change graph of the object
20 pub number_of_nodes: usize,
21 /// The "tips" of the change graph, i.e the object IDs pointed to by
22 /// references to the object
23 pub tips: BTreeSet<Oid>,
24}
25
26/// Retrieve additional information about the change graph of an object. This
27/// is mostly useful for debugging and testing
28///
29/// The `storage` is the backing storage for storing
30/// [`crate::Entry`]s at content-addressable locations. Please see
31/// [`Store`] for further information.
32///
33/// The `typename` is the type of object to be found, while the `oid`
34/// is the identifier for the particular object under that type.
35pub fn changegraph<S>(
36 storage: &S,
37 typename: &TypeName,
38 oid: &ObjectId,
39) -> Result<Option<ChangeGraphInfo>, error::Retrieve>
40where
41 S: Store,
42{
43 let tip_refs = storage
44 .objects(typename, oid)
45 .map_err(|err| error::Retrieve::Refs { err: Box::new(err) })?;
46 Ok(
47 ChangeGraph::load(storage, tip_refs.iter(), typename, oid).map(|graph| ChangeGraphInfo {
48 object_id: *oid,
49 number_of_nodes: graph.number_of_nodes(),
50 tips: graph.tips(),
51 }),
52 )
53}