--- /dev/null
+use serde_json::Value;
+
+pub fn handle_json(root: Value) -> Result<String, String> {
+ if let Some(Value::String(type_str)) = root.get("type") {
+ match type_str.as_str() {
+ "pairing_request" => handle_pairing_request(root),
+ "check_pairing" => handle_check_pairing(root),
+ "place_token" => handle_place_token(root),
+ "whose_turn" => handle_whose_turn(root),
+ "disconnect" => handle_disconnect(root),
+ "request_board_state" => handle_request_board_state(root),
+ "game_state" => handle_game_state(root),
+ _ => {
+ Err("{\"type\":\"invalid_type\"}".into())
+ }
+ }
+ } else {
+ Err("{\"type\":\"invalid_json\"}".into())
+ }
+}
+
+fn handle_pairing_request(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_check_pairing(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_place_token(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_whose_turn(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_disconnect(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_request_board_state(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_game_state(root: Value) -> Result<String, String> {
+ Err("{\"type\":\"unimplemented\"}".into())
+}
-fn main() {
- println!("Hello, world!");
+mod json_handlers;
+
+use serde_json::Value;
+use warp::Filter;
+
+#[tokio::main]
+async fn main() {
+ let route = warp::body::content_length_limit(1024 * 32)
+ .and(warp::body::json())
+ .map(|json_value: Value| {
+ let result = json_handlers::handle_json(json_value);
+ if let Ok(result_str) = result {
+ result_str
+ } else if let Err(error_str) = result {
+ error_str
+ } else {
+ unreachable!()
+ }
+ });
+
+ warp::serve(route)
+ .run(([0,0,0,0], 1237))
+ .await;
}
"status": "opponent_disconnected", // or "unknown_id"
}
```
+
+8. Failure Response
+
+When request "type" is not handled by the back-end, it returns with
+"invalid\_type".
+```
+ {
+ "type": "invalid_type"
+ }
+```
+
+When JSON is missing a required value, it returns with "invalid\_json".
+```
+ {
+ "type": "invalid_json"
+ }
+```
+
+When the back-end hasn't yet implemented handling a specific type, it returns
+"unimplemented".
+```
+ {
+ "type": "unimplemented"
+ }
+```