catcolab_backend/app.rs
1use firebase_auth::FirebaseUser;
2use socketioxide::SocketIo;
3use sqlx::PgPool;
4use thiserror::Error;
5use uuid::Uuid;
6
7/** Top-level application state.
8
9Cheaply cloneable and intended to be moved around the program.
10 */
11#[derive(Clone)]
12pub struct AppState {
13 /// Connection to the Postgres database.
14 pub db: PgPool,
15
16 /// Socket for communicating with Automerge document server.
17 pub automerge_io: SocketIo,
18}
19
20/// Context available to RPC procedures.
21#[derive(Clone)]
22pub struct AppCtx {
23 /// Application state.
24 pub state: AppState,
25
26 /// Authenticated Firebase user, if any.
27 pub user: Option<FirebaseUser>,
28}
29
30/// Top-level application error.
31#[derive(Error, Debug)]
32pub enum AppError {
33 /// Error from the SQL database.
34 #[error("SQL database error: {0}")]
35 Db(#[from] sqlx::Error),
36
37 /// Error from the socket communicating with the Automerge document server.
38 #[error("Error receiving acknowledgment from socket: {0}")]
39 Ack(#[from] socketioxide::AckError<()>),
40
41 /// Client made request with invalid data.
42 #[error("Request with invalid data: {0}")]
43 Invalid(String),
44
45 /// Client has not authenticated using Firebase auth.
46 #[error("Authentication credentials were not provided")]
47 Unauthorized,
48
49 /// Client does not have permission to perform the requested action on the
50 /// document ref.
51 #[error("Not authorized to access ref: {0}")]
52 Forbidden(Uuid),
53}