Skip to main content

session_rs/
lib.rs

1use std::pin::Pin;
2
3use serde::{Deserialize, Serialize};
4
5pub mod server;
6pub mod session;
7pub mod ws;
8
9pub type Result<T> = std::result::Result<T, Error>;
10pub type BoxFuture<'a, T = Option<(bool, serde_json::Value)>> =
11    Pin<Box<dyn Future<Output = T> + Send + 'a>>;
12pub type MethodHandler = Box<dyn Fn(u32, serde_json::Value) -> BoxFuture<'static> + Send + Sync>;
13
14pub trait Method {
15    const NAME: &'static str;
16    type Request: Serialize + for<'de> Deserialize<'de> + Send + Sync;
17    type Response: Serialize + for<'de> Deserialize<'de>;
18    type Error: Serialize + for<'de> Deserialize<'de>;
19}
20
21pub struct GenericMethod;
22
23impl Method for GenericMethod {
24    const NAME: &'static str = "generic_do_not_use";
25    type Request = serde_json::Value;
26    type Response = serde_json::Value;
27    type Error = serde_json::Value;
28}
29
30#[derive(Debug)]
31pub enum Error {
32    WebSocket(ws::Error),
33    Json(serde_json::Error),
34    Io(std::io::Error),
35    RecvError(tokio::sync::broadcast::error::RecvError),
36}
37
38impl From<ws::Error> for Error {
39    fn from(value: ws::Error) -> Self {
40        Self::WebSocket(value)
41    }
42}
43
44impl From<std::io::Error> for Error {
45    fn from(value: std::io::Error) -> Self {
46        Self::Io(value)
47    }
48}
49
50impl From<serde_json::Error> for Error {
51    fn from(value: serde_json::Error) -> Self {
52        Self::Json(value)
53    }
54}
55
56impl From<tokio::sync::broadcast::error::RecvError> for Error {
57    fn from(value: tokio::sync::broadcast::error::RecvError) -> Self {
58        Self::RecvError(value)
59    }
60}