1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-23 11:02:35 +03:00

Allow fetching more nodes by their IDs

This commit is contained in:
Quentin Gliech
2022-11-16 17:21:40 +01:00
parent 10815d8101
commit 78778648ca
18 changed files with 232 additions and 67 deletions

View File

@@ -35,6 +35,54 @@ use sqlx::migrate::Migrator;
use thiserror::Error;
use ulid::Ulid;
#[derive(Debug, Error)]
#[error("failed to lookup {what}")]
pub struct GenericLookupError {
what: &'static str,
source: sqlx::Error,
}
impl GenericLookupError {
#[must_use]
pub fn what(what: &'static str) -> Box<dyn Fn(sqlx::Error) -> Self> {
Box::new(move |source: sqlx::Error| Self { what, source })
}
}
impl LookupError for GenericLookupError {
fn not_found(&self) -> bool {
matches!(self.source, sqlx::Error::RowNotFound)
}
}
pub trait LookupError {
fn not_found(&self) -> bool;
}
pub trait LookupResultExt {
type Error;
type Output;
/// Transform a [`Result`] with a [`LookupError`] to transform "not
/// found" errors into [`None`]
fn to_option(self) -> Result<Option<Self::Output>, Self::Error>;
}
impl<T, E> LookupResultExt for Result<T, E>
where
E: LookupError,
{
type Output = T;
type Error = E;
fn to_option(self) -> Result<Option<Self::Output>, Self::Error> {
match self {
Ok(v) => Ok(Some(v)),
Err(e) if e.not_found() => Ok(None),
Err(e) => Err(e),
}
}
}
#[derive(Default, Debug, Clone, Copy)]
pub struct Clock {
_private: (),