]> Untitled Git - lemmy.git/commitdiff
try to simplify code with higher order functions
authorFelix <me@nutomic.com>
Wed, 15 Jan 2020 15:48:21 +0000 (16:48 +0100)
committerFelix <me@nutomic.com>
Sat, 18 Jan 2020 13:23:44 +0000 (14:23 +0100)
server/src/routes/api.rs

index edf1ead6ad2f7bf3a55443a65286acbcec4b4e0c..11919b8cc84bd5df4a97119a4f98289787af9156 100644 (file)
@@ -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::<ListCommunities, ListCommunitiesResponse>(UserOperation::ListCommunities, info, db)
-      }),
+      web::get().to(
+        route::<ListCommunities, ListCommunitiesResponse>(UserOperation::ListCommunities)
+      ),
     )
     .route(
       "/api/v1/get_community",
-      web::get().to(|info, db| {
-        route::<GetCommunity, GetCommunityResponse>(UserOperation::GetCommunity, info, db)
-      }),
+      web::get().to(route::<GetCommunity, GetCommunityResponse>(
+        UserOperation::GetCommunity,
+      )),
     );
 }
 
@@ -46,11 +46,9 @@ where
   Ok(HttpResponse::Ok().json(response?))
 }
 
-async fn route<Data, Response>(
+fn route<Data, Response>(
   op: UserOperation,
-  info: web::Query<Data>,
-  db: DbParam,
-) -> Result<HttpResponse, Error>
+) -> Box<(dyn Fn(web::Query<Data>, DbParam) -> Result<HttpResponse, Error> + '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::<Data, Response>(op, info.0, db)
+  Box::new(|data, db| perform::<Data, Response>(op, data.0, db))
 }