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

ambee/giterated

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

Fixed imports!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ef0e853

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