You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
Box the repository everywhere
This commit is contained in:
@ -28,21 +28,21 @@
|
||||
clippy::module_name_repetitions
|
||||
)]
|
||||
|
||||
use rand_core::CryptoRngCore;
|
||||
|
||||
pub mod clock;
|
||||
pub mod pagination;
|
||||
pub(crate) mod repository;
|
||||
|
||||
pub mod compat;
|
||||
pub mod oauth2;
|
||||
pub mod pagination;
|
||||
pub(crate) mod repository;
|
||||
pub mod upstream_oauth2;
|
||||
pub mod user;
|
||||
|
||||
use rand_core::CryptoRngCore;
|
||||
|
||||
pub use self::{
|
||||
clock::{Clock, SystemClock},
|
||||
pagination::{Page, Pagination},
|
||||
repository::Repository,
|
||||
repository::{BoxRepository, Repository, RepositoryError},
|
||||
};
|
||||
|
||||
pub struct MapErr<Repository, Mapper> {
|
||||
@ -86,7 +86,6 @@ macro_rules! repository_impl {
|
||||
where
|
||||
R: $repo_trait,
|
||||
F: FnMut(<R as $repo_trait>::Error) -> E + ::std::marker::Send + ::std::marker::Sync,
|
||||
E: ::std::error::Error + ::std::marker::Send + ::std::marker::Sync,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
|
@ -12,6 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use futures_util::{future::BoxFuture, FutureExt, TryFutureExt};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
compat::{
|
||||
CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionRepository,
|
||||
@ -32,6 +35,23 @@ use crate::{
|
||||
pub trait Repository: Send {
|
||||
type Error: std::error::Error + Send + Sync + 'static;
|
||||
|
||||
fn map_err<Mapper>(self, mapper: Mapper) -> MapErr<Self, Mapper>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
MapErr::new(self, mapper)
|
||||
}
|
||||
|
||||
fn boxed(self) -> BoxRepository<Self::Error>
|
||||
where
|
||||
Self: Sized + Sync + 'static,
|
||||
{
|
||||
Box::new(self)
|
||||
}
|
||||
|
||||
fn save(self: Box<Self>) -> BoxFuture<'static, Result<(), Self::Error>>;
|
||||
fn cancel(self: Box<Self>) -> BoxFuture<'static, Result<(), Self::Error>>;
|
||||
|
||||
fn upstream_oauth_link<'c>(
|
||||
&'c mut self,
|
||||
) -> Box<dyn UpstreamOAuthLinkRepository<Error = Self::Error> + 'c>;
|
||||
@ -91,14 +111,44 @@ pub trait Repository: Send {
|
||||
) -> Box<dyn CompatRefreshTokenRepository<Error = Self::Error> + 'c>;
|
||||
}
|
||||
|
||||
/// An opaque, type-erased error
|
||||
#[derive(Debug, Error)]
|
||||
#[error(transparent)]
|
||||
pub struct RepositoryError {
|
||||
source: Box<dyn std::error::Error + Send + Sync + 'static>,
|
||||
}
|
||||
|
||||
impl RepositoryError {
|
||||
pub fn from_error<E>(value: E) -> Self
|
||||
where
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
Self {
|
||||
source: Box::new(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BoxRepository<E = RepositoryError> =
|
||||
Box<dyn Repository<Error = E> + Send + Sync + 'static>;
|
||||
|
||||
impl<R, F, E> Repository for crate::MapErr<R, F>
|
||||
where
|
||||
R: Repository,
|
||||
F: FnMut(R::Error) -> E + Send + Sync,
|
||||
R::Error: 'static,
|
||||
F: FnMut(R::Error) -> E + Send + Sync + 'static,
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn save(self: Box<Self>) -> BoxFuture<'static, Result<(), Self::Error>> {
|
||||
Box::new(self.inner).save().map_err(self.mapper).boxed()
|
||||
}
|
||||
|
||||
fn cancel(self: Box<Self>) -> BoxFuture<'static, Result<(), Self::Error>> {
|
||||
Box::new(self.inner).cancel().map_err(self.mapper).boxed()
|
||||
}
|
||||
|
||||
fn upstream_oauth_link<'c>(
|
||||
&'c mut self,
|
||||
) -> Box<dyn UpstreamOAuthLinkRepository<Error = Self::Error> + 'c> {
|
||||
|
@ -21,7 +21,7 @@ use crate::{pagination::Page, repository_impl, Clock, Pagination};
|
||||
|
||||
#[async_trait]
|
||||
pub trait UpstreamOAuthLinkRepository: Send + Sync {
|
||||
type Error: std::error::Error + Send + Sync;
|
||||
type Error;
|
||||
|
||||
/// Lookup an upstream OAuth link by its ID
|
||||
async fn lookup(&mut self, id: Ulid) -> Result<Option<UpstreamOAuthLink>, Self::Error>;
|
||||
|
Reference in New Issue
Block a user