Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 144de0d

Browse files
committed
storage: freeze the error type on BoxRepository
This avoids having to deal with traits bounds everywhere. It also moves the `boxed()` method to the PgRepository, because it was unnecessary to keep it on the `Repository` trait
1 parent 48c4c34 commit 144de0d

File tree

12 files changed

+41
-48
lines changed

12 files changed

+41
-48
lines changed

crates/cli/src/app_state.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use mas_matrix::BoxHomeserverConnection;
3030
use mas_matrix_synapse::SynapseConnection;
3131
use mas_policy::{Policy, PolicyFactory};
3232
use mas_router::UrlBuilder;
33-
use mas_storage::{BoxClock, BoxRepository, BoxRng, Repository, SystemClock};
33+
use mas_storage::{BoxClock, BoxRepository, BoxRng, SystemClock};
3434
use mas_storage_pg::PgRepository;
3535
use mas_templates::Templates;
3636
use opentelemetry::{
@@ -351,8 +351,6 @@ impl FromRequestParts<AppState> for BoxRepository {
351351
histogram.record(duration_ms, &[]);
352352
}
353353

354-
Ok(repo
355-
.map_err(mas_storage::RepositoryError::from_error)
356-
.boxed())
354+
Ok(repo.boxed())
357355
}
358356
}

crates/handlers/src/activity_tracker/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use std::{collections::HashMap, net::IpAddr};
1616

1717
use chrono::{DateTime, Utc};
18-
use mas_storage::{user::BrowserSessionRepository, Repository, RepositoryAccess};
18+
use mas_storage::{user::BrowserSessionRepository, RepositoryAccess};
1919
use opentelemetry::{
2020
metrics::{Counter, Histogram},
2121
Key,

crates/handlers/src/graphql/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ use mas_axum_utils::{
4040
use mas_data_model::{BrowserSession, Session, SiteConfig, User};
4141
use mas_matrix::HomeserverConnection;
4242
use mas_policy::{InstantiateError, Policy, PolicyFactory};
43-
use mas_storage::{
44-
BoxClock, BoxRepository, BoxRng, Clock, Repository, RepositoryError, SystemClock,
45-
};
43+
use mas_storage::{BoxClock, BoxRepository, BoxRng, Clock, RepositoryError, SystemClock};
4644
use mas_storage_pg::PgRepository;
4745
use opentelemetry_semantic_conventions::trace::{GRAPHQL_DOCUMENT, GRAPHQL_OPERATION_NAME};
4846
use rand::{thread_rng, SeedableRng};
@@ -82,7 +80,7 @@ impl state::State for GraphQLState {
8280
.await
8381
.map_err(RepositoryError::from_error)?;
8482

85-
Ok(repo.map_err(RepositoryError::from_error).boxed())
83+
Ok(repo.boxed())
8684
}
8785

8886
async fn policy(&self) -> Result<Policy, InstantiateError> {

crates/handlers/src/test_utils.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey};
4343
use mas_matrix::{BoxHomeserverConnection, HomeserverConnection, MockHomeserverConnection};
4444
use mas_policy::{InstantiateError, Policy, PolicyFactory};
4545
use mas_router::{SimpleRoute, UrlBuilder};
46-
use mas_storage::{clock::MockClock, BoxClock, BoxRepository, BoxRng, Repository};
46+
use mas_storage::{clock::MockClock, BoxClock, BoxRepository, BoxRng};
4747
use mas_storage_pg::{DatabaseError, PgRepository};
4848
use mas_templates::{SiteConfigExt, Templates};
4949
use rand::SeedableRng;
@@ -272,9 +272,7 @@ impl TestState {
272272

273273
pub async fn repository(&self) -> Result<BoxRepository, DatabaseError> {
274274
let repo = PgRepository::from_pool(&self.pool).await?;
275-
Ok(repo
276-
.map_err(mas_storage::RepositoryError::from_error)
277-
.boxed())
275+
Ok(repo.boxed())
278276
}
279277

280278
/// Returns a new random number generator.
@@ -330,9 +328,7 @@ impl graphql::State for TestGraphQLState {
330328
.await
331329
.map_err(mas_storage::RepositoryError::from_error)?;
332330

333-
Ok(repo
334-
.map_err(mas_storage::RepositoryError::from_error)
335-
.boxed())
331+
Ok(repo.boxed())
336332
}
337333

338334
async fn policy(&self) -> Result<Policy, InstantiateError> {
@@ -500,9 +496,7 @@ impl FromRequestParts<TestState> for BoxRepository {
500496
state: &TestState,
501497
) -> Result<Self, Self::Rejection> {
502498
let repo = PgRepository::from_pool(&state.pool).await?;
503-
Ok(repo
504-
.map_err(mas_storage::RepositoryError::from_error)
505-
.boxed())
499+
Ok(repo.boxed())
506500
}
507501
}
508502

crates/storage-pg/src/compat/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod tests {
3636
CompatSessionRepository, CompatSsoLoginFilter,
3737
},
3838
user::UserRepository,
39-
Clock, Pagination, Repository, RepositoryAccess,
39+
Clock, Pagination, RepositoryAccess,
4040
};
4141
use rand::SeedableRng;
4242
use rand_chacha::ChaChaRng;

crates/storage-pg/src/oauth2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod tests {
3636
use mas_storage::{
3737
clock::MockClock,
3838
oauth2::{OAuth2DeviceCodeGrantParams, OAuth2SessionFilter, OAuth2SessionRepository},
39-
Clock, Pagination, Repository,
39+
Clock, Pagination,
4040
};
4141
use oauth2_types::{
4242
requests::{GrantType, ResponseMode},

crates/storage-pg/src/repository.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use mas_storage::{
3131
UpstreamOAuthSessionRepository,
3232
},
3333
user::{BrowserSessionRepository, UserEmailRepository, UserPasswordRepository, UserRepository},
34-
Repository, RepositoryAccess, RepositoryTransaction,
34+
BoxRepository, MapErr, Repository, RepositoryAccess, RepositoryError, RepositoryTransaction,
3535
};
3636
use sqlx::{PgConnection, PgPool, Postgres, Transaction};
3737
use tracing::Instrument;
@@ -76,6 +76,11 @@ impl PgRepository {
7676
let txn = pool.begin().await?;
7777
Ok(Self::from_conn(txn))
7878
}
79+
80+
/// Transform the repository into a type-erased [`BoxRepository`]
81+
pub fn boxed(self) -> BoxRepository {
82+
Box::new(MapErr::new(self, RepositoryError::from_error))
83+
}
7984
}
8085

8186
impl<C> PgRepository<C> {

crates/storage-pg/src/user/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use mas_storage::{
1919
BrowserSessionFilter, BrowserSessionRepository, UserEmailFilter, UserEmailRepository,
2020
UserFilter, UserPasswordRepository, UserRepository,
2121
},
22-
Pagination, Repository, RepositoryAccess,
22+
Pagination, RepositoryAccess,
2323
};
2424
use rand::SeedableRng;
2525
use rand_chacha::ChaChaRng;

crates/storage/src/pagination.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,27 @@ impl Pagination {
104104
self
105105
}
106106

107+
/// Clear the before cursor
108+
#[must_use]
109+
pub const fn clear_before(mut self) -> Self {
110+
self.before = None;
111+
self
112+
}
113+
107114
/// Get items after the given cursor
108115
#[must_use]
109116
pub const fn after(mut self, id: Ulid) -> Self {
110117
self.after = Some(id);
111118
self
112119
}
113120

121+
/// Clear the after cursor
122+
#[must_use]
123+
pub const fn clear_after(mut self) -> Self {
124+
self.after = None;
125+
self
126+
}
127+
114128
/// Process a page returned by a paginated query
115129
#[must_use]
116130
pub fn process<T>(&self, mut edges: Vec<T>) -> Page<T> {

crates/storage/src/repository.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::{
3434
BrowserSessionRepository, UserEmailRepository, UserPasswordRepository,
3535
UserRecoveryRepository, UserRepository, UserTermsRepository,
3636
},
37-
MapErr,
3837
};
3938

4039
/// A [`Repository`] helps interacting with the underlying storage backend.
@@ -43,21 +42,6 @@ pub trait Repository<E>:
4342
where
4443
E: std::error::Error + Send + Sync + 'static,
4544
{
46-
/// Construct a (boxed) typed-erased repository
47-
fn boxed(self) -> BoxRepository<E>
48-
where
49-
Self: Sync + Sized + 'static,
50-
{
51-
Box::new(self)
52-
}
53-
54-
/// Map the error type of all the methods of a [`Repository`]
55-
fn map_err<Mapper>(self, mapper: Mapper) -> MapErr<Self, Mapper>
56-
where
57-
Self: Sized,
58-
{
59-
MapErr::new(self, mapper)
60-
}
6145
}
6246

6347
/// An opaque, type-erased error
@@ -80,7 +64,7 @@ impl RepositoryError {
8064
}
8165

8266
/// A type-erased [`Repository`]
83-
pub type BoxRepository<E = RepositoryError> = Box<dyn Repository<E> + Send + Sync + 'static>;
67+
pub type BoxRepository = Box<dyn Repository<RepositoryError> + Send + Sync + 'static>;
8468

8569
/// A [`RepositoryTransaction`] can be saved or cancelled, after a series
8670
/// of operations.
@@ -113,7 +97,7 @@ pub trait RepositoryTransaction {
11397
/// repository is used at a time.
11498
///
11599
/// When adding a new repository, you should add a new method to this trait, and
116-
/// update the implementations for [`MapErr`] and [`Box<R>`] below.
100+
/// update the implementations for [`crate::MapErr`] and [`Box<R>`] below.
117101
///
118102
/// Note: this used to have generic associated types to avoid boxing all the
119103
/// repository traits, but that was removed because it made almost impossible to
@@ -218,7 +202,7 @@ pub trait RepositoryAccess: Send {
218202
}
219203

220204
/// Implementations of the [`RepositoryAccess`], [`RepositoryTransaction`] and
221-
/// [`Repository`] for the [`MapErr`] wrapper and [`Box<R>`]
205+
/// [`Repository`] for the [`crate::MapErr`] wrapper and [`Box<R>`]
222206
mod impls {
223207
use futures_util::{future::BoxFuture, FutureExt, TryFutureExt};
224208

0 commit comments

Comments
 (0)