From 86035c287804444c434b76067a84443d9da6b72f Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 10 Nov 2024 21:39:38 +0100 Subject: [PATCH] feat: improved dereferenceable trait for nodes --- upub/core/src/traits/fetch.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/upub/core/src/traits/fetch.rs b/upub/core/src/traits/fetch.rs index 6765e74..c4a4b40 100644 --- a/upub/core/src/traits/fetch.rs +++ b/upub/core/src/traits/fetch.rs @@ -464,26 +464,28 @@ async fn resolve_object_r(ctx: &crate::Context, object: serde_json::Value, depth } #[allow(async_fn_in_trait)] -pub trait Fetchable : Sync + Send { - async fn fetch(&mut self, ctx: &crate::Context) -> Result<&mut Self, RequestError>; +pub trait Dereferenceable : Sync + Send { + async fn resolve(self, ctx: &crate::Context) -> Result; } -impl Fetchable for apb::Node { - async fn fetch(&mut self, ctx: &crate::Context) -> Result<&mut Self, RequestError> { - if let apb::Node::Link(uri) = self { - if let Ok(href) = uri.href() { - *self = crate::Context::request(Method::GET, href, None, ctx.base(), ctx.pkey(), ctx.domain()) +impl Dereferenceable for apb::Node { + async fn resolve(self, ctx: &crate::Context) -> Result { + match self { + apb::Node::Link(uri) => { + let href = uri.href()?; + tracing::info!("dereferencing {href}"); + let res = crate::Context::request(Method::GET, href, None, ctx.base(), ctx.pkey(), ctx.domain()) .await? .json::() - .await? - .into(); - } + .await?; + Ok(res) + }, + apb::Node::Object(x) => Ok(*x), + apb::Node::Empty => Err(RequestError::Tombstone), + apb::Node::Array(_) => Err(RequestError::Malformed(apb::FieldErr("id"))), // TODO weird!! } - - Ok(self) } } - // #[async_recursion::async_recursion] // async fn crawl_replies(ctx: &crate::Context, id: &str, depth: usize) -> Result<(), PullError> { // tracing::info!("crawling replies of '{id}'");