From: Felix Date: Wed, 15 Jan 2020 15:48:21 +0000 (+0100) Subject: try to simplify code with higher order functions X-Git-Url: http://these/git/readmes/%7B%60https:/%22https:/hacktivis.me/static/gitweb.js?a=commitdiff_plain;h=19afdf993e55e42ae5f86882fe04ac108f71a6d6;p=lemmy.git try to simplify code with higher order functions --- 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)) }