From 90d461025c93c619ccf2a0a25890bad92088d4bd Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sun, 24 Sep 2023 22:39:54 +0200 Subject: [PATCH] Add pre-commit config --- .pre-commit-config.yaml | 7 +++ src/config.rs | 2 +- src/main.rs | 29 ++++------ src/types.rs | 119 ++++++++++++---------------------------- src/updater.rs | 102 +++++++++++++++++----------------- src/utils.rs | 10 +--- 6 files changed, 108 insertions(+), 161 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..0d99f5c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: +- repo: https://github.com/doublify/pre-commit-rust + rev: v1.0 + hooks: + - id: fmt + - id: cargo-check + - id: clippy diff --git a/src/config.rs b/src/config.rs index d5003d0..e1023e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ pub enum Method { #[serde(rename = "get")] Get, #[serde(rename = "post")] - Post + Post, } #[derive(Deserialize, Clone)] diff --git a/src/main.rs b/src/main.rs index 288c940..1d4f664 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,19 +3,18 @@ extern crate lazy_static; pub mod config; pub mod types; -pub mod utils; pub mod updater; +pub mod utils; +use axum::{http::HeaderMap, routing::post, Router}; +use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions}; use std::{net::SocketAddr, str::FromStr}; -use axum::{Router, routing::post, http::HeaderMap}; -use sentry::{ClientOptions, types::Dsn, integrations::debug_images::DebugImagesIntegration}; +use tower_http::trace::{self, TraceLayer}; use tracing::log; -use tower_http::trace::{TraceLayer, self}; use tracing::Level; use crate::updater::cron_jobs; - async fn update(headers: HeaderMap) -> &'static str { let config_api_key = config::CONFIG.api_key.clone(); @@ -25,7 +24,7 @@ async fn update(headers: HeaderMap) -> &'static str { }; if config_api_key != api_key.to_str().unwrap() { - return "Wrong api-key!" + return "Wrong api-key!"; } tokio::spawn(async { @@ -38,15 +37,12 @@ async fn update(headers: HeaderMap) -> &'static str { "Update started" } - async fn start_app() { - let app = Router::new() - .route("/update", post(update)) - .layer( - TraceLayer::new_for_http() - .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) - .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), - ); + let app = Router::new().route("/update", post(update)).layer( + TraceLayer::new_for_http() + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), + ); let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); @@ -74,8 +70,5 @@ async fn main() { let _guard = sentry::init(options); - tokio::join![ - cron_jobs(), - start_app() - ]; + tokio::join![cron_jobs(), start_app()]; } diff --git a/src/types.rs b/src/types.rs index b529a0a..f073c30 100644 --- a/src/types.rs +++ b/src/types.rs @@ -11,9 +11,7 @@ pub trait FromVecExpression { #[async_trait] pub trait Update { - async fn before_update( - client: &Client, - ) -> Result<(), Box>; + async fn before_update(client: &Client) -> Result<(), Box>; async fn update( &self, @@ -21,9 +19,7 @@ pub trait Update { source_id: i16, ) -> Result<(), Box>; - async fn after_update( - client: &Client, - ) -> Result<(), Box>; + async fn after_update(client: &Client) -> Result<(), Box>; } #[derive(Debug)] @@ -59,9 +55,7 @@ impl FromVecExpression for Author { #[async_trait] impl Update for Author { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_author( @@ -98,9 +92,7 @@ impl Update for Author { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -157,9 +149,7 @@ impl FromVecExpression for Book { #[async_trait] impl Update for Book { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_book( @@ -198,13 +188,14 @@ impl Update for Book { } } - async fn after_update( - client: &Client - ) -> Result<(), Box> { - match client.execute( - "UPDATE books SET is_deleted = 't' WHERE lang NOT IN ('ru', 'be', 'uk');", - &[] - ).await { + async fn after_update(client: &Client) -> Result<(), Box> { + match client + .execute( + "UPDATE books SET is_deleted = 't' WHERE lang NOT IN ('ru', 'be', 'uk');", + &[], + ) + .await + { Ok(_) => Ok(()), Err(err) => Err(Box::new(err)), } @@ -235,9 +226,7 @@ impl FromVecExpression for BookAuthor { #[async_trait] impl Update for BookAuthor { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_book_author(source_ smallint, book_ integer, author_ integer) RETURNS void AS $$ @@ -283,9 +272,7 @@ impl Update for BookAuthor { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -318,9 +305,7 @@ impl FromVecExpression for Translator { #[async_trait] impl Update for Translator { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_translation(source_ smallint, book_ integer, author_ integer, position_ smallint) RETURNS void AS $$ @@ -372,9 +357,7 @@ impl Update for Translator { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -402,9 +385,7 @@ impl FromVecExpression for Sequence { #[async_trait] impl Update for Sequence { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_sequences(source_ smallint, remote_id_ int, name_ varchar) RETURNS void AS $$ @@ -440,9 +421,7 @@ impl Update for Sequence { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -472,9 +451,7 @@ impl FromVecExpression for SequenceInfo { op_span: _, operand, } => match (op, operand.as_ref()) { - (sql_parse::UnaryOperator::Minus, Expression::Integer(v)) => { - v.0 - } + (sql_parse::UnaryOperator::Minus, Expression::Integer(v)) => v.0, (_, _) => panic!("SequenceInfo.position = {:?}", &value[2]), }, _ => panic!("SequenceInfo.position = {:?}", &value[2]), @@ -485,9 +462,7 @@ impl FromVecExpression for SequenceInfo { #[async_trait] impl Update for SequenceInfo { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_book_sequence(source_ smallint, book_ integer, sequence_ integer, position_ smallint) RETURNS void AS $$ @@ -543,9 +518,7 @@ impl Update for SequenceInfo { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -579,9 +552,7 @@ impl FromVecExpression for BookAnnotation { #[async_trait] impl Update for BookAnnotation { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_book_annotation(source_ smallint, book_ integer, title_ varchar, text_ text) RETURNS void AS $$ @@ -625,9 +596,7 @@ impl Update for BookAnnotation { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -655,9 +624,7 @@ impl FromVecExpression for BookAnnotationPic { #[async_trait] impl Update for BookAnnotationPic { - async fn before_update( - _client: &Client - ) -> Result<(), Box> { + async fn before_update(_client: &Client) -> Result<(), Box> { Ok(()) } @@ -683,9 +650,7 @@ WHERE book = books.id;\ } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -719,9 +684,7 @@ impl FromVecExpression for AuthorAnnotation { #[async_trait] impl Update for AuthorAnnotation { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_author_annotation(source_ smallint, author_ integer, title_ varchar, text_ text) RETURNS void AS $$ @@ -765,9 +728,7 @@ impl Update for AuthorAnnotation { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -795,9 +756,7 @@ impl FromVecExpression for AuthorAnnotationPic { #[async_trait] impl Update for AuthorAnnotationPic { - async fn before_update( - _client: &Client - ) -> Result<(), Box> { + async fn before_update(_client: &Client) -> Result<(), Box> { Ok(()) } @@ -822,9 +781,7 @@ WHERE author = authors.id;", } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -862,9 +819,7 @@ impl FromVecExpression for Genre { #[async_trait] impl Update for Genre { - async fn before_update( - client: &Client - ) -> Result<(), Box> { + async fn before_update(client: &Client) -> Result<(), Box> { match client.execute( " CREATE OR REPLACE FUNCTION update_book_sequence(source_ smallint, book_ integer, genre_ integer) RETURNS void AS $$ @@ -908,9 +863,7 @@ impl Update for Genre { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } @@ -938,9 +891,7 @@ impl FromVecExpression for BookGenre { #[async_trait] impl Update for BookGenre { - async fn before_update( - _client: &Client - ) -> Result<(), Box> { + async fn before_update(_client: &Client) -> Result<(), Box> { Ok(()) } @@ -961,9 +912,7 @@ impl Update for BookGenre { } } - async fn after_update( - _client: &Client - ) -> Result<(), Box> { + async fn after_update(_client: &Client) -> Result<(), Box> { Ok(()) } } diff --git a/src/updater.rs b/src/updater.rs index d4c9cdf..3d52115 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,31 +1,27 @@ -use std::{ - fmt::Debug, - sync::Arc, - str::FromStr -}; +use std::{fmt::Debug, str::FromStr, sync::Arc}; -use crate::config::{Webhook, self}; +use crate::config::{self, Webhook}; use deadpool_postgres::{Config, CreatePoolError, ManagerConfig, Pool, RecyclingMethod, Runtime}; use futures::{io::copy, TryStreamExt}; -use reqwest::header::{HeaderMap, HeaderValue, HeaderName}; -use tokio::fs::{File, remove_file}; +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; +use tokio::fs::{remove_file, File}; use tokio::sync::Mutex; -use tokio_cron_scheduler::{JobScheduler, Job}; +use tokio_cron_scheduler::{Job, JobScheduler}; use tokio_postgres::NoTls; use tracing::log; use async_compression::futures::bufread::GzipDecoder; -use sql_parse::{ - parse_statement, InsertReplace, InsertReplaceType, ParseOptions, SQLArguments, SQLDialect, - Statement, -}; -use tokio_util::compat::TokioAsyncReadCompatExt; use crate::types::{ Author, AuthorAnnotation, AuthorAnnotationPic, BookAnnotation, BookAnnotationPic, BookAuthor, BookGenre, FromVecExpression, Genre, Sequence, SequenceInfo, Translator, Update, }; use crate::utils::read_lines; +use sql_parse::{ + parse_statement, InsertReplace, InsertReplaceType, ParseOptions, SQLArguments, SQLDialect, + Statement, +}; +use tokio_util::compat::TokioAsyncReadCompatExt; use crate::types::Book; @@ -53,8 +49,8 @@ async fn download_file(filename_str: &str) -> Result<(), Box v.compat(), Err(err) => { log::error!("Can't create {filename_str}: {:?}", err); - return Err(Box::new(err)) - }, + return Err(Box::new(err)); + } }; let data = response @@ -68,8 +64,8 @@ async fn download_file(filename_str: &str) -> Result<(), Box (), Err(err) => { log::error!("Can't write data {filename_str}: {}", err); - return Err(Box::new(err)) - }, + return Err(Box::new(err)); + } }; log::info!("{filename_str} downloaded!"); @@ -148,7 +144,8 @@ where type_: InsertReplaceType::Insert(_), .. }, - )) = ast { + )) = ast + { for value in i.values.into_iter() { for t_value in value.1.into_iter() { let value = T::from_vec_expression(&t_value); @@ -160,8 +157,8 @@ where } Err(err) => { log::error!("Update error: {:?} : {:?}", value, err); - return Err(err) - }, + return Err(err); + } } } } @@ -220,30 +217,33 @@ enum UpdateStatus { async fn send_webhooks() -> Result<(), Box> { for webhook in config::CONFIG.webhooks.clone().into_iter() { - let Webhook { method, url, headers } = webhook; + let Webhook { + method, + url, + headers, + } = webhook; let client = reqwest::Client::new(); let builder = match method { - config::Method::Get => { - client.get(url) - }, - config::Method::Post => { - client.post(url) - }, + config::Method::Get => client.get(url), + config::Method::Post => client.post(url), }; - let t_headers: Vec<(HeaderName, HeaderValue)> = headers.into_iter().map(|(key, val)| { - let value = match val { - serde_json::Value::String(v) => v, - _ => panic!("Header value not string!") - }; + let t_headers: Vec<(HeaderName, HeaderValue)> = headers + .into_iter() + .map(|(key, val)| { + let value = match val { + serde_json::Value::String(v) => v, + _ => panic!("Header value not string!"), + }; - ( - HeaderName::from_str(key.as_ref()).unwrap(), - HeaderValue::from_str(&value).unwrap() - ) - }).collect(); + ( + HeaderName::from_str(key.as_ref()).unwrap(), + HeaderValue::from_str(&value).unwrap(), + ) + }) + .collect(); let headers = HeaderMap::from_iter(t_headers.into_iter()); @@ -258,7 +258,7 @@ async fn send_webhooks() -> Result<(), Box> { Ok(_) => (), Err(err) => return Err(Box::new(err)), }; - }; + } Ok(()) } @@ -479,7 +479,7 @@ pub async fn update() -> Result<(), Box> { author_annotation_process, author_annotation_pics_process, genre_annotation_process, - book_genre_process + book_genre_process, ] { let process_result = match process.await { Ok(v) => v, @@ -495,11 +495,11 @@ pub async fn update() -> Result<(), Box> { match send_webhooks().await { Ok(_) => { log::info!("Webhooks sended!"); - }, + } Err(err) => { log::info!("Webhooks send failed : {err}"); - return Err(Box::new(err)) - }, + return Err(Box::new(err)); + } }; Ok(()) @@ -508,12 +508,14 @@ pub async fn update() -> Result<(), Box> { pub async fn cron_jobs() { let job_scheduler = JobScheduler::new().await.unwrap(); - let update_job = match Job::new_async("0 0 3 * * *", |_uuid, _l| Box::pin(async { - match update().await { - Ok(_) => log::info!("Updated"), - Err(err) => log::info!("Update err: {:?}", err), - }; - })) { + let update_job = match Job::new_async("0 0 3 * * *", |_uuid, _l| { + Box::pin(async { + match update().await { + Ok(_) => log::info!("Updated"), + Err(err) => log::info!("Update err: {:?}", err), + }; + }) + }) { Ok(v) => v, Err(err) => panic!("{:?}", err), }; @@ -525,4 +527,4 @@ pub async fn cron_jobs() { Ok(v) => v, Err(err) => panic!("{:?}", err), }; -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 41ec72a..b91e6e9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,8 @@ +use ammonia::Builder; +use maplit::hashset; use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; -use ammonia::Builder; -use maplit::hashset; pub fn read_lines

(filename: P) -> io::Result>> where @@ -35,13 +35,9 @@ pub fn fix_annotation_text(text: &str) -> String { } let tags = hashset!["a"]; - Builder::new() - .tags(tags) - .clean(&temp_text) - .to_string() + Builder::new().tags(tags).clean(&temp_text).to_string() } - #[cfg(test)] mod tests { use crate::utils::fix_annotation_text;