From b4e9fd499f5fc886dc34b4b078e3944e0b5bc03b Mon Sep 17 00:00:00 2001 From: Johannes Heuel Date: Mon, 22 Aug 2022 20:07:39 +0200 Subject: [PATCH] add pre-commit config --- .env | 2 +- .pre-commit-config.yaml | 13 ++++++++++++ backend/Cargo.toml | 2 +- .../2022-04-12-165834_create_photos/down.sql | 2 +- .../2022-04-12-165834_create_photos/up.sql | 2 +- backend/src/actions.rs | 6 +++--- backend/src/bin/add_picture.rs | 20 +++++++++---------- backend/src/bin/show_pictures.rs | 4 ++-- backend/src/lib.rs | 13 ++++++------ common/Cargo.toml | 2 +- common/src/lib.rs | 4 ++-- frontend/src/gallery/mod.rs | 2 +- src/main.rs | 2 +- 13 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.env b/.env index d6706b6..f5eae95 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL=postgres://user:password@localhost/diesel_demo \ No newline at end of file +DATABASE_URL=postgres://user:password@localhost/diesel_demo diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..009d9cc --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/doublify/pre-commit-rust + rev: master + hooks: + - id: fmt + - id: cargo-check diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 0a5a1e1..32aec28 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -19,4 +19,4 @@ dotenv = "0.15.0" walkdir = "2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -common = { path = "../common" } \ No newline at end of file +common = { path = "../common" } diff --git a/backend/migrations/2022-04-12-165834_create_photos/down.sql b/backend/migrations/2022-04-12-165834_create_photos/down.sql index 33a3a43..5b73e6f 100644 --- a/backend/migrations/2022-04-12-165834_create_photos/down.sql +++ b/backend/migrations/2022-04-12-165834_create_photos/down.sql @@ -1 +1 @@ -DROP TABLE pictures \ No newline at end of file +DROP TABLE pictures diff --git a/backend/migrations/2022-04-12-165834_create_photos/up.sql b/backend/migrations/2022-04-12-165834_create_photos/up.sql index d6843f3..52ce61b 100644 --- a/backend/migrations/2022-04-12-165834_create_photos/up.sql +++ b/backend/migrations/2022-04-12-165834_create_photos/up.sql @@ -15,4 +15,4 @@ CREATE TABLE pictures ( exposure_program VARCHAR, exposure_compensation VARCHAR, thumbnail VARCHAR -) \ No newline at end of file +) diff --git a/backend/src/actions.rs b/backend/src/actions.rs index 0576e8c..f393ee8 100644 --- a/backend/src/actions.rs +++ b/backend/src/actions.rs @@ -1,16 +1,16 @@ extern crate diesel; use backend::models::*; -use diesel::prelude::*; use backend::*; +use diesel::prelude::*; type DbError = Box; pub fn list_pictures(conn: &PgConnection) -> Result, DbError> { use self::schema::pictures::dsl::*; - + Ok(pictures .limit(50) .order_by(created_at.desc()) .load::(conn)?) -} \ No newline at end of file +} diff --git a/backend/src/bin/add_picture.rs b/backend/src/bin/add_picture.rs index 492e7a8..b0c5cea 100644 --- a/backend/src/bin/add_picture.rs +++ b/backend/src/bin/add_picture.rs @@ -1,7 +1,7 @@ extern crate diesel; use backend::create_picture; -use backend::models::NewPicture; use backend::establish_connection; +use backend::models::NewPicture; // use backend::*; use std::ffi::OsStr; @@ -123,17 +123,18 @@ fn main() { println!("pe = {}", serde_json::to_string_pretty(pe).unwrap()); - let created_at: Option = if let Some(c) = pe.created_at { Some(c) } else { let metadata = fs::metadata(&path.path()).unwrap(); if let Ok(time) = metadata.created() { - Some(time.duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() - .try_into() - .unwrap()) + Some( + time.duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() + .try_into() + .unwrap(), + ) } else { println!("Not supported on this platform or filesystem"); None @@ -161,10 +162,7 @@ fn main() { }; let pic = create_picture(&connection, new_picture); - println!( - "Created picture with filepath={} and id={}", - filepath, pic - ); + println!("Created picture with filepath={} and id={}", filepath, pic); }); } diff --git a/backend/src/bin/show_pictures.rs b/backend/src/bin/show_pictures.rs index 4585629..5045882 100644 --- a/backend/src/bin/show_pictures.rs +++ b/backend/src/bin/show_pictures.rs @@ -1,8 +1,8 @@ extern crate diesel; use self::models::*; -use diesel::prelude::*; use backend::*; +use diesel::prelude::*; fn main() { use self::schema::pictures::dsl::*; @@ -18,4 +18,4 @@ fn main() { println!("filepath: {}", picture.filepath); println!("\tid: {}", picture.id); } -} \ No newline at end of file +} diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 4890174..960f2b7 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -5,17 +5,16 @@ extern crate dotenv; pub mod models; pub mod schema; -use self::models::{NewPicture}; -use diesel::prelude::*; +use self::models::NewPicture; use diesel::pg::PgConnection; +use diesel::prelude::*; use dotenv::dotenv; use std::env; pub fn establish_connection() -> PgConnection { dotenv().ok(); - let database_url = env::var("DATABASE_URL") - .expect("DATABASE_URL must be set"); + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); PgConnection::establish(&database_url) .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } @@ -24,7 +23,7 @@ pub fn create_picture<'a>(conn: &PgConnection, new_picture: NewPicture) -> usize use schema::pictures; diesel::insert_into(pictures::table) - .values(&new_picture) - .execute(conn) - .expect("Error saving new picture") + .values(&new_picture) + .execute(conn) + .expect("Error saving new picture") } diff --git a/common/Cargo.toml b/common/Cargo.toml index 108e5ea..8da4d72 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -7,4 +7,4 @@ license = "MIT" [dependencies] serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" diff --git a/common/src/lib.rs b/common/src/lib.rs index 17e3884..e090865 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,6 +1,6 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; -#[derive(Serialize,Deserialize,Debug,Clone,PartialEq)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct OutputPicture { pub thumbnail: Option, pub width: u32, diff --git a/frontend/src/gallery/mod.rs b/frontend/src/gallery/mod.rs index 4c8eb77..5796fcd 100644 --- a/frontend/src/gallery/mod.rs +++ b/frontend/src/gallery/mod.rs @@ -4,4 +4,4 @@ pub mod grid; pub use grid::Grid; pub mod picture; -pub use picture::Picture; \ No newline at end of file +pub use picture::Picture; diff --git a/src/main.rs b/src/main.rs index bcf2929..623ab5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,3 @@ fn main() { println!("Hello world!") -} \ No newline at end of file +}