1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{connected_client::ConnectedClient, ErrorHandling, RequestHandling};
use async_handle::Handle;
use async_trait::async_trait;
use basws_shared::{protocol::InstallationConfig, Uuid, VersionReq};
use serde::{de::DeserializeOwned, Serialize};
use std::{fmt::Debug, hash::Hash};

pub trait Identifiable {
    type Id: Copy + Hash + Eq + Send + Sync;
    fn id(&self) -> Self::Id;
}

#[async_trait]
pub trait ServerLogic: Send + Sync {
    type Request: Serialize + DeserializeOwned + Clone + Send + Sync + Debug;
    type Response: Serialize + DeserializeOwned + Clone + Send + Sync + Debug;
    type Account: Identifiable<Id = Self::AccountId>
        + Serialize
        + DeserializeOwned
        + Send
        + Sync
        + Debug;
    type AccountId: Copy + Hash + Eq + Send + Sync;

    // TODO Make ConnectedClient an opaque type

    async fn handle_request(
        &self,
        client: &ConnectedClient<Self::Response, Self::Account>,
        request: Self::Request,
    ) -> anyhow::Result<RequestHandling<Self::Response>>;

    async fn lookup_account_from_installation_id(
        &self,
        installation_id: Uuid,
    ) -> anyhow::Result<Option<Handle<Self::Account>>>;

    fn protocol_version_requirements(&self) -> VersionReq;

    async fn lookup_or_create_installation(
        &self,
        installation_id: Option<Uuid>,
    ) -> anyhow::Result<InstallationConfig>;

    async fn client_reconnected(
        &self,
        installation_id: Uuid,
        account: Option<Handle<Self::Account>>,
    ) -> anyhow::Result<RequestHandling<Self::Response>>;

    async fn handle_websocket_error(&self, _err: warp::Error) -> ErrorHandling {
        ErrorHandling::Disconnect
    }

    async fn new_installation_connected(
        &self,
        installation_id: Uuid,
    ) -> anyhow::Result<RequestHandling<Self::Response>>;
}