routes.rs 1.69 KiB
use std::net::SocketAddr;
use super::methods::Method;
pub trait ResponseBody {
fn get_data(&self) -> Vec<u8>;
}
pub struct RoutInfo {
name: Option<&'static str>,
method: Method,
path: &'static str,
handler: fn(Request, Data) -> Outcome<Response, Status, Data>,
format: Option<MediaType>,
rank: Option<isize>,
}
pub struct Route<'a> {
name: Option<&'static str>,
method: Method,
uri: Uri<'a>,
handler: fn(Request, Data) -> Outcome<Response, Status, Data>,
rank: isize,
format: Option<MediaType>,
}
impl Route<'_> {
pub fn from(routeinfo: RoutInfo) -> Self {
let rank = routeinfo.rank.unwrap_or(0);
Route {
name: routeinfo.name,
method: routeinfo.method,
uri: routeinfo.path,
handler: routeinfo.handler,
rank,
format: routeinfo.format,
}
}
}
pub struct Request<'a> {
pub uri: Uri<'a>,
pub headers: HeaderMap,
pub method: Method,
// pub connection: ConnectionMeta,
}
struct ConnectionMeta {
remote: Option<SocketAddr>,
// certificates
}
type HeaderMap = Vec<String>;
type Uri<'a> = &'a str;
pub enum Outcome<S, E, F> {
Success(S),
Failure(E),
Forward(F),
}
enum MediaType {
Json,
Plain,
Html,
}
#[derive(Debug)]
pub struct Status {
pub code: u16,
}
pub struct Body {
pub size: Option<usize>,
pub body: Vec<u8>,
}
impl ResponseBody for Body {
fn get_data(&self) -> Vec<u8> {
self.body.clone()
}
}
pub struct Response {
pub headers: HeaderMap,
pub status: Option<Status>,
pub body: Box<dyn ResponseBody>,
}
pub struct Data {
pub buffer: Vec<u8>,
pub is_complete: bool,
}