catcolab_backend/
app.rs

1use firebase_auth::FirebaseUser;
2use serde::Serialize;
3use socketioxide::SocketIo;
4use sqlx::PgPool;
5use thiserror::Error;
6use ts_rs::TS;
7use uuid::Uuid;
8
9/** Top-level application state.
10
11Cheaply cloneable and intended to be moved around the program.
12 */
13#[derive(Clone)]
14pub struct AppState {
15    /// Connection to the Postgres database.
16    pub db: PgPool,
17
18    /// Socket for communicating with Automerge document server.
19    pub automerge_io: SocketIo,
20}
21
22/// Context available to RPC procedures.
23#[derive(Clone)]
24pub struct AppCtx {
25    /// Application state.
26    pub state: AppState,
27
28    /// Authenticated Firebase user, if any.
29    pub user: Option<FirebaseUser>,
30}
31
32/// A page of items along with pagination metadata.
33#[derive(Clone, Debug, Serialize, TS)]
34pub struct Paginated<T> {
35    /// The total number of items matching the query criteria.
36    pub total: i32,
37
38    /// The number of items skipped.
39    pub offset: i32,
40
41    /// The items in the current page.
42    pub items: Vec<T>,
43}
44
45/// Top-level application error.
46#[derive(Error, Debug)]
47pub enum AppError {
48    /// Error from the SQL database.
49    #[error("SQL database error: {0}")]
50    Db(#[from] sqlx::Error),
51
52    /// Error from the socket communicating with the Automerge document server.
53    #[error("Error receiving acknowledgment from socket: {0}")]
54    Ack(#[from] socketioxide::AckError<()>),
55
56    /// Client made request with invalid data.
57    #[error("Request with invalid data: {0}")]
58    Invalid(String),
59
60    /// Client has not authenticated using Firebase auth.
61    #[error("Authentication credentials were not provided")]
62    Unauthorized,
63
64    /// Something went wrong in a socket call to the automerge server
65    #[error("Automerge server error: {0}")]
66    AutomergeServer(String),
67
68    /// Client does not have permission to perform the requested action on the
69    /// document ref.
70    #[error("Not authorized to access ref: {0}")]
71    Forbidden(Uuid),
72}