From 1b1d1aed77ba92d3cd6f7247e130362665ff109d Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sat, 28 Dec 2024 13:59:53 +0100 Subject: cargo fmt --- server/src/api_model.rs | 4 +- server/src/main.rs | 147 ++++++++++++++++++++++++++++-------------------- 2 files changed, 87 insertions(+), 64 deletions(-) (limited to 'server') diff --git a/server/src/api_model.rs b/server/src/api_model.rs index c5b1ce3..1c02f6c 100644 --- a/server/src/api_model.rs +++ b/server/src/api_model.rs @@ -1,4 +1,4 @@ -use serde::{Serialize}; +use serde::Serialize; #[derive(Serialize, Copy, Clone)] pub enum ReviewState { @@ -50,7 +50,7 @@ pub struct Reviews { pub struct Project { pub id: u64, pub title: String, - pub description: String, + pub description: String, pub members: Vec, } diff --git a/server/src/main.rs b/server/src/main.rs index 9f7204a..413545b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,13 +1,14 @@ -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; -use futures::{stream::TryStreamExt, future::TryFutureExt}; +use futures::{future::TryFutureExt, stream::TryStreamExt}; -use rocket::{Rocket, Build, futures}; use rocket::fairing::{self, AdHoc}; -use rocket::request::{self, Outcome, Request, FromRequest}; -use rocket::response::{status::NotFound}; +use rocket::request::{self, FromRequest, Outcome, Request}; +use rocket::response::status::NotFound; use rocket::serde::json::Json; -use rocket_db_pools::{sqlx, Database, Connection}; +use rocket::{futures, Build, Rocket}; +use rocket_db_pools::{sqlx, Connection, Database}; mod api_model; @@ -30,7 +31,9 @@ impl<'r> FromRequest<'r> for User { type Error = UserError; async fn from_request(_req: &'r Request<'_>) -> request::Outcome { - Outcome::Success(User{ username: String::from("foo") }) + Outcome::Success(User { + username: String::from("foo"), + }) } } @@ -51,7 +54,7 @@ impl TryFrom for Role { match value { 0 => Ok(Role::Reviewer), 1 => Ok(Role::Watcher), - _ => Err("Invalid role") + _ => Err("Invalid role"), } } } @@ -65,30 +68,36 @@ impl TryFrom for api_model::ReviewState { 1 => Ok(api_model::ReviewState::Open), 2 => Ok(api_model::ReviewState::Dropped), 3 => Ok(api_model::ReviewState::Closed), - _ => Err("Invalid review state") + _ => Err("Invalid review state"), } } } #[get("/projects?&")] -async fn projects<'r>(mut db: Connection, _user: User, limit: Option, offset: Option) -> Json { +async fn projects<'r>( + mut db: Connection, + _user: User, + limit: Option, + offset: Option, +) -> Json { let uw_offset = offset.unwrap_or(0); let uw_limit = limit.unwrap_or(10); let entries = sqlx::query!( "SELECT id,title FROM projects ORDER BY title,id LIMIT ? OFFSET ?", - uw_limit, uw_offset) - .fetch(&mut **db) - .map_ok(|r| api_model::ProjectEntry { - id: r.id, - title: r.title, - }) - .try_collect::>() - .await - .unwrap(); - - let count = sqlx::query!( - "SELECT COUNT(id) AS count FROM projects") + uw_limit, + uw_offset + ) + .fetch(&mut **db) + .map_ok(|r| api_model::ProjectEntry { + id: r.id, + title: r.title, + }) + .try_collect::>() + .await + .unwrap(); + + let count = sqlx::query!("SELECT COUNT(id) AS count FROM projects") .fetch_one(&mut **db) .map_ok(|r| r.count) .await @@ -96,20 +105,22 @@ async fn projects<'r>(mut db: Connection, _user: User, limit: Option, o let u32_count = u32::try_from(count).unwrap(); - Json( - api_model::Projects { - offset: uw_offset, - limit: uw_limit, - total_count: u32_count, - more: uw_offset + uw_limit < u32_count, - projects: entries, - }, - ) + Json(api_model::Projects { + offset: uw_offset, + limit: uw_limit, + total_count: u32_count, + more: uw_offset + uw_limit < u32_count, + projects: entries, + }) } #[get("/project/")] -async fn project<'r>(mut db: Connection, _user: User, projectid: u64) -> Result, NotFound> { - let members = sqlx::query!( +async fn project<'r>( + mut db: Connection, + _user: User, + projectid: u64, +) -> Result, NotFound> { + let members = sqlx::query!( "SELECT id, username, name, active FROM users JOIN project_users ON project_users.user=users.id WHERE project_users.project=?", projectid) .fetch(&mut **db) @@ -125,23 +136,30 @@ async fn project<'r>(mut db: Connection, _user: User, projectid: u64) -> Res let project = sqlx::query!( "SELECT id,title,description FROM projects WHERE id=?", - projectid) - .fetch_one(&mut **db) - .map_ok(|r| api_model::Project { - id: r.id, - title: r.title, - description: r.description, - members: members, - }) - .await - .map_err(|e| NotFound(e.to_string())) - .unwrap(); + projectid + ) + .fetch_one(&mut **db) + .map_ok(|r| api_model::Project { + id: r.id, + title: r.title, + description: r.description, + members: members, + }) + .await + .map_err(|e| NotFound(e.to_string())) + .unwrap(); Ok(Json(project)) } #[get("/reviews/?&")] -async fn reviews<'r>(mut db: Connection, _user: User, projectid: u64, limit: Option, offset: Option) -> Json { +async fn reviews<'r>( + mut db: Connection, + _user: User, + projectid: u64, + limit: Option, + offset: Option, +) -> Json { let uw_offset = offset.unwrap_or(0); let uw_limit = limit.unwrap_or(10); let entries = sqlx::query!( @@ -166,27 +184,31 @@ async fn reviews<'r>(mut db: Connection, _user: User, projectid: u64, limit: let count = sqlx::query!( "SELECT COUNT(id) AS count FROM reviews WHERE project=?", - projectid) - .fetch_one(&mut **db) - .map_ok(|r| r.count) - .await - .unwrap(); + projectid + ) + .fetch_one(&mut **db) + .map_ok(|r| r.count) + .await + .unwrap(); let u32_count = u32::try_from(count).unwrap(); - Json( - api_model::Reviews { - offset: uw_offset, - limit: uw_limit, - total_count: u32_count, - more: uw_offset + uw_limit < u32_count, - reviews: entries, - }, - ) + Json(api_model::Reviews { + offset: uw_offset, + limit: uw_limit, + total_count: u32_count, + more: uw_offset + uw_limit < u32_count, + reviews: entries, + }) } #[get("/review//")] -async fn review<'r>(mut db: Connection, _user: User, projectid: u64, reviewid: u64) -> Result, NotFound> { +async fn review<'r>( + mut db: Connection, + _user: User, + projectid: u64, + reviewid: u64, +) -> Result, NotFound> { let mut users = sqlx::query!( "SELECT id,username,name,active,review_users.role AS role FROM users JOIN review_users ON review_users.user=id WHERE review_users.review=? ORDER BY role,username,id", reviewid) @@ -204,7 +226,8 @@ async fn review<'r>(mut db: Connection, _user: User, projectid: u64, reviewi .await .unwrap(); - let first_reviewer = users.iter() + let first_reviewer = users + .iter() .position(|u| matches!(u.role, Role::Reviewer)) .unwrap_or(users.len()); let mut reviewers: Vec = Vec::with_capacity(first_reviewer); @@ -250,7 +273,7 @@ async fn run_migrations(rocket: Rocket) -> fairing::Result { error!("Failed to initialize database: {}", e); Err(rocket) } - } + }, None => Err(rocket), } } -- cgit v1.2.3-70-g09d2