Rename db to util and use OffsetDateTime with sqlx
This commit is contained in:
parent
6fcd073738
commit
553a56bb12
7 changed files with 46 additions and 28 deletions
|
|
@ -1,8 +1,8 @@
|
|||
mod config;
|
||||
mod db;
|
||||
mod recurring;
|
||||
mod somehow;
|
||||
mod state;
|
||||
mod util;
|
||||
mod web;
|
||||
|
||||
use std::{io, path::PathBuf, process};
|
||||
|
|
@ -130,7 +130,7 @@ async fn run() -> somehow::Result<()> {
|
|||
// running. Maybe this is due to the thread pool not deferring blocking
|
||||
// work to a separate thread? In any case, replacing it with a sleep
|
||||
// doesn't block the signals.
|
||||
//
|
||||
//
|
||||
// In order to fix this, I could maybe register a bare signal handler
|
||||
// (instead of using tokio streams) that just calls process::exit(1) and
|
||||
// nothing else?
|
||||
|
|
|
|||
|
|
@ -1,12 +1,17 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use time::{format_description::well_known::Rfc3339, macros::format_description, OffsetDateTime};
|
||||
use gix::date::Time;
|
||||
use time::{macros::format_description, OffsetDateTime, UtcOffset};
|
||||
|
||||
use crate::somehow;
|
||||
|
||||
pub fn format_time(time: &str) -> somehow::Result<String> {
|
||||
pub fn time_to_offset_datetime(time: Time) -> somehow::Result<OffsetDateTime> {
|
||||
Ok(OffsetDateTime::from_unix_timestamp(time.seconds)?
|
||||
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))
|
||||
}
|
||||
|
||||
pub fn format_time(time: OffsetDateTime) -> somehow::Result<String> {
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let time = OffsetDateTime::parse(time, &Rfc3339)?;
|
||||
let delta = time - now;
|
||||
|
||||
let formatted_time = time.format(format_description!(
|
||||
|
|
@ -21,7 +26,7 @@ pub fn format_time(time: &str) -> somehow::Result<String> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn summary(message: &str) -> String {
|
||||
pub fn format_commit_summary(message: &str) -> String {
|
||||
// Take everything up to the first double newline
|
||||
let title = message
|
||||
.split_once("\n\n")
|
||||
|
|
@ -34,6 +39,6 @@ pub fn summary(message: &str) -> String {
|
|||
|
||||
pub fn format_commit_short(hash: &str, message: &str) -> String {
|
||||
let short_hash = hash.chars().take(8).collect::<String>();
|
||||
let summary = summary(message);
|
||||
let summary = format_commit_summary(message);
|
||||
format!("{short_hash} ({summary})")
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ use axum::{
|
|||
use futures::TryStreamExt;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{config::Config, db, somehow};
|
||||
use crate::{config::Config, somehow, util};
|
||||
|
||||
struct Commit {
|
||||
hash: String,
|
||||
|
|
@ -18,7 +18,7 @@ struct Commit {
|
|||
impl Commit {
|
||||
fn new(hash: String, message: &str, reachable: i64) -> Self {
|
||||
Self {
|
||||
short: db::format_commit_short(&hash, message),
|
||||
short: util::format_commit_short(&hash, message),
|
||||
hash,
|
||||
reachable,
|
||||
}
|
||||
|
|
@ -48,9 +48,23 @@ pub async fn get(
|
|||
State(config): State<&'static Config>,
|
||||
State(db): State<SqlitePool>,
|
||||
) -> somehow::Result<Response> {
|
||||
let Some(commit) = sqlx::query!("SELECT * FROM commits WHERE hash = ?", hash)
|
||||
.fetch_optional(&db)
|
||||
.await?
|
||||
let Some(commit) = sqlx::query!(
|
||||
"\
|
||||
SELECT \
|
||||
hash, \
|
||||
author, \
|
||||
author_date AS \"author_date: time::OffsetDateTime\", \
|
||||
committer, \
|
||||
committer_date AS \"committer_date: time::OffsetDateTime\", \
|
||||
message, \
|
||||
reachable \
|
||||
FROM commits \
|
||||
WHERE hash = ? \
|
||||
",
|
||||
hash
|
||||
)
|
||||
.fetch_optional(&db)
|
||||
.await?
|
||||
else {
|
||||
return Ok(StatusCode::NOT_FOUND.into_response());
|
||||
};
|
||||
|
|
@ -89,12 +103,12 @@ pub async fn get(
|
|||
current: "commit".to_string(),
|
||||
hash: commit.hash,
|
||||
author: commit.author,
|
||||
author_date: db::format_time(&commit.author_date)?,
|
||||
author_date: util::format_time(commit.author_date)?,
|
||||
commit: commit.committer,
|
||||
commit_date: db::format_time(&commit.committer_date)?,
|
||||
commit_date: util::format_time(commit.committer_date)?,
|
||||
parents,
|
||||
children,
|
||||
summary: db::summary(&commit.message),
|
||||
summary: util::format_commit_summary(&commit.message),
|
||||
message: commit.message.trim_end().to_string(),
|
||||
reachable: commit.reachable,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use axum::{extract::State, response::IntoResponse};
|
|||
use futures::TryStreamExt;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{config::Config, db, somehow};
|
||||
use crate::{config::Config, somehow, util};
|
||||
|
||||
struct Ref {
|
||||
name: String,
|
||||
|
|
@ -36,7 +36,7 @@ pub async fn get(
|
|||
.fetch(&db)
|
||||
.map_ok(|r| Ref {
|
||||
name: r.name,
|
||||
short: db::format_commit_short(&r.hash, &r.message),
|
||||
short: util::format_commit_short(&r.hash, &r.message),
|
||||
hash: r.hash,
|
||||
tracked: r.tracked != 0,
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue