pub mod error;
pub mod message;
pub mod request;
pub mod response;
use std::fmt;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub use request::Request;
pub use response::Response;
use self::error::ErrorResponse;
#[cfg_attr(feature = "ts", derive(ts_rs::TS), ts(export))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum HttpMethod {
GET,
POST,
PUT,
DELETE,
}
impl HttpMethod {
pub fn as_str(&self) -> &'static str {
match self {
Self::GET => "GET",
Self::POST => "POST",
Self::PUT => "PUT",
Self::DELETE => "DELETE",
}
}
pub fn from_http(method: &axum::http::Method) -> Option<Self> {
use axum::http::Method;
match *method {
Method::GET => Some(Self::GET),
Method::POST => Some(Self::POST),
Method::PUT => Some(Self::PUT),
Method::DELETE => Some(Self::DELETE),
_ => None,
}
}
pub fn has_body(&self) -> bool {
matches!(self, Self::POST | Self::PUT)
}
}
impl fmt::Display for HttpMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[async_trait::async_trait]
pub trait RestRouter: Send + Sync + 'static {
async fn route(&self, req: Request) -> Result<Response, ErrorResponse>;
}
#[async_trait::async_trait]
pub trait SocketRouter<Req, Resp>: Send + Sync + 'static
where
Req: DeserializeOwned + Send + 'static,
Resp: Serialize + Send + 'static,
{
async fn route(&self, msg: Req, connection_id: String) -> Option<Resp>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoSocketRouter;
#[async_trait::async_trait]
impl SocketRouter<serde_json::Value, serde_json::Value> for NoSocketRouter {
async fn route(&self, _msg: serde_json::Value, _connection_id: String) -> Option<serde_json::Value> {
None
}
}