little_web/
lib.rs

1use std::{process::Output, future::Future, fmt::{Display, Debug}};
2
3use parser::{HttpRequest, HttpResponse};
4use serde::{Serialize, Deserialize};
5
6pub mod core;
7pub mod parser;
8pub mod route;
9pub mod error;
10
11
12#[derive(Serialize, Deserialize, Debug,PartialEq)]
13pub enum Method {
14    POST,
15    GET,
16    OPTION,
17    HEAD,
18    PUT,
19    DELETE
20}
21
22impl Copy for Method {
23    
24}
25
26impl Clone for Method {
27
28    fn clone(&self) -> Self {
29        match self {
30            Self::POST => Self::POST,
31            Self::GET => Self::GET,
32            Self::OPTION => Self::OPTION,
33            Self::HEAD => Self::HEAD,
34            Self::PUT => Self::PUT,
35            Self::DELETE => Self::DELETE,
36        }
37    }
38}
39
40impl From<&str> for Method {
41    fn from(s: &str) -> Self {
42        match s {
43            "POST" => Method::POST,
44            "GET" => Method::GET,
45            _ => Method::PUT
46        }
47    }
48}
49
50
51#[derive(Serialize, Deserialize, Debug)]
52pub enum Protocal {
53    HTTP,
54    HTTPS,
55}
56
57impl From<&str> for Protocal {
58    fn from(s: &str) -> Self {
59        match s {
60            "HTTP" => Protocal::HTTP,
61            "HTTPS" => Protocal::HTTPS,
62            _ => Protocal::HTTP,
63        }
64    }
65}
66
67impl From<String> for Protocal {
68    fn from(s: String) -> Self {
69        if s == String::from("HTTP"){
70            return Protocal::HTTP;
71        }else if  s == String::from("HTTPS"){
72            return Protocal::HTTPS;
73        }else{
74            return Protocal::HTTPS;
75        }
76        
77    }
78}
79
80
81#[derive(Serialize, Deserialize, Debug)]
82pub enum Version {
83    V1_1,
84    UNKONE,
85}
86
87impl From<&str> for Version {
88    fn from(s: &str) -> Self {
89        match s {
90            "1.1" => Version::V1_1,
91            _ => Version::UNKONE
92        }
93    }
94}
95
96impl From<String> for Version {
97    fn from(s: String) -> Self {
98        if s == String::from("1.1") {
99            return Version::V1_1;
100        }else{
101            Version::UNKONE
102        }
103    }
104}
105
106
107// pub trait Handler<Input>: dyn_clone::DynClone + Send + Sync + 'static {
108//     /// The returned type after the call operator is used.
109//     type Output;
110
111//     /// Performs the call operation.
112//     #[must_use]
113//     fn call(&self, input: Input) -> Self::Output;
114// }
115
116// impl<I, T> HandlerExt<I> for T where T: Handler<I> + ?Sized {}
117
118
119// impl<F, I, Fut, O> Handler<I> for F
120// where
121//     I: Send + 'static,
122//     F: Fn(I) -> Fut + ?Sized + Clone + Send + Sync + 'static,
123//     Fut: Future<Output = O> + Send,
124// {
125//     type Output = Fut::Output;
126
127//     fn call(&self, i: I) -> Self::Output {
128//         (self)(i)
129//     }
130// }
131
132// auto trait Handler {
133
134// }
135
136// trait NewTrait: FnOnce(HttpRequest) -> HttpResponse + Clone {}
137
138pub trait Handler<Input>: dyn_clone::DynClone + Send + Sync + 'static {
139    /// The returned type after the call operator is used.
140    type Output;
141
142    /// Performs the call operation.
143    #[must_use]
144    fn call(&self, input: Input) -> Self::Output;
145}
146
147impl<F, I, O> Handler<I> for F
148where
149    I: Send + 'static,
150    F: Fn(I) -> O + ?Sized + Clone + Send + Sync + 'static,
151    O: Send + 'static
152    //Fut: Future<Output = O> + Send,
153{
154    type Output = O;
155
156    fn call(&self, i: I) -> Self::Output {
157        (self)(i)
158    }
159}
160
161
162pub type handler<I=HttpRequest, O = HttpResponse>= Box<dyn Handler<I, Output=O>>;
163//pub type handler = Box<dyn FnOnce(HttpRequest) -> HttpResponse + Send  + 'static>;
164
165impl Clone for handler {
166    fn clone(&self) -> Self {
167        dyn_clone::clone_box(&**self)
168    }
169
170}
171
172impl Display for handler {
173    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174        write!(f,"let me see")
175    }
176}
177
178impl Debug for handler {
179    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180        write!(f,"let me see")
181    }
182}
183
184
185pub type handlers = Vec<handler>;
186