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}'");