JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Completely refactor project structure

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ae8ff44

⁨giterated-daemon/src/connection.rs⁩ - ⁨2175⁩ bytes
Raw
1 pub mod authentication;
2 pub mod handshake;
3 pub mod repository;
4 pub mod user;
5 pub mod wrapper;
6
7 use std::{any::type_name, collections::HashMap};
8
9 use anyhow::Error;
10 use giterated_models::{
11 messages::ErrorMessage,
12 model::instance::{Instance, InstanceMeta},
13 };
14 use serde::{de::DeserializeOwned, Serialize};
15 use tokio::{net::TcpStream, task::JoinHandle};
16 use tokio_tungstenite::WebSocketStream;
17
18 #[derive(Debug, thiserror::Error)]
19 pub enum ConnectionError {
20 #[error("connection error message {0}")]
21 ErrorMessage(#[from] ErrorMessage),
22 #[error("connection should close")]
23 Shutdown,
24 #[error("internal error {0}")]
25 InternalError(#[from] Error),
26 }
27
28 pub struct RawConnection {
29 pub task: JoinHandle<()>,
30 }
31
32 pub struct InstanceConnection {
33 pub instance: InstanceMeta,
34 pub task: JoinHandle<()>,
35 }
36
37 /// Represents a connection which hasn't finished the handshake.
38 pub struct UnestablishedConnection {
39 pub socket: WebSocketStream<TcpStream>,
40 }
41
42 #[derive(Default)]
43 pub struct Connections {
44 pub connections: Vec<RawConnection>,
45 pub instance_connections: HashMap<Instance, InstanceConnection>,
46 }
47
48 #[derive(Debug, thiserror::Error)]
49 #[error("handler did not handle")]
50 pub struct HandlerUnhandled;
51
52 pub trait MessageHandling<A, M, R> {
53 fn message_type() -> &'static str;
54 }
55
56 impl<T1, F, M, R> MessageHandling<(T1,), M, R> for F
57 where
58 F: FnOnce(T1) -> R,
59 T1: Serialize + DeserializeOwned,
60 {
61 fn message_type() -> &'static str {
62 type_name::<T1>()
63 }
64 }
65
66 impl<T1, T2, F, M, R> MessageHandling<(T1, T2), M, R> for F
67 where
68 F: FnOnce(T1, T2) -> R,
69 T1: Serialize + DeserializeOwned,
70 {
71 fn message_type() -> &'static str {
72 type_name::<T1>()
73 }
74 }
75
76 impl<T1, T2, T3, F, M, R> MessageHandling<(T1, T2, T3), M, R> for F
77 where
78 F: FnOnce(T1, T2, T3) -> R,
79 T1: Serialize + DeserializeOwned,
80 {
81 fn message_type() -> &'static str {
82 type_name::<T1>()
83 }
84 }
85
86 impl<T1, T2, T3, T4, F, M, R> MessageHandling<(T1, T2, T3, T4), M, R> for F
87 where
88 F: FnOnce(T1, T2, T3, T4) -> R,
89 T1: Serialize + DeserializeOwned,
90 {
91 fn message_type() -> &'static str {
92 type_name::<T1>()
93 }
94 }
95