Skip to content

Commit

Permalink
formatting and renaming .jinja files to .j2
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kruse committed Nov 1, 2024
1 parent 53aec73 commit 1f6d5c9
Show file tree
Hide file tree
Showing 101 changed files with 534 additions and 701 deletions.
6 changes: 4 additions & 2 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
edition = "2021"
style_edition = "2024"
max_width = 120
fn_call_width = 120

unstable_features = true

group_imports = "StdExternalCrate"
version = "Two"
imports_granularity = "Module"
format_code_in_doc_comments = true
doc_comment_code_block_width = 120
format_macro_bodies = true
format_macro_matchers = true
format_strings = true

12 changes: 6 additions & 6 deletions src/articles.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use axum::{
middleware::map_response_with_state,
routing::{get, post},
Router,
};
use axum::Router;
use axum::middleware::map_response_with_state;
use axum::routing::{get, post};
use axum_login::login_required;
use chrono::Duration;

use crate::{middleware::caching_middleware, store::Store, AppRouter};
use crate::AppRouter;
use crate::middleware::caching_middleware;
use crate::store::Store;

pub mod actions;
pub mod archive;
Expand Down
117 changes: 39 additions & 78 deletions src/articles/actions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use anyhow::{anyhow, Error};
use anyhow::{Error, anyhow};
use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime};
use sqlx::{query, query_as, query_scalar, Connection, PgConnection};
use sqlx::{Connection, PgConnection, query, query_as, query_scalar};
use validator::Validate;

use crate::{
models::{Article, NewArticle},
uri_helpers::root_uri,
utils::MONTHS,
};
use crate::models::{Article, NewArticle};
use crate::uri_helpers::root_uri;
use crate::utils::MONTHS;

pub async fn list_articles(
limit: i64,
Expand Down Expand Up @@ -56,24 +54,17 @@ pub async fn get_youngest_article(only_visible: bool, conn: &mut PgConnection) -
.fetch_one(conn)
.await
} else {
query_as!(
Article,
"SELECT * FROM articles ORDER BY inserted_at DESC, updated_at DESC, id DESC LIMIT 1"
)
.fetch_one(conn)
.await
query_as!(Article, "SELECT * FROM articles ORDER BY inserted_at DESC, updated_at DESC, id DESC LIMIT 1")
.fetch_one(conn)
.await
}
}

pub async fn get_article(article_id: i32, only_visible: bool, conn: &mut PgConnection) -> Result<Article, sqlx::Error> {
if only_visible {
query_as!(
Article,
"SELECT * FROM articles WHERE id = $1 AND published = true",
article_id
)
.fetch_one(conn)
.await
query_as!(Article, "SELECT * FROM articles WHERE id = $1 AND published = true", article_id)
.fetch_one(conn)
.await
} else {
query_as!(Article, "SELECT * FROM articles WHERE id = $1", article_id)
.fetch_one(conn)
Expand All @@ -87,13 +78,9 @@ pub async fn get_article_by_slug(
conn: &mut PgConnection,
) -> Result<Option<Article>, sqlx::Error> {
if only_visible {
query_as!(
Article,
"SELECT * FROM articles WHERE slug = $1 AND published = true",
article_slug
)
.fetch_optional(conn)
.await
query_as!(Article, "SELECT * FROM articles WHERE slug = $1 AND published = true", article_slug)
.fetch_optional(conn)
.await
} else {
query_as!(Article, "SELECT * FROM articles WHERE slug = $1", article_slug)
.fetch_optional(conn)
Expand All @@ -109,13 +96,9 @@ pub async fn get_article_by_slug_part(
let article_slug = format!("%/{}", article_slug);

if only_visible {
query_as!(
Article,
"SELECT * FROM articles WHERE slug ILIKE $1 AND published = true",
article_slug
)
.fetch_optional(conn)
.await
query_as!(Article, "SELECT * FROM articles WHERE slug ILIKE $1 AND published = true", article_slug)
.fetch_optional(conn)
.await
} else {
query_as!(Article, "SELECT * FROM articles WHERE slug ILIKE $1", article_slug)
.fetch_optional(conn)
Expand Down Expand Up @@ -268,14 +251,11 @@ pub async fn get_articles_for_year_and_month(
let date = NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid date"))?;
let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap();
let dt = NaiveDateTime::new(date, time);
let days_in_mon = NaiveDate::from_ymd_opt(
if month == 12 { year + 1 } else { year },
if month == 12 { 1 } else { month + 1 },
1,
)
.ok_or_else(|| anyhow!("invalid date"))?
.signed_duration_since(NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid_date"))?)
.num_days();
let days_in_mon =
NaiveDate::from_ymd_opt(if month == 12 { year + 1 } else { year }, if month == 12 { 1 } else { month + 1 }, 1)
.ok_or_else(|| anyhow!("invalid date"))?
.signed_duration_since(NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid_date"))?)
.num_days();
let dt_end = dt.checked_add_signed(Duration::days(days_in_mon)).unwrap_or(dt);

let articles = if only_visible {
Expand Down Expand Up @@ -316,32 +296,21 @@ pub async fn count_articles_for_year_and_month(
let date = NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid date"))?;
let time = NaiveTime::from_hms_opt(0, 0, 0).unwrap();
let dt = NaiveDateTime::new(date, time);
let days_in_mon = NaiveDate::from_ymd_opt(
if month == 12 { year + 1 } else { year },
if month == 12 { 1 } else { month + 1 },
1,
)
.ok_or_else(|| anyhow!("invalid date"))?
.signed_duration_since(NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid_date"))?)
.num_days();
let days_in_mon =
NaiveDate::from_ymd_opt(if month == 12 { year + 1 } else { year }, if month == 12 { 1 } else { month + 1 }, 1)
.ok_or_else(|| anyhow!("invalid date"))?
.signed_duration_since(NaiveDate::from_ymd_opt(year, month, 1).ok_or_else(|| anyhow!("invalid_date"))?)
.num_days();
let dt_end = dt.checked_add_signed(Duration::days(days_in_mon)).unwrap_or(dt);

let cnt = if only_visible {
query_scalar!(
"SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2",
dt,
dt_end
)
.fetch_one(conn)
.await?
query_scalar!("SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2", dt, dt_end)
.fetch_one(conn)
.await?
} else {
query_scalar!(
"SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2",
dt,
dt_end
)
.fetch_one(conn)
.await?
query_scalar!("SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2", dt, dt_end)
.fetch_one(conn)
.await?
};

Ok(cnt.unwrap_or(0))
Expand Down Expand Up @@ -401,21 +370,13 @@ pub async fn count_articles_for_year(year: i32, only_visible: bool, conn: &mut P
let dt_end = NaiveDateTime::new(date, time);

let cnt = if only_visible {
query_scalar!(
"SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2",
dt,
dt_end
)
.fetch_one(conn)
.await?
query_scalar!("SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2", dt, dt_end)
.fetch_one(conn)
.await?
} else {
query_scalar!(
"SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2",
dt,
dt_end
)
.fetch_one(conn)
.await?
query_scalar!("SELECT COUNT(*) FROM articles WHERE inserted_at > $1 AND inserted_at < $2", dt, dt_end)
.fetch_one(conn)
.await?
};

Ok(cnt.unwrap_or(0))
Expand Down
84 changes: 39 additions & 45 deletions src/articles/archive.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use askama::Template;
use axum::{
extract::{Path, Query, State},
http::header,
response::IntoResponse,
};
use axum::extract::{Path, Query, State};
use axum::http::header;
use axum::response::IntoResponse;
#[cfg(not(debug_assertions))]
use chrono::Duration;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc};

use super::{actions, PER_PAGE};
use crate::{
errors::AppError, models::Article, uri_helpers::*, utils as filters, utils::paging::*, AppState, AuthSession,
};
use super::{PER_PAGE, actions};
use crate::errors::AppError;
use crate::models::Article;
use crate::uri_helpers::*;
use crate::utils::paging::*;
use crate::{AppState, AuthSession, utils as filters};

#[derive(Template)]
#[template(path = "articles/archive_month.html.jinja")]
#[template(path = "articles/archive_month.html.j2")]
pub struct Month<'a> {
lang: &'a str,
title: Option<String>,
Expand Down Expand Up @@ -74,28 +74,25 @@ pub async fn monthly_view(
[(header::EXPIRES, value), (header::CACHE_CONTROL, duration_seconds)]
};

Ok((
headers,
Month {
lang: "en",
title: Some(format!("Articles in {}", date.format("%B, %Y"))),
page_type: None,
page_image: None,
body_id: None,
paging,
logged_in,
year,
date,
short_month: month_str,
articles,
index: true,
atom: false,
},
))
Ok((headers, Month {
lang: "en",
title: Some(format!("Articles in {}", date.format("%B, %Y"))),
page_type: None,
page_image: None,
body_id: None,
paging,
logged_in,
year,
date,
short_month: month_str,
articles,
index: true,
atom: false,
}))
}

#[derive(Template)]
#[template(path = "articles/archive_year.html.jinja")]
#[template(path = "articles/archive_year.html.j2")]
pub struct Year<'a> {
lang: &'a str,
title: Option<String>,
Expand Down Expand Up @@ -146,20 +143,17 @@ pub async fn yearly_view(
[(header::EXPIRES, value), (header::CACHE_CONTROL, duration_seconds)]
};

Ok((
headers,
Year {
lang: "en",
title: Some(format!("Articles in {}", year)),
page_type: None,
page_image: None,
body_id: None,
logged_in,
paging,
year,
articles,
index: true,
atom: false,
},
))
Ok((headers, Year {
lang: "en",
title: Some(format!("Articles in {}", year)),
page_type: None,
page_image: None,
body_id: None,
logged_in,
paging,
year,
articles,
index: true,
atom: false,
}))
}
10 changes: 5 additions & 5 deletions src/articles/delete.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use axum::{
extract::{Path, State},
response::{IntoResponse, Redirect},
};
use axum::extract::{Path, State};
use axum::response::{IntoResponse, Redirect};

use super::actions;
use crate::{errors::AppError, uri_helpers::*, AppState};
use crate::AppState;
use crate::errors::AppError;
use crate::uri_helpers::*;

pub async fn delete(State(state): State<AppState>, Path(id): Path<i32>) -> Result<impl IntoResponse, AppError> {
let mut conn = state.pool.acquire().await?;
Expand Down
23 changes: 9 additions & 14 deletions src/articles/edit.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use askama::Template;
use axum::{
extract::{Form, Path, State},
response::{IntoResponse, Redirect, Response},
};
use axum::extract::{Form, Path, State};
use axum::response::{IntoResponse, Redirect, Response};

use super::actions;
use crate::{
errors::AppError,
models::{Article, NewArticle},
posse::mastodon::post_article,
uri_helpers::*,
utils as filters,
webmentions::send::send_mentions,
AppState, AuthSession,
};
use crate::errors::AppError;
use crate::models::{Article, NewArticle};
use crate::posse::mastodon::post_article;
use crate::uri_helpers::*;
use crate::webmentions::send::send_mentions;
use crate::{AppState, AuthSession, utils as filters};

#[derive(Template)]
#[template(path = "articles/edit.html.jinja")]
#[template(path = "articles/edit.html.j2")]
pub struct Edit<'a> {
lang: &'a str,
title: Option<String>,
Expand Down
Loading

0 comments on commit 1f6d5c9

Please sign in to comment.