gitea_pages/backend/src/api/loaders/mod.rs

78 lines
2.2 KiB
Rust

use async_trait::async_trait;
use std::clone::Clone;
use std::fmt::Debug;
use std::hash::Hash;
use std::io::{Error, ErrorKind};
pub mod repository;
pub mod user;
#[async_trait]
pub trait TryOptionLoad<K, V>: Clone
where
K: Eq + Hash + Clone + Debug + Send + Sync,
V: Clone + Debug + Send,
{
async fn try_option_load(&self, key: K) -> Result<Option<V>, Error>;
}
#[async_trait]
impl<K, V, F> TryOptionLoad<K, V> for dataloader::non_cached::Loader<K, V, F>
where
K: Eq + Hash + Clone + Debug + Send + Sync,
V: Clone + Debug + Send,
F: dataloader::BatchFn<K, V> + Send + Sync,
{
async fn try_option_load(&self, key: K) -> Result<Option<V>, Error> {
async fn internal_try_option_load<K, V, F>(
loader: &dataloader::non_cached::Loader<K, V, F>,
key: K,
) -> Result<Option<V>, Error>
where
K: Eq + Hash + Clone + Debug + Send + Sync,
V: Clone + Debug + Send,
F: dataloader::BatchFn<K, V> + Send + Sync,
{
match loader.try_load(key).await {
Ok(x) => Ok(Some(x)),
Err(e) => match e.kind() {
ErrorKind::NotFound => Ok(None),
_ => Err(e),
},
}
}
internal_try_option_load(self, key).await
}
}
#[async_trait]
impl<K, V, F> TryOptionLoad<K, V> for dataloader::cached::Loader<K, V, F>
where
K: Eq + Hash + Clone + Debug + Send + Sync,
V: Clone + Debug + Send,
F: dataloader::BatchFn<K, V> + Send + Sync,
{
async fn try_option_load(&self, key: K) -> Result<Option<V>, Error> {
async fn internal_try_option_load<K, V, F>(
loader: &dataloader::cached::Loader<K, V, F>,
key: K,
) -> Result<Option<V>, Error>
where
K: Eq + Hash + Clone + Debug + Send + Sync,
V: Clone + Debug + Send,
F: dataloader::BatchFn<K, V> + Send + Sync,
{
match loader.try_load(key).await {
Ok(x) => Ok(Some(x)),
Err(e) => match e.kind() {
ErrorKind::NotFound => Ok(None),
_ => Err(e),
},
}
}
internal_try_option_load(self, key).await
}
}