From b3dac339907c56c5b80df5a61b88602662735422 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Thu, 27 Mar 2025 14:58:45 +0000 Subject: [PATCH] Fixes for either impl (#140) * Fixes for either impl * link --- src/http_signatures.rs | 2 +- src/traits/either.rs | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/http_signatures.rs b/src/http_signatures.rs index ca6a02f..c255f03 100644 --- a/src/http_signatures.rs +++ b/src/http_signatures.rs @@ -57,7 +57,7 @@ impl Keypair { /// /// Note that this method is very slow in debug mode. To make it faster, follow /// instructions in the RSA crate's readme. -/// https://github.com/RustCrypto/RSA/blob/master/README.md +/// pub fn generate_actor_keypair() -> Result { let mut rng = rand::thread_rng(); let rsa = RsaPrivateKey::new(&mut rng, 2048)?; diff --git a/src/traits/either.rs b/src/traits/either.rs index 63a6ba4..95c2411 100644 --- a/src/traits/either.rs +++ b/src/traits/either.rs @@ -1,10 +1,19 @@ +use super::{Actor, Object}; use crate::{config::Data, error::Error}; use async_trait::async_trait; use chrono::{DateTime, Utc}; use either::Either; +use serde::{Deserialize, Serialize}; +use std::fmt::Debug; use url::Url; -use super::{Actor, Object}; +#[doc(hidden)] +#[derive(Serialize, Deserialize)] +#[serde(untagged)] +pub enum UntaggedEither { + Left(L), + Right(R), +} #[async_trait] impl Object for Either @@ -14,10 +23,10 @@ where ::Kind: Send + Sync, ::Kind: Send + Sync, D: Sync + Send + Clone, - E: From, + E: From + Debug, { type DataType = D; - type Kind = Either; + type Kind = UntaggedEither; type Error = E; fn last_refreshed_at(&self) -> Option> { @@ -39,7 +48,7 @@ where if let Some(r) = r { return Ok(Some(Either::Right(r))); } - Err(Error::NotFound.into()) + Ok(None) } async fn delete(self, data: &Data) -> Result<(), Self::Error> { @@ -51,8 +60,8 @@ where async fn into_json(self, data: &Data) -> Result { Ok(match self { - Either::Left(l) => Either::Left(l.into_json(data).await?), - Either::Right(r) => Either::Right(r.into_json(data).await?), + Either::Left(l) => UntaggedEither::Left(l.into_json(data).await?), + Either::Right(r) => UntaggedEither::Right(r.into_json(data).await?), }) } @@ -62,16 +71,16 @@ where data: &Data, ) -> Result<(), Self::Error> { match json { - Either::Left(l) => T::verify(l, expected_domain, data).await?, - Either::Right(r) => R::verify(r, expected_domain, data).await?, + UntaggedEither::Left(l) => T::verify(l, expected_domain, data).await?, + UntaggedEither::Right(r) => R::verify(r, expected_domain, data).await?, }; Ok(()) } async fn from_json(json: Self::Kind, data: &Data) -> Result { Ok(match json { - Either::Left(l) => Either::Left(T::from_json(l, data).await?), - Either::Right(r) => Either::Right(R::from_json(r, data).await?), + UntaggedEither::Left(l) => Either::Left(T::from_json(l, data).await?), + UntaggedEither::Right(r) => Either::Right(R::from_json(r, data).await?), }) } } @@ -84,7 +93,7 @@ where ::Kind: Send + Sync, ::Kind: Send + Sync, D: Sync + Send + Clone, - E: From, + E: From + Debug, { fn id(&self) -> Url { match self {