From: Ian Jackson Date: Sun, 31 Jan 2021 00:37:38 +0000 (+0000) Subject: daemon: use macro for all api routes X-Git-Tag: otter-0.4.0~616 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8821ace7fe9cc0a443f93723a4c1eb25b042709f;p=otter.git daemon: use macro for all api routes Signed-off-by: Ian Jackson --- diff --git a/daemon/api.rs b/daemon/api.rs index def92c25..82ffe356 100644 --- a/daemon/api.rs +++ b/daemon/api.rs @@ -173,13 +173,12 @@ fn api_piece_op(form : Json>) "" } -macro_rules! api_route { - { $fn:ident, $path:expr, - struct $form:ident $body:tt +macro_rules! api_route_core { + { $fn:ident, $path:expr, $form:ident, $formdef:item, $( $impl:tt )* } => { #[derive(Debug,Serialize,Deserialize)] - struct $form $body + $formdef #[post($path, format="json", data="
")] #[throws(OER)] @@ -194,6 +193,29 @@ macro_rules! api_route { } } +macro_rules! api_route { + { $fn:ident, $path:expr, + struct $form:ident { $( $body:tt )* } + $( $impl:tt )* + } => { + api_route_core!{ + $fn, $path, $form, + struct $form { $( $body )* }, + $( $impl )* + } + }; + { $fn:ident, $path:expr, + struct $form:ident ( $( $body:tt )* ); + $( $impl:tt )* + } => { + api_route_core!{ + $fn, $path, $form, + struct $form ( $( $body )* );, + $( $impl )* + } + } +} + api_route!{ api_grab, "/_/api/grab", struct ApiPieceGrab { @@ -215,16 +237,10 @@ api_route!{ } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceWrest { -} -#[post("/_/api/wrest", format="json", data="")] -#[throws(OER)] -fn api_wrest(form : Json>) - -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceWrest { +api_route!{ + api_wrest, "/_/api/wrest", + struct ApiPieceWrest { + } #[throws(OnlineError)] fn check_held(&self, _pc: &PieceState, _player: PlayerId) { } @@ -254,16 +270,10 @@ impl ApiPieceOp for ApiPieceWrest { } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceUngrab { -} -#[post("/_/api/ungrab", format="json", data="")] -#[throws(OER)] -fn api_ungrab(form : Json>) - -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceUngrab { +api_route!{ + api_ungrab, "/_/api/ungrab", + struct ApiPieceUngrab { + } #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,player,piece,p,lens, .. } = a; @@ -281,17 +291,11 @@ impl ApiPieceOp for ApiPieceUngrab { } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceSetZ { - z : ZCoord, -} -#[post("/_/api/setz", format="json", data="")] -#[throws(OER)] -fn api_raise(form : Json>) - -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceSetZ { +api_route!{ + api_raise, "/_/api/setz", + struct ApiPieceSetZ { + z : ZCoord, + } #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,piece, .. } = a; @@ -303,14 +307,10 @@ impl ApiPieceOp for ApiPieceSetZ { } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceMove (Pos); -#[post("/_/api/m", format="json", data="")] -#[throws(OER)] -fn api_move(form : Json>) -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceMove { +api_route!{ + api_move, "/_/api/m", + struct ApiPieceMove(Pos); + #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,piece, .. } = a; @@ -330,14 +330,10 @@ impl ApiPieceOp for ApiPieceMove { } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceRotate(CompassAngle); -#[post("/_/api/rotate", format="json", data="")] -#[throws(OER)] -fn api_rotate(form : Json>) -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceRotate { +api_route!{ + api_rotate, "/_/api/rotate", + struct ApiPieceRotate(CompassAngle); + #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,player,piece,p,lens, .. } = a; @@ -351,14 +347,10 @@ impl ApiPieceOp for ApiPieceRotate { } } -#[derive(Debug,Serialize,Deserialize)] -struct ApiPiecePin (bool); -#[post("/_/api/pin", format="json", data="")] -#[throws(OER)] -fn api_pin(form : Json>) -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPiecePin { +api_route!{ + api_pin, "/_/api/pin", + struct ApiPiecePin (bool); + #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,player,piece,p,lens, .. } = a; @@ -377,14 +369,12 @@ impl ApiPieceOp for ApiPiecePin { const DEFKEY_FLIP : UoKey = 'f'; -#[derive(Debug,Serialize,Deserialize)] -struct ApiPieceUo { opname: String, wrc: WhatResponseToClientOp } -#[post("/_/api/k", format="json", data="")] -#[throws(OER)] -fn api_uo(form : Json>) -> impl response::Responder<'static> { - api_piece_op(form)? -} -impl ApiPieceOp for ApiPieceUo { +api_route!{ + api_uo, "/_/api/k", + struct ApiPieceUo { + opname: String, + wrc: WhatResponseToClientOp, + } #[throws(ApiPieceOpError)] fn op(&self, a: ApiPieceOpArgs) -> PieceUpdateFromOp { let ApiPieceOpArgs { gs,player,piece,p,lens, .. } = a;