radicle_cob/object/collaboration/
get.rs

1// Copyright © 2022 The Radicle Link Contributors
2
3use crate::{change_graph::ChangeGraph, CollaborativeObject, Evaluate, ObjectId, Store, TypeName};
4
5use super::error;
6
7/// Get a [`CollaborativeObject`], if it exists.
8///
9/// The `storage` is the backing storage for storing
10/// [`crate::Entry`]s at content-addressable locations. Please see
11/// [`Store`] for further information.
12///
13/// The `typename` is the type of object to be found, while the
14/// `object_id` is the identifier for the particular object under that
15/// type.
16pub fn get<T, S>(
17    storage: &S,
18    typename: &TypeName,
19    oid: &ObjectId,
20) -> Result<Option<CollaborativeObject<T>>, error::Retrieve>
21where
22    T: Evaluate<S>,
23    S: Store,
24{
25    let tip_refs = storage
26        .objects(typename, oid)
27        .map_err(|err| error::Retrieve::Refs { err: Box::new(err) })?;
28
29    ChangeGraph::load(storage, tip_refs.iter(), typename, oid)
30        .map(|graph| graph.evaluate(storage).map_err(error::Retrieve::evaluate))
31        .transpose()
32}