diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-27 19:55:10 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-27 19:58:45 -0500 |
commit | 21b7b57336cd3bf7bd328ada38c6069a48504f85 (patch) | |
tree | c0d47337f584403febcfc9ec365ea1ea810e782e /webapp | |
parent | 3418ebf4db2375e0dfe2343da57d674e1f4fd57f (diff) |
`cargo fmt`
Diffstat (limited to 'webapp')
-rw-r--r-- | webapp/src/components/app_context_provider.rs | 86 | ||||
-rw-r--r-- | webapp/src/components/bidding.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/bidding_box.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/bidding_table.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/card.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/hand.rs | 2 | ||||
-rw-r--r-- | webapp/src/components/table.rs | 6 | ||||
-rw-r--r-- | webapp/src/routing.rs | 3 | ||||
-rw-r--r-- | webapp/src/services.rs | 28 | ||||
-rw-r--r-- | webapp/src/utils.rs | 10 |
10 files changed, 78 insertions, 65 deletions
diff --git a/webapp/src/components/app_context_provider.rs b/webapp/src/components/app_context_provider.rs index 6298c2b..081026c 100644 --- a/webapp/src/components/app_context_provider.rs +++ b/webapp/src/components/app_context_provider.rs @@ -1,14 +1,14 @@ -use log::error; use crate::{routing::Route, utils::ok_json}; +use anyhow::Context; use gloo_net::http::Request; +use log::error; use log::info; use protocol::UserInfo; -use uuid::Uuid; use std::future::Future; +use uuid::Uuid; use wasm_bindgen_futures::spawn_local; use yew::prelude::*; use yew_router::prelude::*; -use anyhow::Context; #[derive(Properties, Clone, PartialEq, Debug)] pub struct ErrorInfoProperties { @@ -35,7 +35,7 @@ impl AppContext { let error = self.state.error.clone(); spawn_local(async move { if let Err(err) = f.await { - error!("Error occurred: {err:?}"); + error!("Error occurred: {err:?}"); error.set(Some(ErrorInfoProperties { message: format!("{err:?}"), })); @@ -58,40 +58,41 @@ impl AppContext { } pub fn create_table(&self) { - let user = self.state.user.clone(); - let history = self.history.clone(); - self.spawn_async(async move { - let response = Request::post("/api/table").send().await?; - let table_id: Uuid = ok_json(response).await.context("creating table")?; + let user = self.state.user.clone(); + let history = self.history.clone(); + self.spawn_async(async move { + let response = Request::post("/api/table").send().await?; + let table_id: Uuid = + ok_json(response).await.context("creating table")?; info!("Created table {table_id}"); - if let Some(user_info) = user.as_ref() { - user.set(Some(UserInfo { - table: Some(protocol::Table { id: table_id }), - ..(user_info.clone()) - })); - } - history.push(Route::Home); - Ok(()) - }); + if let Some(user_info) = user.as_ref() { + user.set(Some(UserInfo { + table: Some(protocol::Table { id: table_id }), + ..(user_info.clone()) + })); + } + history.push(Route::Home); + Ok(()) + }); } pub fn leave_table(&self) { - let user = self.state.user.clone(); - let history = self.history.clone(); - self.spawn_async(async move { - let response = Request::delete("/api/table").send().await?; - if !response.ok() { - anyhow::bail!("error while leaving table"); - } - if let Some(user_info) = user.as_ref() { - user.set(Some(UserInfo { - table: None, - ..(user_info.clone()) - })); - } - history.push(Route::Home); - Ok(()) - }); + let user = self.state.user.clone(); + let history = self.history.clone(); + self.spawn_async(async move { + let response = Request::delete("/api/table").send().await?; + if !response.ok() { + anyhow::bail!("error while leaving table"); + } + if let Some(user_info) = user.as_ref() { + user.set(Some(UserInfo { + table: None, + ..(user_info.clone()) + })); + } + history.push(Route::Home); + Ok(()) + }); } } @@ -101,7 +102,10 @@ pub struct Props { } async fn initialize_user_info() -> Result<Option<UserInfo>, anyhow::Error> { - let response = Request::get("/api/user/info").send().await.context("fetching user_info")?; + let response = Request::get("/api/user/info") + .send() + .await + .context("fetching user_info")?; if response.status() == 401 { web_sys::window() .unwrap() @@ -113,7 +117,7 @@ async fn initialize_user_info() -> Result<Option<UserInfo>, anyhow::Error> { } pub fn use_app_context() -> AppContext { - let state : AppState = use_context::<AppState>().unwrap(); + let state: AppState = use_context::<AppState>().unwrap(); let history = use_history().unwrap(); AppContext { state, history } @@ -136,7 +140,10 @@ pub fn app_context_provider(props: &Props) -> Html { match initialize_user_info().await { Ok(user_info) => user.set(user_info), Err(e) => error.set(Some(ErrorInfoProperties { - message: format!("Could not contact server: {:?}", e), + message: format!( + "Could not contact server: {:?}", + e + ), })), }; }); @@ -155,10 +162,7 @@ pub fn app_context_provider(props: &Props) -> Html { info!("Recomputing state"); info!("User is {:?}", *user); - let state = AppState { - user, - error, - }; + let state = AppState { user, error }; html! { <ContextProvider<AppState> context={state}> diff --git a/webapp/src/components/bidding.rs b/webapp/src/components/bidding.rs index 2c48ca3..9b92930 100644 --- a/webapp/src/components/bidding.rs +++ b/webapp/src/components/bidding.rs @@ -1,6 +1,6 @@ -use protocol::bridge_engine::{self, BiddingResult, Contract, Player}; use crate::components::{BiddingBox, BiddingTable}; use log::error; +use protocol::bridge_engine::{self, BiddingResult, Contract, Player}; use yew::prelude::*; #[derive(PartialEq, Properties, Clone)] diff --git a/webapp/src/components/bidding_box.rs b/webapp/src/components/bidding_box.rs index 0b384ec..0b660ea 100644 --- a/webapp/src/components/bidding_box.rs +++ b/webapp/src/components/bidding_box.rs @@ -1,5 +1,5 @@ -use protocol::bridge_engine::{Bid, Raise}; use crate::components::bid_css_class; +use protocol::bridge_engine::{Bid, Raise}; use yew::prelude::*; #[function_component(BiddingBox)] diff --git a/webapp/src/components/bidding_table.rs b/webapp/src/components/bidding_table.rs index 0f1a824..161f85a 100644 --- a/webapp/src/components/bidding_table.rs +++ b/webapp/src/components/bidding_table.rs @@ -1,5 +1,5 @@ -use protocol::bridge_engine::{Bid, Bidding, Player}; use crate::components::bid_css_class; +use protocol::bridge_engine::{Bid, Bidding, Player}; use yew::prelude::*; #[function_component(BiddingTable)] diff --git a/webapp/src/components/card.rs b/webapp/src/components/card.rs index 48d53e3..82d4fcb 100644 --- a/webapp/src/components/card.rs +++ b/webapp/src/components/card.rs @@ -1,5 +1,5 @@ -use protocol::card; use crate::components::suit_css_class; +use protocol::card; use yew::prelude::*; #[function_component(Card)] diff --git a/webapp/src/components/hand.rs b/webapp/src/components/hand.rs index 7946a5c..7d97ffd 100644 --- a/webapp/src/components/hand.rs +++ b/webapp/src/components/hand.rs @@ -1,5 +1,5 @@ -use protocol::card; use crate::components::card::Card; +use protocol::card; use yew::prelude::*; #[function_component(Hand)] diff --git a/webapp/src/components/table.rs b/webapp/src/components/table.rs index bd49b10..6d35613 100644 --- a/webapp/src/components/table.rs +++ b/webapp/src/components/table.rs @@ -149,9 +149,11 @@ impl Component for OnlineTableInner { async move { services::new_deal(table.clone()).await?; services::get_table_player_view(table).await - }.map(Msg::TableStateUpdated)); + } + .map(Msg::TableStateUpdated), + ); false - }, + } } } diff --git a/webapp/src/routing.rs b/webapp/src/routing.rs index 57d4af0..b1e6e01 100644 --- a/webapp/src/routing.rs +++ b/webapp/src/routing.rs @@ -1,5 +1,5 @@ -use yew_router::prelude::*; use uuid::Uuid; +use yew_router::prelude::*; #[derive(Clone, Routable, PartialEq)] pub enum Route { @@ -8,4 +8,3 @@ pub enum Route { #[at("/table/:id")] Table { id: Uuid }, } - diff --git a/webapp/src/services.rs b/webapp/src/services.rs index 668530f..cd0c649 100644 --- a/webapp/src/services.rs +++ b/webapp/src/services.rs @@ -1,6 +1,10 @@ use anyhow::Context; use gloo_net::http::Request; -use protocol::{bridge_engine::{TableStatePlayerView, Bid}, Table, card::Card}; +use protocol::{ + bridge_engine::{Bid, TableStatePlayerView}, + card::Card, + Table, +}; use crate::utils::ok_json; @@ -16,22 +20,20 @@ pub async fn get_table_player_view( } pub async fn bid(table: Table, bid: Bid) -> Result<(), anyhow::Error> { - let response = - Request::post(&format!("/api/table/{}/bid", table.id)) - .json(&bid)? - .send() - .await - .context("submitting bid")?; + let response = Request::post(&format!("/api/table/{}/bid", table.id)) + .json(&bid)? + .send() + .await + .context("submitting bid")?; ok_json(response).await } pub async fn play(table: Table, card: Card) -> Result<(), anyhow::Error> { - let response = - Request::post(&format!("/api/table/{}/play", table.id)) - .json(&card)? - .send() - .await - .context("submitting play")?; + let response = Request::post(&format!("/api/table/{}/play", table.id)) + .json(&card)? + .send() + .await + .context("submitting play")?; ok_json(response).await } diff --git a/webapp/src/utils.rs b/webapp/src/utils.rs index 1eadf2f..86493eb 100644 --- a/webapp/src/utils.rs +++ b/webapp/src/utils.rs @@ -2,9 +2,15 @@ use gloo_net::http::Response; use serde::de::DeserializeOwned; pub async fn ok_json<T>(response: Response) -> Result<T, anyhow::Error> -where T: DeserializeOwned { +where + T: DeserializeOwned, +{ if !response.ok() { - anyhow::bail!("Request failed: {} {}", response.status(), response.status_text()); + anyhow::bail!( + "Request failed: {} {}", + response.status(), + response.status_text() + ); } Ok(response.json().await?) } |