Trait apub_core::repo::Repo[][src]

pub trait Repo {
    type Error: 'static;
    fn fetch<'life0, 'async_trait, D: Dereference, S: Session>(
        &'life0 self,
        id: D,
        session: S
    ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
    where
        D: 'async_trait,
        S: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
; }
Expand description

A Repository from which objects can be dereferenced

This can describe both HTTP remotes, e.g. for fetching remote ActivityPub objects, as well as for fetching local objects from a database

use apub_core::{repo::{Dereference, Repo}, session::Session};
use std::{collections::HashMap, sync::{Arc, Mutex}};
use url::Url;

/// An in-memory repository for objects
pub struct MemoryRepo {
    inner: Arc<Mutex<HashMap<Url, serde_json::Value>>>,
}

#[async_trait::async_trait(?Send)]
impl Repo for MemoryRepo {
    type Error = serde_json::Error;

    async fn fetch<D: Dereference, S: Session>(&self, id: D, _: S) -> Result<Option<D::Output>, Self::Error> {
        if let Some(value) = self.inner.lock().unwrap().get(id.url()) {
            serde_json::from_value(value.clone()).map(Some)
        } else {
            Ok(None)
        }
    }
}

Associated Types

The Error produced by fetching an object

Required methods

Fetch the object from the repository

Implementations on Foreign Types

Implementors