From 76f6a6b67bab113b6b39b82790313d27ce141280 Mon Sep 17 00:00:00 2001 From: "Philip (a-0)" <@ph:a-0.me> Date: Sun, 21 Jan 2024 12:25:11 +0100 Subject: [PATCH] rustfmt --- ubisync/src/api/v0/app.rs | 49 ++++++++++++++++++++++----- ubisync/src/api/v0/element.rs | 12 ++++--- ubisync/src/api/v0/mod.rs | 5 ++- ubisync/src/comm/message_processor.rs | 4 +-- ubisync/src/state/api_state.rs | 16 ++++++--- ubisync/src/state/comm_state.rs | 5 ++- ubisync/src/state/queries/apps.rs | 40 ++++++++++++++-------- ubisync/src/state/queries/elements.rs | 24 ++++++++----- ubisync/src/state/queries/pots.rs | 5 +-- ubisync/tests/api.rs | 20 ++++++----- 10 files changed, 120 insertions(+), 60 deletions(-) diff --git a/ubisync/src/api/v0/app.rs b/ubisync/src/api/v0/app.rs index f5bcdda..845d2c6 100644 --- a/ubisync/src/api/v0/app.rs +++ b/ubisync/src/api/v0/app.rs @@ -13,7 +13,14 @@ use jsonwebtoken::{decode, Header}; use serde::{Deserialize, Serialize}; use serde_with::chrono::{DateTime, Utc}; use tracing::{debug, error, warn}; -use ubisync_lib::{api::app::{AppRegisterRequest, AppRegisterResponse, AppSetDefaultPotRequest, AppSetDefaultPotResponse, AppCreatePotRequest, AppCreatePotResponse, AppGetDefaultPotRequest, AppGetDefaultPotResponse}, types::PotId}; +use ubisync_lib::{ + api::app::{ + AppCreatePotRequest, AppCreatePotResponse, AppGetDefaultPotRequest, + AppGetDefaultPotResponse, AppRegisterRequest, AppRegisterResponse, AppSetDefaultPotRequest, + AppSetDefaultPotResponse, + }, + types::PotId, +}; use uuid::Uuid; use crate::state::ApiState; @@ -112,7 +119,13 @@ pub(super) async fn set_default_pot( Json(body): Json, ) -> Response { match s.set_app_default_pot(&app_id.0, &body.pot_id) { - Ok(_) => (StatusCode::OK, Json { 0: AppSetDefaultPotResponse }).into_response(), + Ok(_) => ( + StatusCode::OK, + Json { + 0: AppSetDefaultPotResponse, + }, + ) + .into_response(), Err(e) => { debug!("Could not set default pot: {}", e); StatusCode::INTERNAL_SERVER_ERROR.into_response() @@ -126,7 +139,13 @@ pub(super) async fn get_default_pot( Json(_): Json, ) -> Response { match s.get_default_pot(&app_id.0) { - Ok(p) => (StatusCode::OK, Json {0: AppGetDefaultPotResponse { pot: p }}).into_response(), + Ok(p) => ( + StatusCode::OK, + Json { + 0: AppGetDefaultPotResponse { pot: p }, + }, + ) + .into_response(), Err(e) => { warn!("No default pot found: {}", e); StatusCode::NOT_FOUND.into_response() @@ -143,19 +162,25 @@ pub(super) async fn create_pot( Ok(a) => a, Err(e) => { debug!("Failed to fetch app: {}", e); - return StatusCode::INTERNAL_SERVER_ERROR.into_response() + return StatusCode::INTERNAL_SERVER_ERROR.into_response(); } }; let inferred_app_type = match body.app_type { Some(t) => t, - None => app.app_type + None => app.app_type, }; match s.create_pot(&app_id.0, &inferred_app_type) { Ok(id) => { // If this is the first pot for this app, set it as default if app.default_pot.is_none() { match s.set_app_default_pot(&app_id.0, &id) { - Ok(_) => (StatusCode::OK, Json { 0: AppCreatePotResponse {pot_id: id} }).into_response(), + Ok(_) => ( + StatusCode::OK, + Json { + 0: AppCreatePotResponse { pot_id: id }, + }, + ) + .into_response(), Err(e) => { debug!("Failed to set default pot: {}", e); StatusCode::INTERNAL_SERVER_ERROR.into_response() @@ -164,12 +189,18 @@ pub(super) async fn create_pot( } // Otherwise, just return else { - (StatusCode::OK, Json { 0: AppCreatePotResponse {pot_id: id} }).into_response() + ( + StatusCode::OK, + Json { + 0: AppCreatePotResponse { pot_id: id }, + }, + ) + .into_response() } - }, + } Err(e) => { debug!("Could not create pot: {}", e); StatusCode::INTERNAL_SERVER_ERROR.into_response() } } -} \ No newline at end of file +} diff --git a/ubisync/src/api/v0/element.rs b/ubisync/src/api/v0/element.rs index d289bf4..ff664b6 100644 --- a/ubisync/src/api/v0/element.rs +++ b/ubisync/src/api/v0/element.rs @@ -19,7 +19,11 @@ use ubisync_lib::{ use super::app::AppId; -pub(super) async fn get(Path(id): Path, app: Extension, s: Extension>) -> Response { +pub(super) async fn get( + Path(id): Path, + app: Extension, + s: Extension>, +) -> Response { let element = s.get_element(&id, &app); match element { Ok(el) => ( @@ -32,7 +36,7 @@ pub(super) async fn get(Path(id): Path, app: Extension, s: Ext Err(e) => { warn!("Could not get element: {}", e); StatusCode::NOT_FOUND.into_response() - }, + } } } @@ -47,8 +51,8 @@ pub(super) async fn create( Ok(p) => p.id, Err(e) => { warn!("Element create request did not provide pot id, and no default pot for requesting app was found: {}", e); - return StatusCode::INTERNAL_SERVER_ERROR.into_response() - }, + return StatusCode::INTERNAL_SERVER_ERROR.into_response(); + } }, }; diff --git a/ubisync/src/api/v0/mod.rs b/ubisync/src/api/v0/mod.rs index b680948..f868897 100644 --- a/ubisync/src/api/v0/mod.rs +++ b/ubisync/src/api/v0/mod.rs @@ -14,7 +14,10 @@ pub fn get_router(state: ApiState) -> Router { Router::new() // authenticated routes .route("/app/pot", put(app::create_pot)) - .route("/app/pot/default", get(app::get_default_pot).post(app::set_default_pot)) + .route( + "/app/pot/default", + get(app::get_default_pot).post(app::set_default_pot), + ) .route("/element", put(element::create)) .route( "/element/:id", diff --git a/ubisync/src/comm/message_processor.rs b/ubisync/src/comm/message_processor.rs index e07af95..a8e7bf6 100644 --- a/ubisync/src/comm/message_processor.rs +++ b/ubisync/src/comm/message_processor.rs @@ -12,7 +12,7 @@ pub fn handle(state: &CommState, peer: &PeerId, message: Message) { MessageContent::Hello { peer_name } => { state.set_peer(peer, peer_name).expect("State failed"); } - MessageContent::CreateElement { id, content , pot} => { + MessageContent::CreateElement { id, content, pot } => { state .add_received_element(id, content, message.id(), pot) .expect("State failed"); @@ -24,7 +24,7 @@ pub fn handle(state: &CommState, peer: &PeerId, message: Message) { } MessageContent::RemoveElement { id } => { state.remove_element(id).expect("State failed"); - }, + } MessageContent::AddPot { id, app_type } => { state.add_pot(id, app_type).expect("State failed"); //TODO: remove when setting default pot properly is possible diff --git a/ubisync/src/state/api_state.rs b/ubisync/src/state/api_state.rs index 5f5804b..06c65e7 100644 --- a/ubisync/src/state/api_state.rs +++ b/ubisync/src/state/api_state.rs @@ -7,7 +7,7 @@ use serde_with::chrono::Utc; use tracing::debug; use ubisync_lib::{ messages::MessageContent, - types::{Element, ElementContent, ElementId, PotId, Pot}, + types::{Element, ElementContent, ElementId, Pot, PotId}, }; use crate::{ @@ -82,8 +82,7 @@ impl ApiState { debug!("Wrote element content {{{}}}", &id.to_string()); Ok(()) - } - else { + } else { Err(Error::msg( "Element does not exist or app does not have access to it", )) @@ -109,7 +108,10 @@ impl ApiState { let pot_id = PotId::new(); queries::apps::create_pot(self.db(), &pot_id, app_id, app_type)?; - self.state.send_to_peers(MessageContent::AddPot { id: pot_id.to_owned(), app_type: app_type.to_string() }); + self.state.send_to_peers(MessageContent::AddPot { + id: pot_id.to_owned(), + app_type: app_type.to_string(), + }); Ok(pot_id) } @@ -191,7 +193,11 @@ mod tests { .create_element(&ElementContent::Text("Test-text".to_string()), &pot_id) .unwrap(); state - .write_element_content(&id, &app_id, &ElementContent::Text("Test-text 2".to_string())) + .write_element_content( + &id, + &app_id, + &ElementContent::Text("Test-text 2".to_string()), + ) .unwrap(); let el = state.get_element(&id, &app_id).unwrap(); assert_eq!( diff --git a/ubisync/src/state/comm_state.rs b/ubisync/src/state/comm_state.rs index e63a798..cd0b2e7 100644 --- a/ubisync/src/state/comm_state.rs +++ b/ubisync/src/state/comm_state.rs @@ -92,9 +92,8 @@ impl CommState { } } Ok(()) - } - else { - Err(Error::msg("Could not fetch list of all apps")) + } else { + Err(Error::msg("Could not fetch list of all apps")) } } diff --git a/ubisync/src/state/queries/apps.rs b/ubisync/src/state/queries/apps.rs index b12f0f8..8d61197 100644 --- a/ubisync/src/state/queries/apps.rs +++ b/ubisync/src/state/queries/apps.rs @@ -71,7 +71,7 @@ pub fn get_all_ids(db: &DbInstance) -> anyhow::Result> { BTreeMap::new(), ScriptMutability::Immutable, ); - + match result { Ok(named_rows) => Ok(named_rows .rows @@ -79,8 +79,7 @@ pub fn get_all_ids(db: &DbInstance) -> anyhow::Result> { .filter_map(|row| { if let [DataValue::Str(app_id)] = row.as_slice() { serde_json::from_str::(app_id).ok() - } - else { + } else { None } }) @@ -138,11 +137,17 @@ pub fn set_default_pot(db: &DbInstance, id: &AppId, pot: &PotId) -> anyhow::Resu ]; match run_query!( - &db, ":update apps {id => default_pot}", + &db, + ":update apps {id => default_pot}", params.clone(), ScriptMutability::Mutable ) { - Ok(_) => match run_query!(&db, ":put pot_memberships {pot_id = default_pot, app_id = id}", params, ScriptMutability::Mutable) { + Ok(_) => match run_query!( + &db, + ":put pot_memberships {pot_id = default_pot, app_id = id}", + params, + ScriptMutability::Mutable + ) { Ok(_) => Ok(()), Err(report) => bail!(report), }, @@ -165,7 +170,7 @@ pub fn get_default_pot(db: &DbInstance, app: &AppId) -> anyhow::Result { params.clone(), ScriptMutability::Immutable, ); - + match result { Ok(rows) => { if let Some(firstrow) = rows.rows.first() { @@ -206,22 +211,26 @@ pub fn create_pot( match run_query!( &db, - ":insert pots {id = pot_id => app_type}" - , + ":insert pots {id = pot_id => app_type}", params.clone(), ScriptMutability::Mutable ) { - Ok(_) => match run_query!(&db, ":insert pot_memberships {pot_id => app_id}", params, ScriptMutability::Mutable) { + Ok(_) => match run_query!( + &db, + ":insert pot_memberships {pot_id => app_id}", + params, + ScriptMutability::Mutable + ) { Ok(_) => Ok(()), Err(e) => { warn!("{:?}", e); bail!(e) - }, + } }, Err(e) => { warn!("{:?}", e); bail!(e) - }, + } } } @@ -229,10 +238,13 @@ pub fn create_pot( mod tests { use cozo::DbInstance; use serde_with::chrono::Utc; - use tracing::{Level, debug}; + use tracing::{debug, Level}; use ubisync_lib::types::PotId; - use crate::{state::{schema, queries::pots}, api::v0::app::AppId}; + use crate::{ + api::v0::app::AppId, + state::{queries::pots, schema}, + }; #[test] pub fn default_pot() { @@ -251,4 +263,4 @@ mod tests { debug!("Result: {:?}", super::get_default_pot(&db, &app_id)); } -} \ No newline at end of file +} diff --git a/ubisync/src/state/queries/elements.rs b/ubisync/src/state/queries/elements.rs index 7b98fea..dcd3988 100644 --- a/ubisync/src/state/queries/elements.rs +++ b/ubisync/src/state/queries/elements.rs @@ -6,11 +6,12 @@ use serde_json::Value; use tracing::{debug, error}; use crate::{ + api::v0::app::AppId, run_query, - state::{Element, ElementContent, ElementId}, api::v0::app::AppId, + state::{Element, ElementContent, ElementId}, }; -use ubisync_lib::types::{MessageId, Tag, PotId}; +use ubisync_lib::types::{MessageId, PotId, Tag}; pub fn add( db: &DbInstance, @@ -140,17 +141,22 @@ pub fn get_app_access(db: &DbInstance, id: &ElementId, app: &AppId) -> anyhow::R "id".to_string(), DataValue::Str(serde_json::to_string(&id)?.into()), ); - params.insert("app_id".to_string(), DataValue::Str(serde_json::to_string(&app)?.into())); + params.insert( + "app_id".to_string(), + DataValue::Str(serde_json::to_string(&app)?.into()), + ); - let result = db.run_script(" + let result = db.run_script( + " memberships[pot_id] := *pot_memberships{pot_id, app_id} ?[id] := memberships[pot_id], *elements{id, pot: pot_id} - ", params.clone(), cozo::ScriptMutability::Immutable); - + ", + params.clone(), + cozo::ScriptMutability::Immutable, + ); + match result { - Ok(named_rows) => { - Ok(named_rows.rows.len() > 0) - }, + Ok(named_rows) => Ok(named_rows.rows.len() > 0), Err(report) => bail!(report), } } diff --git a/ubisync/src/state/queries/pots.rs b/ubisync/src/state/queries/pots.rs index 3089b07..ff64f5a 100644 --- a/ubisync/src/state/queries/pots.rs +++ b/ubisync/src/state/queries/pots.rs @@ -4,10 +4,7 @@ use anyhow::Error; use cozo::{DataValue, DbInstance, ScriptMutability}; use ubisync_lib::types::{Pot, PotId}; -use crate::{ - api::v0::app::AppId, - run_query, -}; +use crate::{api::v0::app::AppId, run_query}; pub fn add(db: &DbInstance, id: &PotId, app_type: &str) -> anyhow::Result<()> { let params = vec![ diff --git a/ubisync/tests/api.rs b/ubisync/tests/api.rs index c4540d8..6ec0206 100644 --- a/ubisync/tests/api.rs +++ b/ubisync/tests/api.rs @@ -23,15 +23,17 @@ async fn two_nodes_element_creation() { ubi1.add_peer_from_id(ubi2.get_destination().unwrap().into()) .unwrap(); - let api_client1 = UbisyncClient::init("localhost", 9981, None, "App", "Long desc", "test-app-type") - .await - .unwrap() - .create_default_pot() - .await - .unwrap(); - let api_client2 = UbisyncClient::init("localhost", 9982, None, "App", "Long desc", "test-app-type") - .await - .unwrap(); + let api_client1 = + UbisyncClient::init("localhost", 9981, None, "App", "Long desc", "test-app-type") + .await + .unwrap() + .create_default_pot() + .await + .unwrap(); + let api_client2 = + UbisyncClient::init("localhost", 9982, None, "App", "Long desc", "test-app-type") + .await + .unwrap(); tokio::time::sleep(Duration::from_millis(5000)).await;