From 778fd8454473cbeef4cb4add8cda04bf2fda8cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 20 Mar 2024 09:59:09 +0100 Subject: [PATCH] refactor: Make return --- src/delegation/agent.rs | 2 +- src/delegation/store/memory.rs | 16 ++++++++++------ src/delegation/store/traits.rs | 9 ++++++--- src/invocation/agent.rs | 8 ++++++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/delegation/agent.rs b/src/delegation/agent.rs index 89f86a8b..886b331b 100644 --- a/src/delegation/agent.rs +++ b/src/delegation/agent.rs @@ -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)] diff --git a/src/delegation/store/memory.rs b/src/delegation/store/memory.rs index 18d86103..297715e2 100644 --- a/src/delegation/store/memory.rs +++ b/src/delegation/store/memory.rs @@ -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)] @@ -117,12 +120,13 @@ where delegation::Payload: TryFrom>, Delegation: Encode, { - type DelegationStoreError = String; // FIXME misisng + type DelegationStoreError = Infallible; - fn get(&self, cid: &Cid) -> Result<&Delegation, Self::DelegationStoreError> { - self.ucans - .get(cid) - .ok_or(format!("not found in delegation memstore: {:?}", cid).into()) + fn get( + &self, + cid: &Cid, + ) -> Result>, Self::DelegationStoreError> { + Ok(self.ucans.get(cid)) // FIXME } diff --git a/src/delegation/store/traits.rs b/src/delegation/store/traits.rs index 3bd7b1fb..2f8f42d5 100644 --- a/src/delegation/store/traits.rs +++ b/src/delegation/store/traits.rs @@ -11,7 +11,10 @@ use web_time::SystemTime; pub trait Store, Enc: Codec + TryFrom + Into> { type DelegationStoreError: Debug; - fn get(&self, cid: &Cid) -> Result<&Delegation, Self::DelegationStoreError>; + fn get( + &self, + cid: &Cid, + ) -> Result>, Self::DelegationStoreError>; fn insert( &mut self, @@ -60,7 +63,7 @@ pub trait Store, Enc: Codec + TryFrom + In fn get_many( &self, cids: &[Cid], - ) -> Result>, Self::DelegationStoreError> { + ) -> Result>>, Self::DelegationStoreError> { cids.iter().try_fold(vec![], |mut acc, cid| { acc.push(self.get(cid)?); Ok(acc) @@ -73,7 +76,7 @@ impl, DID: Did, V: varsig::Header, C: Codec + TryFrom>::DelegationStoreError; - fn get(&self, cid: &Cid) -> Result<&Delegation, Self::DelegationStoreError> { + fn get(&self, cid: &Cid) -> Result>, Self::DelegationStoreError> { (**self).get(cid) } diff --git a/src/invocation/agent.rs b/src/invocation/agent.rs index a4e5f7fd..c93e8036 100644 --- a/src/invocation/agent.rs +++ b/src/invocation/agent.rs @@ -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::>>()?; let _ = &invocation .payload @@ -290,6 +291,9 @@ pub enum ReceiveError< > where >::InvocationStoreError: fmt::Debug, { + #[error("missing delegation: {0}")] + MissingDelegation(Cid), + #[error("encoding error: {0}")] EncodingError(#[from] libipld_core::error::Error),