Skip to content

Commit

Permalink
refactor: Make return
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Mar 20, 2024
1 parent b965982 commit 778fd84
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/delegation/agent.rs
Expand Up @@ -14,7 +14,7 @@ use std::{collections::BTreeMap, marker::PhantomData};
use thiserror::Error;
use web_time::SystemTime;

/// A stateful agent capable of delegatint to others, and being delegated to.
/// A stateful agent capable of delegating to others, and being delegated to.
///
/// This is helpful for sessions where more than one delegation will be made.
#[derive(Debug)]
Expand Down
16 changes: 10 additions & 6 deletions src/delegation/store/memory.rs
Expand Up @@ -10,7 +10,10 @@ use libipld_core::codec::Encode;
use libipld_core::ipld::Ipld;
use libipld_core::{cid::Cid, codec::Codec};
use nonempty::NonEmpty;
use std::collections::{BTreeMap, BTreeSet};
use std::{
collections::{BTreeMap, BTreeSet},
convert::Infallible,
};
use web_time::SystemTime;

#[cfg_attr(doc, aquamarine::aquamarine)]
Expand Down Expand Up @@ -117,12 +120,13 @@ where
delegation::Payload<DID>: TryFrom<Named<Ipld>>,
Delegation<DID, V, Enc>: Encode<Enc>,
{
type DelegationStoreError = String; // FIXME misisng
type DelegationStoreError = Infallible;

fn get(&self, cid: &Cid) -> Result<&Delegation<DID, V, Enc>, Self::DelegationStoreError> {
self.ucans
.get(cid)
.ok_or(format!("not found in delegation memstore: {:?}", cid).into())
fn get(
&self,
cid: &Cid,
) -> Result<Option<&Delegation<DID, V, Enc>>, Self::DelegationStoreError> {
Ok(self.ucans.get(cid))
// FIXME
}

Expand Down
9 changes: 6 additions & 3 deletions src/delegation/store/traits.rs
Expand Up @@ -11,7 +11,10 @@ use web_time::SystemTime;
pub trait Store<DID: Did, V: varsig::Header<Enc>, Enc: Codec + TryFrom<u64> + Into<u64>> {
type DelegationStoreError: Debug;

fn get(&self, cid: &Cid) -> Result<&Delegation<DID, V, Enc>, Self::DelegationStoreError>;
fn get(
&self,
cid: &Cid,
) -> Result<Option<&Delegation<DID, V, Enc>>, Self::DelegationStoreError>;

fn insert(
&mut self,
Expand Down Expand Up @@ -60,7 +63,7 @@ pub trait Store<DID: Did, V: varsig::Header<Enc>, Enc: Codec + TryFrom<u64> + In
fn get_many(
&self,
cids: &[Cid],
) -> Result<Vec<&Delegation<DID, V, Enc>>, Self::DelegationStoreError> {
) -> Result<Vec<Option<&Delegation<DID, V, Enc>>>, Self::DelegationStoreError> {
cids.iter().try_fold(vec![], |mut acc, cid| {
acc.push(self.get(cid)?);
Ok(acc)
Expand All @@ -73,7 +76,7 @@ impl<T: Store<DID, V, C>, DID: Did, V: varsig::Header<C>, C: Codec + TryFrom<u64
{
type DelegationStoreError = <T as Store<DID, V, C>>::DelegationStoreError;

fn get(&self, cid: &Cid) -> Result<&Delegation<DID, V, C>, Self::DelegationStoreError> {
fn get(&self, cid: &Cid) -> Result<Option<&Delegation<DID, V, C>>, Self::DelegationStoreError> {
(**self).get(cid)
}

Expand Down
8 changes: 6 additions & 2 deletions src/invocation/agent.rs
Expand Up @@ -209,8 +209,9 @@ where
.get_many(&invocation.proofs())
.map_err(ReceiveError::DelegationStoreError)?
.iter()
.map(|d| &d.payload)
.collect();
.zip(invocation.proofs().iter())
.map(|(d, cid)| Ok(&d.ok_or(ReceiveError::MissingDelegation(*cid))?.payload))
.collect::<Result<_, ReceiveError<T, DID, D::DelegationStoreError, S, V, C>>>()?;

let _ = &invocation
.payload
Expand Down Expand Up @@ -290,6 +291,9 @@ pub enum ReceiveError<
> where
<S as Store<T, DID, V, C>>::InvocationStoreError: fmt::Debug,
{
#[error("missing delegation: {0}")]
MissingDelegation(Cid),

#[error("encoding error: {0}")]
EncodingError(#[from] libipld_core::error::Error),

Expand Down

0 comments on commit 778fd84

Please sign in to comment.