From 19afdf993e55e42ae5f86882fe04ac108f71a6d6 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 15 Jan 2020 16:48:21 +0100 Subject: [PATCH] try to simplify code with higher order functions --- server/src/routes/api.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index edf1ead6..11919b8c 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -16,15 +16,15 @@ pub fn config(cfg: &mut web::ServiceConfig) { // TODO: need to repeat this for every endpoint .route( "/api/v1/list_communities", - web::get().to(|info, db| { - route::(UserOperation::ListCommunities, info, db) - }), + web::get().to( + route::(UserOperation::ListCommunities) + ), ) .route( "/api/v1/get_community", - web::get().to(|info, db| { - route::(UserOperation::GetCommunity, info, db) - }), + web::get().to(route::( + UserOperation::GetCommunity, + )), ); } @@ -46,11 +46,9 @@ where Ok(HttpResponse::Ok().json(response?)) } -async fn route( +fn route( op: UserOperation, - info: web::Query, - db: DbParam, -) -> Result +) -> Box<(dyn Fn(web::Query, DbParam) -> Result + 'static)> where Data: Serialize, Response: Serialize, @@ -58,5 +56,5 @@ where { // TODO: want an implementation like this, where useroperation is passed without explicitly passing the other params // maybe with a higher order functions? (but that would probably have worse performance) - perform::(op, info.0, db) + Box::new(|data, db| perform::(op, data.0, db)) } -- 2.44.1