Desired code: The commented out block compiles and works, however I'd like to move from nested matching style to a cleaner chain of functions
async fn ws_req_resp(msg: String, conn: PgConn) -> Result<String, Box<dyn std::error::Error>>{
let req: WSReq = serde_json::from_str(&msg)?;
match req.request_type{
"upsert_competitions" => {
// let dr = serde_json::from_value(req.data);
// match dr{
// Ok(d) => match upsert_competitions(conn, d).await{
// Ok(x) => serde_json::to_string(&x).map_err(|e| e.into()),
// Err(e) => Err(Box::new(e))
// }
// Err(e) => Err(Box::new(e))
// }
serde_json::from_value(req.data).and_then(|d| async move {
upsert_competitions(conn, d).await}).and_then(|r| serde_json::to_string(&r))
.map_err(|e| e.into())
},
uwotm8 => {
Err(Box::new(InvalidRequestError{req_type: uwotm8.to_string()}))
}
}
}
upsert_competitions signature pub async fn upsert_competitions(conn: PgConn, new: Vec<ApiNewCompetition>) -> Result<Vec<DbCompetition>, diesel::result::Error>
Error:
expected enum `std::result::Result<_, serde_json::error::Error>`
found opaque type `impl core::future::future::Future`
Have tried putting the await in multiple places in chain and none compile. Believe awaiting a future should sit until it's finished, then return the result.
(It might be better for me to return a future from this function; and unwrap outside. However I do not understand why the await in the chain is failing, so clearly I'm lacking understanding...also trying to return a future I run into issues with compiler not knowing Size of return)