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