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

ambee/giterated-api

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

Error handling fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨5eaea71

⁨src/lib.rs⁩ - ⁨1461⁩ bytes
Raw
1 pub use giterated_models as model;
2
3 use std::{fmt::Debug, net::SocketAddr};
4
5 use deadpool::managed::{BuildError, Pool};
6 use giterated_models::instance::Instance;
7 use pool::GiteratedConnectionPool;
8 use tokio::net::TcpStream;
9 use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
10
11 pub mod daemon_backend;
12 mod handshake;
13 mod pool;
14
15 type Socket = WebSocketStream<MaybeTlsStream<TcpStream>>;
16
17 #[macro_use]
18 extern crate tracing;
19
20 #[derive(Clone)]
21 pub struct DaemonConnectionPool(Pool<GiteratedConnectionPool>);
22
23 impl Debug for DaemonConnectionPool {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 f.debug_tuple("DaemonConnectionPool").finish()
26 }
27 }
28
29 impl DaemonConnectionPool {
30 pub fn connect(instance: impl ToOwned<Owned = Instance>) -> Result<Self, BuildError> {
31 let instance = instance.to_owned();
32 Ok(Self(
33 Pool::builder(GiteratedConnectionPool {
34 socket_addr: None,
35 target_instance: instance.to_owned(),
36 })
37 .build()?,
38 ))
39 }
40
41 pub fn connect_other(
42 instance_identity: impl ToOwned<Owned = Instance>,
43 connection_addr: SocketAddr,
44 ) -> Result<Self, BuildError> {
45 Ok(Self(
46 Pool::builder(GiteratedConnectionPool {
47 target_instance: instance_identity.to_owned(),
48 socket_addr: Some(connection_addr),
49 })
50 .build()?,
51 ))
52 }
53 }
54