chiark / gitweb /
daemon: wip macro for api routes
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 31 Jan 2021 00:11:10 +0000 (00:11 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 31 Jan 2021 00:40:56 +0000 (00:40 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/api.rs

index eea0cdce8a79772d957ab881fa30f3b42414392c..def92c2591a85558e793b7fdc8d2db011e3d7a08 100644 (file)
@@ -173,16 +173,31 @@ fn api_piece_op<O: ApiPieceOp>(form : Json<ApiPiece<O>>)
   ""
 }
 
-#[derive(Debug,Serialize,Deserialize)]
-struct ApiPieceGrab {
-}
-#[post("/_/api/grab", format="json", data="<form>")]
-#[throws(OER)]
-fn api_grab(form : Json<ApiPiece<ApiPieceGrab>>)
-            -> impl response::Responder<'static> {
-  api_piece_op(form)?
+macro_rules! api_route {
+  { $fn:ident, $path:expr,
+    struct $form:ident $body:tt
+    $( $impl:tt )*
+  } => {
+    #[derive(Debug,Serialize,Deserialize)]
+    struct $form $body
+
+    #[post($path, format="json", data="<form>")]
+    #[throws(OER)]
+    fn $fn(form : Json<ApiPiece<$form>>)
+           -> impl response::Responder<'static> {
+      api_piece_op(form)?
+    }
+
+    impl ApiPieceOp for $form {
+      $( $impl )*
+    }
+  }
 }
-impl ApiPieceOp for ApiPieceGrab {
+
+api_route!{
+  api_grab, "/_/api/grab",
+  struct ApiPieceGrab {
+  }
   #[throws(ApiPieceOpError)]
   fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp {
     let ApiPieceOpArgs { gs,player,piece,p,lens, .. } = a;