add pre-commit config

This commit is contained in:
Johannes Heuel
2022-08-22 20:07:39 +02:00
parent 9c6b9d173c
commit 12e5c79a5e
13 changed files with 42 additions and 32 deletions

2
.env
View File

@@ -1 +1 @@
DATABASE_URL=postgres://user:password@localhost/diesel_demo
DATABASE_URL=postgres://user:password@localhost/diesel_demo

13
.pre-commit-config.yaml Normal file
View File

@@ -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

View File

@@ -19,4 +19,4 @@ dotenv = "0.15.0"
walkdir = "2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
common = { path = "../common" }
common = { path = "../common" }

View File

@@ -1 +1 @@
DROP TABLE pictures
DROP TABLE pictures

View File

@@ -15,4 +15,4 @@ CREATE TABLE pictures (
exposure_program VARCHAR,
exposure_compensation VARCHAR,
thumbnail VARCHAR
)
)

View File

@@ -1,16 +1,16 @@
extern crate diesel;
use backend::models::*;
use diesel::prelude::*;
use backend::*;
use diesel::prelude::*;
type DbError = Box<dyn std::error::Error + Send + Sync>;
pub fn list_pictures(conn: &PgConnection) -> Result<Vec<Picture>, DbError> {
use self::schema::pictures::dsl::*;
Ok(pictures
.limit(50)
.order_by(created_at.desc())
.load::<Picture>(conn)?)
}
}

View File

@@ -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<i32> = 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);
});
}

View File

@@ -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);
}
}
}

View File

@@ -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")
}

View File

@@ -7,4 +7,4 @@ license = "MIT"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0"

View File

@@ -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<String>,
pub width: u32,

View File

@@ -2,4 +2,4 @@ pub mod grid;
pub use grid::Grid;
pub mod picture;
pub use picture::Picture;
pub use picture::Picture;

View File

@@ -1,3 +1,3 @@
fn main() {
println!("Hello world!")
}
}