Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- enum Method {
- Head,
- Option,
- Get,
- Post,
- Patch,
- Put,
- Delete,
- }
- struct Router;
- impl Router {
- pub fn to<H, P>(&self, method: Method, handler: H) -> &Self
- where
- H: HandlerFactory<P>,
- {
- println!("handle route {:?}", method);
- self
- }
- }
- trait HandlerFactory<P> {
- fn call(&self, _: P) -> String;
- }
- impl<F> HandlerFactory<()> for F
- where
- F: Fn() -> String,
- {
- fn call(&self, _: ()) -> String {
- (self)()
- }
- }
- impl<F> HandlerFactory<(Token,)> for F
- where
- F: Fn(Token) -> String,
- {
- fn call(&self, params: (Token,)) -> String {
- (self)(params.0)
- }
- }
- struct Token {}
- fn public_route() -> String {
- "hello world".into()
- }
- fn private_route(token: Token) -> String {
- "hello private world".into()
- }
- fn main() {
- let router = Router {};
- router
- .to(Method::Get, public_route)
- .to(Method::Post, private_route);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement