From 4a39b10afb82ef4f4f986084c7ebd417b417623a Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Thu, 14 Jul 2022 17:06:38 -0700 Subject: [PATCH] cargo fmt --- src/database.rs | 7 ++++++- src/main.rs | 52 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/database.rs b/src/database.rs index b173457..b123ec1 100644 --- a/src/database.rs +++ b/src/database.rs @@ -43,7 +43,12 @@ impl Database { pub fn create_comment(&self, comment: &Comment) -> Result<()> { self.conn.execute( "INSERT INTO comment (author, email, text, content_id) VALUES (?1, ?2, ?3, ?4)", - params![&comment.author, &comment.email, &comment.text, &comment.content_id], + params![ + &comment.author, + &comment.email, + &comment.text, + &comment.content_id + ], )?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index c63e58f..8938432 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,34 @@ -mod comment; use actix_cors::Cors; +mod comment; +use actix_cors::Cors; pub use comment::*; mod database; pub use database::Database; -use actix_web::{get, post, web, App, HttpResponse, HttpRequest, HttpServer, Responder}; -use std::{env, sync::{Mutex, MutexGuard}}; -use validator::Validate; +use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder}; use scraper::{Html, Selector}; -use std::collections::HashMap; use serde::Deserialize; +use std::collections::HashMap; +use std::{ + env, + sync::{Mutex, MutexGuard}, +}; +use validator::Validate; struct AppState { databases: HashMap>, } -fn get_db<'a>(data: &'a web::Data, request: &HttpRequest) -> Result, HttpResponse> { +fn get_db<'a>( + data: &'a web::Data, + request: &HttpRequest, +) -> Result, HttpResponse> { // all the .into() are converting from HttpResponseBuilder to HttpResponse let origin = match request.head().headers().get("Origin") { Some(origin) => match origin.to_str() { Ok(origin) => origin, Err(_) => return Err(HttpResponse::BadRequest().into()), - } + }, None => return Err(HttpResponse::BadRequest().into()), }; match data.databases.get(origin) { @@ -30,11 +37,15 @@ fn get_db<'a>(data: &'a web::Data, request: &HttpRequest) -> Result return Err(HttpResponse::InternalServerError().into()), }), None => return Err(HttpResponse::BadRequest().into()), - } + } } #[get("/{content_id}")] -async fn get_comments(data: web::Data, request: HttpRequest, content_id: web::Path) -> impl Responder { +async fn get_comments( + data: web::Data, + request: HttpRequest, + content_id: web::Path, +) -> impl Responder { let database = match get_db(&data, &request) { Ok(database) => database, Err(response) => return response, @@ -49,7 +60,11 @@ struct PostCommentsRequest { } #[post("/")] -async fn post_comment(data: web::Data, request: HttpRequest, bytes: web::Bytes) -> impl Responder { +async fn post_comment( + data: web::Data, + request: HttpRequest, + bytes: web::Bytes, +) -> impl Responder { match String::from_utf8(bytes.to_vec()) { Ok(text) => { let PostCommentsRequest { url, comment } = match serde_json::from_str(&text) { @@ -82,9 +97,11 @@ async fn post_comment(data: web::Data, request: HttpRequest, bytes: we } match get_page_data(&url).await { Ok(page_data_option) => match page_data_option { - Some(page_data) => if page_data.content_id != comment.content_id { - return HttpResponse::BadRequest().into(); - }, + Some(page_data) => { + if page_data.content_id != comment.content_id { + return HttpResponse::BadRequest().into(); + } + } None => return HttpResponse::BadRequest().into(), }, Err(_) => return HttpResponse::InternalServerError().into(), @@ -116,7 +133,7 @@ async fn get_page_data(url: &str) -> Result, reqwest::Error> { let document = Html::parse_document(&content); let get_meta = |name: &str| -> Option { let selector = Selector::parse(&format!("meta[name=\"{}\"]", name)).unwrap(); - match document.select(&selector).next() { + match document.select(&selector).next() { Some(element) => match element.value().attr("content") { Some(value) => Some(value.to_owned()), None => return None, @@ -129,7 +146,7 @@ async fn get_page_data(url: &str) -> Result, reqwest::Error> { Some(id) => id, None => return Ok(None), }, - })) + })); } #[actix_web::main] @@ -153,7 +170,10 @@ async fn main() -> Result<(), std::io::Error> { } let mut databases = HashMap::new(); for domain in domains.iter() { - databases.insert(domain.to_owned(), Mutex::new(Database::new(testing).unwrap())); + databases.insert( + domain.to_owned(), + Mutex::new(Database::new(testing).unwrap()), + ); } let state = web::Data::new(AppState { databases }); HttpServer::new(move || {