feat: improved dereferenceable trait for nodes

This commit is contained in:
əlemi 2024-11-10 21:39:38 +01:00
parent 7d14bccdee
commit 86035c2878
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -464,26 +464,28 @@ async fn resolve_object_r(ctx: &crate::Context, object: serde_json::Value, depth
} }
#[allow(async_fn_in_trait)] #[allow(async_fn_in_trait)]
pub trait Fetchable : Sync + Send { pub trait Dereferenceable<T> : Sync + Send {
async fn fetch(&mut self, ctx: &crate::Context) -> Result<&mut Self, RequestError>; async fn resolve(self, ctx: &crate::Context) -> Result<T, RequestError>;
} }
impl Fetchable for apb::Node<serde_json::Value> { impl Dereferenceable<serde_json::Value> for apb::Node<serde_json::Value> {
async fn fetch(&mut self, ctx: &crate::Context) -> Result<&mut Self, RequestError> { async fn resolve(self, ctx: &crate::Context) -> Result<serde_json::Value, RequestError> {
if let apb::Node::Link(uri) = self { match self {
if let Ok(href) = uri.href() { apb::Node::Link(uri) => {
*self = crate::Context::request(Method::GET, href, None, ctx.base(), ctx.pkey(), ctx.domain()) let href = uri.href()?;
tracing::info!("dereferencing {href}");
let res = crate::Context::request(Method::GET, href, None, ctx.base(), ctx.pkey(), ctx.domain())
.await? .await?
.json::<serde_json::Value>() .json::<serde_json::Value>()
.await? .await?;
.into(); 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_recursion::async_recursion]
// async fn crawl_replies(ctx: &crate::Context, id: &str, depth: usize) -> Result<(), PullError> { // async fn crawl_replies(ctx: &crate::Context, id: &str, depth: usize) -> Result<(), PullError> {
// tracing::info!("crawling replies of '{id}'"); // tracing::info!("crawling replies of '{id}'");