From aabc4e8f3803062fa240c3d74c4e595dd9b82258 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sun, 24 Sep 2023 22:52:44 +0200 Subject: [PATCH] Add pre-commit config --- .pre-commit-config.yaml | 7 ++++++ src/config.rs | 4 ++-- src/main.rs | 4 ++-- src/models.rs | 30 +++++++++++--------------- src/updater.rs | 48 +++++++++++++++++++++++------------------ 5 files changed, 51 insertions(+), 42 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 22c1ccc..36bed7d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,7 +10,7 @@ pub struct Config { pub postgres_password: String, pub meili_host: String, - pub meili_master_key: String + pub meili_master_key: String, } fn get_env(env: &'static str) -> String { @@ -31,7 +31,7 @@ impl Config { postgres_password: get_env("POSTGRES_PASSWORD"), meili_host: get_env("MEILI_HOST"), - meili_master_key: get_env("MEILI_MASTER_KEY") + meili_master_key: get_env("MEILI_MASTER_KEY"), } } } diff --git a/src/main.rs b/src/main.rs index 366b16b..a6b24b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ extern crate lazy_static; pub mod config; -pub mod updater; pub mod models; +pub mod updater; use axum::{http::HeaderMap, routing::post, Router}; -use sentry::{ClientOptions, types::Dsn, integrations::debug_images::DebugImagesIntegration}; +use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions}; use std::{net::SocketAddr, str::FromStr}; async fn update(headers: HeaderMap) -> &'static str { diff --git a/src/models.rs b/src/models.rs index 4ad8373..3e5de5d 100644 --- a/src/models.rs +++ b/src/models.rs @@ -15,7 +15,7 @@ pub struct Book { pub id: i32, pub title: String, pub lang: String, - pub genres: Vec + pub genres: Vec, } impl UpdateModel for Book { @@ -32,7 +32,7 @@ impl UpdateModel for Book { id: row.get(0), title: row.get(1), lang: row.get(2), - genres: row.get(3) + genres: row.get(3), } } @@ -41,10 +41,7 @@ impl UpdateModel for Book { } fn get_filterable_attributes() -> Vec { - vec![ - "lang".to_string(), - "genres".to_string() - ] + vec!["lang".to_string(), "genres".to_string()] } fn get_ranking_rules() -> Vec { @@ -59,7 +56,6 @@ impl UpdateModel for Book { } } - #[derive(Serialize, Deserialize, Debug)] pub struct Author { pub id: i32, @@ -134,18 +130,17 @@ impl UpdateModel for Author { "attribute".to_string(), "sort".to_string(), "exactness".to_string(), - "books_count:desc".to_string() + "books_count:desc".to_string(), ] } } - #[derive(Serialize, Deserialize, Debug)] pub struct Sequence { pub id: i32, pub name: String, pub langs: Vec, - pub books_count: i64 + pub books_count: i64, } impl UpdateModel for Sequence { @@ -167,7 +162,8 @@ impl UpdateModel for Sequence { WHERE sequences.id = book_sequences.sequence AND books.is_deleted = 'f') as books_count FROM sequences; - ".to_string() + " + .to_string() } fn from_row(row: Row) -> Self { @@ -175,7 +171,7 @@ impl UpdateModel for Sequence { id: row.get(0), name: row.get(1), langs: row.get(2), - books_count: row.get(3) + books_count: row.get(3), } } @@ -195,12 +191,11 @@ impl UpdateModel for Sequence { "attribute".to_string(), "sort".to_string(), "exactness".to_string(), - "books_count:desc".to_string() + "books_count:desc".to_string(), ] } } - #[derive(Serialize, Deserialize, Debug)] pub struct Genre { pub id: i32, @@ -231,7 +226,8 @@ impl UpdateModel for Genre { AND books.is_deleted = 'f' ) as books_count FROM genres; - ".to_string() + " + .to_string() } fn from_row(row: Row) -> Self { @@ -240,7 +236,7 @@ impl UpdateModel for Genre { description: row.get(1), meta: row.get(2), langs: row.get(3), - books_count: row.get(4) + books_count: row.get(4), } } @@ -260,7 +256,7 @@ impl UpdateModel for Genre { "attribute".to_string(), "sort".to_string(), "exactness".to_string(), - "books_count:desc".to_string() + "books_count:desc".to_string(), ] } } diff --git a/src/updater.rs b/src/updater.rs index e9b4d7b..0bb8d74 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,10 +1,13 @@ use deadpool_postgres::{Config, CreatePoolError, ManagerConfig, Pool, RecyclingMethod, Runtime}; -use tokio_postgres::NoTls; -use futures::{StreamExt, pin_mut}; +use futures::{pin_mut, StreamExt}; use meilisearch_sdk::client::*; use serde::Serialize; +use tokio_postgres::NoTls; -use crate::{config, models::{Book, UpdateModel, Author, Sequence, Genre}}; +use crate::{ + config, + models::{Author, Book, Genre, Sequence, UpdateModel}, +}; async fn get_postgres_pool() -> Result { let mut config = Config::new(); @@ -26,14 +29,15 @@ async fn get_postgres_pool() -> Result { } fn get_meili_client() -> Client { - Client::new(config::CONFIG.meili_host.clone(), Some(config::CONFIG.meili_master_key.clone())) + Client::new( + config::CONFIG.meili_host.clone(), + Some(config::CONFIG.meili_master_key.clone()), + ) } -async fn update_model( - pool: Pool, -) -> Result<(), Box> +async fn update_model(pool: Pool) -> Result<(), Box> where - T: UpdateModel + Serialize + T: UpdateModel + Serialize, { let client = pool.get().await.unwrap(); @@ -42,9 +46,7 @@ where let index = meili_client.index(T::get_index()); let params: Vec = vec![]; - let stream = match client.query_raw( - &T::get_query(), params - ).await { + let stream = match client.query_raw(&T::get_query(), params).await { Ok(stream) => stream, Err(err) => return Err(Box::new(err)), }; @@ -55,11 +57,9 @@ where while let Some(chunk) = chunks.next().await { let items: Vec = chunk .into_iter() - .map(|result| { - match result { - Ok(v) => T::from_row(v), - Err(err) => panic!("{}", err), - } + .map(|result| match result { + Ok(v) => T::from_row(v), + Err(err) => panic!("{}", err), }) .collect(); @@ -68,11 +68,17 @@ where }; } - if let Err(err) = index.set_searchable_attributes(T::get_searchable_attributes()).await { + if let Err(err) = index + .set_searchable_attributes(T::get_searchable_attributes()) + .await + { return Err(Box::new(err)); }; - if let Err(err) = index.set_filterable_attributes(T::get_filterable_attributes()).await { + if let Err(err) = index + .set_filterable_attributes(T::get_filterable_attributes()) + .await + { return Err(Box::new(err)); }; @@ -109,7 +115,7 @@ pub async fn update() -> Result<(), Box> { let pool_clone = pool.clone(); let update_sequences_process = tokio::spawn(async move { - match update_model::(pool_clone).await { + match update_model::(pool_clone).await { Ok(_) => (), Err(err) => panic!("{}", err), } @@ -117,7 +123,7 @@ pub async fn update() -> Result<(), Box> { let pool_clone = pool.clone(); let update_genres_process = tokio::spawn(async move { - match update_model::(pool_clone).await { + match update_model::(pool_clone).await { Ok(_) => (), Err(err) => panic!("{}", err), } @@ -127,7 +133,7 @@ pub async fn update() -> Result<(), Box> { update_books_process, update_authors_process, update_sequences_process, - update_genres_process + update_genres_process, ] { match process.await { Ok(v) => v,