From 04481b3b8d94db683f73672169ef09a465bf91d5 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 27 Mar 2025 15:37:52 +0100 Subject: [PATCH] Fixes for either impl --- src/traits/either.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) 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 {