From 92979a73915f6e5f3a82291b836b05fcdbe84ed4 Mon Sep 17 00:00:00 2001 From: guochao Date: Sat, 6 Aug 2022 11:56:43 +0800 Subject: [PATCH] cargo fmt --- src/main.rs | 14 +++++++------- src/web/controller.rs | 4 +--- src/web/mod.rs | 23 +++++++++++------------ src/web/urls.rs | 2 +- src/web/view.rs | 16 +++++++++++----- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 70b535c..062034d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::Shell; -use sea_orm::{Database}; +use sea_orm::Database; use sea_orm_migration::prelude::*; #[derive(Parser, Clone)] @@ -10,10 +10,10 @@ struct Opts { #[clap(short, long = "database", value_parser, env)] database_url: String, - #[clap(short, long, value_parser, env, default_value="0.0.0.0")] + #[clap(short, long, value_parser, env, default_value = "0.0.0.0")] address: String, - #[clap(short, long, value_parser, env, default_value="8888")] + #[clap(short, long, value_parser, env, default_value = "8888")] port: u16, #[clap(subcommand)] @@ -37,9 +37,7 @@ enum Commands { #[derive(Subcommand, Clone)] enum MigrationDirection { /// Migrate database up - Up { - steps: Option, - }, + Up { steps: Option }, /// Do a fresh install Fresh, /// Remove previous tables and do a fresh install @@ -72,7 +70,9 @@ async fn main() -> Result<(), Box> { if let Some(command) = args.commands.clone() { match command { Commands::Migrate { direction } => match direction { - MigrationDirection::Up{steps} => migrations::Migrator::up(&database, steps).await?, + MigrationDirection::Up { steps } => { + migrations::Migrator::up(&database, steps).await? + } MigrationDirection::Fresh => migrations::Migrator::fresh(&database).await?, MigrationDirection::Refresh => migrations::Migrator::refresh(&database).await?, }, diff --git a/src/web/controller.rs b/src/web/controller.rs index b2ebaf0..cd4eae9 100644 --- a/src/web/controller.rs +++ b/src/web/controller.rs @@ -1,5 +1,3 @@ pub(crate) struct UserController; -impl UserController { - -} \ No newline at end of file +impl UserController {} diff --git a/src/web/mod.rs b/src/web/mod.rs index 716ec19..06df0dc 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -1,31 +1,30 @@ -use actix_web::{HttpServer, App}; +use actix_web::{App, HttpServer}; use sea_orm::DatabaseConnection; use crate::Opts; mod urls; +mod controller; mod model; mod view; -mod controller; #[derive(Debug, Clone)] struct AppState { database: DatabaseConnection, } -pub(crate) async fn run(database: DatabaseConnection, opts: Opts) -> Result<(), Box> { - let data = actix_web::web::Data::new(AppState { - database: database, - }); +pub(crate) async fn run( + database: DatabaseConnection, + opts: Opts, +) -> Result<(), Box> { + let data = actix_web::web::Data::new(AppState { database: database }); - let server = HttpServer::new(move|| { - App::new() - .configure(urls::configure) - .app_data(data.clone()) - }).bind((opts.address, opts.port))?; + let server = + HttpServer::new(move || App::new().configure(urls::configure).app_data(data.clone())) + .bind((opts.address, opts.port))?; let result = server.run().await?; Ok(result) -} \ No newline at end of file +} diff --git a/src/web/urls.rs b/src/web/urls.rs index f591186..0058bae 100644 --- a/src/web/urls.rs +++ b/src/web/urls.rs @@ -2,4 +2,4 @@ use actix_web::web::ServiceConfig; pub(crate) fn configure(cfg: &mut ServiceConfig) -> () { cfg.service(super::view::index_page); -} \ No newline at end of file +} diff --git a/src/web/view.rs b/src/web/view.rs index 6dfb66d..8d2ff74 100644 --- a/src/web/view.rs +++ b/src/web/view.rs @@ -1,4 +1,4 @@ -use actix_web::{Responder, HttpRequest}; +use actix_web::{HttpRequest, Responder}; use sea_orm::*; use serde::{Deserialize, Serialize}; @@ -13,10 +13,16 @@ pub struct Params { } #[actix_web::get("/")] -async fn index_page(req: HttpRequest, data: actix_web::web::Data) -> Result> { +async fn index_page( + req: HttpRequest, + data: actix_web::web::Data, +) -> Result> { let params = actix_web::web::Query::::from_query(req.query_string()).unwrap(); - let users = User::find().paginate(&data.database, params.posts_per_page.unwrap_or(10)).fetch_page(params.page.unwrap_or(0)).await?; - + let users = User::find() + .paginate(&data.database, params.posts_per_page.unwrap_or(10)) + .fetch_page(params.page.unwrap_or(0)) + .await?; + Ok(actix_web::web::Json(users)) -} \ No newline at end of file +}